0

I have the following Angular Service:

GetEstateTypes: function () {
  return $http.get('api/estate-types');
}    

Which returns the following:

[
  {"id":1,"name":"House"},
  {"id":2,"name":"Apartment"},
  {"id":3,"name":"Shop"},
  {"id":4,"name":"Warehouse"}
]

How can I create a filter so I can pass a list of Ids to GetEstateTypes and filter the response?

For example, by passing 1 and 2 the response would be filtered and GetEstateTypes would return only the first two rows.

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

3 Answers3

0

Your customFilter would be like below.

app.filter('customFilter', function(){
   return function(values, list){
      var returnValue = [];
      angular.forEach(values, function(val, index){
         if(val.id.indexOf(list))
           returnValue.push(val.id);
      });
      return returnValue;
   }
});

You can use it on html like below

HTML

<div>{{estateTypes | customFilter: selectedItems}}<div>

Or inside controller you could use like below

Controller

$filter('estateTypes')($scope.estateTypes, selectedItems);

In above both solution selectedItems would be and array of ids of selected element like [1,2,3]

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
0

You can create a filter and use it in the controller.

Here's a Plunker

I'm not sure what you're filter will be based on so this just returns the < 2;

app.controller('MainCtrl', function($scope, spotSortFilter) {
...
app.filter('spotSort', function(){
Dylan
  • 4,703
  • 1
  • 20
  • 23
0
 GetEstateTypes: function (value) {
      return $http.get('api/estate-types?id=value');
    } 

This value should be an array and you need to get the values of array in the backend and pick only datas with those ids in the backend and pass the json.

For Example, if php is your backend then

 foreach($_GET['id'] as $value){
      $newArray[] =  //fetch values for that id.
}
json_encode($newArray);

This link will help you more on the array on backend and This will help you for creating array in front end.

Community
  • 1
  • 1
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83