Unknown developer
Unknown developer

Reputation: 5920

Angular filters

I have the following code:

<select name="condition"
    ng-options="operation.id  as operation.description for operation in Operators  | myfilter:criterion_id" > 
</select>

and

module.filter('myfilter', function () {
  return function (x) {
    return  ?????
  };
});

I want to populate my select list with those options whose operation.id equals criterion_id. How may I achieve that?

Upvotes: 0

Views: 53

Answers (3)

Manu Obre
Manu Obre

Reputation: 2294

You should filter your array based on a condition and return the filtered array.

module.filter('myfilter', function () {
  return function (arrayOptions, criterion_id) {
    var result = [];
    result = arrayOptions.filter(function(operation){
        return (operation.id == criterion_id);
    })
    return result;
  };
});

But you should do this in case of a complex filtering process with data manipulation, otherwise you should use filter filter from Angular.

Operators  | filter: {id: criterion_id}

Upvotes: 1

Everton Santos
Everton Santos

Reputation: 162

I've just written some code here.. please check it: http://jsfiddle.net/k86sy0ku/1/

    <select 
        ng-options="p as p.first for p in people |  filter:{id:FilterId}"
        ng-model="selectedPerson"></select>

Upvotes: 0

Ankh
Ankh

Reputation: 5718

You don't need to make your own filter, Angular's default filter will work:

<select name="condition"
    ng-options="operation.id  as operation.description for operation in Operators | filter:{id:criterion_id}" > 
</select>

Upvotes: 2

Related Questions