-1

I have a list of checkboxs:

 <input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value1'" ng-false-value="''"> value1<br>
 <input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value2'" ng-false-value="''"> value2<br>

.... .... And I want to filter the checked checkboxes fields like this:

x in X | filter: { 'filterField': ['value1','value2']}

Any solution to push into my array filterField and filter more than one value? Thanks!

5
  • Have you considered creating your own filter? Commented Sep 20, 2016 at 10:48
  • I Will need more filterFields and probably this filterFields Will Be also arrays for more than one value, how can i do that?? Commented Sep 20, 2016 at 10:53
  • This might help stackoverflow.com/questions/16563018/… Commented Sep 20, 2016 at 10:55
  • mmm...not so much xd maybe i dont understand, im an angular ignorant Commented Sep 20, 2016 at 11:12
  • I'll provide an example Commented Sep 20, 2016 at 11:14

3 Answers 3

0

From AngularJS : Custom filters and ng-repeat

Checking multiple fields

// Filter, where element would be your x
$scope.filterFn = function(element)
{ 
    if(element.field === 'value1' || element.anotherField === 'value2')
    {
        return true; // this will be listed in the results
    }
    return false; // otherwise it won't be within the results
};

And then apply it
x in X | filter:filterFn

Sign up to request clarification or add additional context in comments.

Comments

0

Another way would be to use one custom filter and add parameters to it. This fiddle shows how to work with a filter with multiple parameters: http://jsfiddle.net/halirgb/Lvc0u55v/

Like this

myApp.filter('customFilter', function() { // Name of filter
return function(first, second,third) {
// write filter code here
  return second;// shows the last filter parameter
};});

And use the filter like this:

  <div ng-repeat="x in X">  {{x|customFilter:myFilter.filterField}} </div>

Comments

0

It is better to use custom filters in that case

$scope.filterField = function(x) {
    return x.filterField == 'value1' || x.filterField == 'value2' || x.anotherField == 'value';
};


x in X | filter: filterField

2 Comments

but if i want more filter fields i need to create one function for each field ???
you can use like this x.filterField == 'value1' || x.field1 == 'value' || x.field2 == 'value1' @Charly

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.