Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to filter data, getting data from api, but it isnt working, any solution ?

$scope.filter_all2 = function(value) {
    $scope.filtered_inventories = new Inventory().query({model: value, manufacturer: value}).$object;
};

my template

<select ng-model="inventory.model" ng-change="filter_all2(inventory.model)">
    <option value="">Model</option>
    <option ng-repeat="inventory in inventories | unique:'model'" value="{{inventory.model}}">{{inventory.model}}</div>
</select>

<select ng-model="inventory.manufacturer" ng-change="filter_all2(inventory.manufacturer)">
    <option value="">Manufacturer</option>
    <option ng-repeat="inventory in inventories | unique:'manufacturer'" value="{{inventory.manufacturer}}">{{inventory.manufacturer}}</div>
</select> 

EDIT:

Tastypie api, information.

class InventoryResource(ModelResource):
assigned = fields.ForeignKey('bos_inventory.api.AssignedResource', 'assigned', full=True, null=True)
location = fields.ForeignKey('bos_inventory.api.LocationResource', 'location', full=True, null=True)
tags = fields.ToManyField(TagResource, 'tags', full=True, null=True)

class Meta:
    queryset = Inventory.objects.all()
    resource_name = 'inventory'
    list_allowed_methods = ['get', 'put', 'post', 'delete', 'copy']
    detail_allowed_methods = ['get', 'put', 'post', 'delete', 'copy']
    authorization = DjangoAuthorization()
    authorization = Authorization()
    serializer = Serializer()
    filtering = {'id': ALL, 'barcode': ALL, 'model': ALL,  'manufacturer': ALL, 'location': ALL, 'tags': ALL, 'assigned': ALL, 'inventory': ALL}
share|improve this question
    
You have to filter the query on the server. The server needs to interpret the query-parameter and return the filtered result. This has nothing to do with javascript, angularjs, api, restangular –  phylax Apr 10 at 9:29

1 Answer 1

Angular doesn't provide built-in 'unique' filter. You can use angularUI unique filter to solve this problem.

app.js

angular.module("myApp",['ui.utils'])
.controller("myCtrl",function($scope){
  //fake data
  $scope.inventories = [{model:"a model",manufacturer:"a manufacturer"},{model:"a model",manufacturer:"b manufacturer"},{model:"b model",manufacturer:"b manufacturer"},{model:"b model",manufacturer:"a manufacturer"}];

  $scope.filter_all = function(value){
    alert(value);
    //put ur RESTful service call here
  }
});

app.html

<script src="http://angular-ui.github.io/ui-utils/dist/ui-utils.js"></script>
<div ng-controller="myCtrl">
  {{inventory.model}}//{{inventory.manufacturer}}

  <select ng-model="inventory.model" ng-change="filter_all(inventory.model)" ng-options="inventory.model as inventory.model for inventory in inventories | unique: 'model'">
    <!--prompt option-->
    <option value="">Model</option>
  </select>

  <select ng-model="inventory.manufacturer" ng-change="filter_all(inventory.manufacturer)" ng-options="inventory.manufacturer as inventory.manufacturer for inventory in inventories | unique: 'manufacturer'">
    <option value="">Manufacturer</option>
  </select> 
</div>

Please note that instead of use ng-repeat to generate dynamic options, use ng-options when you want the select model to be bound to a non-string value.

Here is a jsFiddle DEMO

share|improve this answer
    
i am not talking about unique here, but how to filter data, sending query to server and receiving only selected information. I have unique directive. –  Erick Apr 11 at 7:38
    
So... what's your api call problem. Can't send GET request to server? Get unexpected server response? Any error on console log? Can you post your Inventory service implementation ? –  Chickenrice Apr 11 at 7:52
    
I don`t get any errors,it just don`t work if i put 2 values or more, it works, if i put one value, it gets selected value. –  Erick Apr 11 at 8:07
    
Can you provide the custom service Inventory implementation? .factory('Inventory',['Restangular'],function(){....} –  Chickenrice Apr 11 at 9:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.