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.

3 Answers 3

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(){
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.