0

I'm working on a filtering solution for a grid. The filter contains multiple checkboxes in a panel. I currently have a working solution that uses the filter built into the ng-repeat directive. However, I haven't found a way to make it filter a single field with multiple parameters. This is what I have:

HTML:

<tr data-ng-repeat="rule in rules | orderBy:sortOrder | filter:filterOrder">

Contoller:

var defaultFilter = {critical:'', score:'', category:'', pass:'false'};
$scope.filterOrder = defaultFilter;

What I would like is to be able to do something like this:

var defaultFilter = {critical:'', score:'', category:'', pass:'false && notRun'};

I'm not sure if this is possible. I've looked for the syntax on it, but I have yet to find it. I know there are ways to put it in the controller, but due to our current project this implementation would be considerably easier.

2
  • Can you give a more complete example? Like in a jsFiddle? I'm having trouble understanding what you are trying to do, or more specifically, what isn't working. Commented Aug 31, 2013 at 19:49
  • @BrianGenisio - I can't go into too much detail due to the nature of the application, but the gist is that I'm using ng-repeat to iterate over a dataset and populate a grid. I've got a filter attached to that grid which will then populate the filterOrder variable you see set in the example. It works as long as only one "filter" is set per item (i.e. - critical: "true"), however, as soon as I try to put more than one parameter as a filter it doesn't work (i.e. - critical: "true" && "false" ). I'm not sure if this is possible in the ng-repeat directive, or if I just have the syntax wrong. Commented Aug 31, 2013 at 20:07

2 Answers 2

3

Assuming you are using the 1.0.x branch, the relevant part of the source code for the filtering on objects is shown in the footnote below. This shows that the standard filter finds the key to test, and then gets var text = (''+expression[key]).toLowerCase(); from your search criteria before calling search. search then does a text search using (indexOf(text) > -1.

Therefore you can't set one of the items in your search criteria to an expression that requires evaluation in the way that you are trying.

Your only other options are to:

  1. use a predicate function as the parameter for the filter (this just needs to return true/false); or
  2. to write your own filter (this just needs to return a new array from the original one).

Predicate function

$scope.myFilterTest(item) { return ... true or false test result ...; }

and:

<tr data-ng-repeat="rule in rules | orderBy:sortOrder | filter:myFilterTest">

custom filter

var someApp=angular.module('myApp', []);

someApp.filter('complexFilter', function() {
  return function(input, criteria) {
    var result = [];
    for (var i = 0; i < input.length; i++) {
      if(... multiple tests ...){
        result.push(input[i]);
      }
    }
    return result;
  }
});

And then:

<tr data-ng-repeat="rule in rules | orderBy:sortOrder | complexFilter:filterOrder">

Footnote:

relevant part of src/ng/filter/filter.js

  case "object":
    for (var key in expression) {
      if (key == '$') {
        (function() {
          var text = (''+expression[key]).toLowerCase();
          if (!text) return;
          predicates.push(function(value) {
            return search(value, text);
          });
        })();
      } else {
        (function() {
          var path = key;
          var text = (''+expression[key]).toLowerCase();
          if (!text) return;
          predicates.push(function(value) {
            return search(getter(value, path), text);
          });
        })();
      }
    }
    break;
Sign up to request clarification or add additional context in comments.

1 Comment

That's the answer I needed, but wasn't the one I was hoping for. I'll get cracking on the solution now. Thanks for the quick turn around on the question.
0

yes you can achieve this using custom filter, here is the answer for you question.

And fiddle link here

filterMultiple

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.