Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to filter my data with multiple checkboxes. One variable can have multiple filters.

Here is the link

So for example when 4 and 5 are checked, all 4s and 5s are shwon.

html:

<div class="btn-group" data-toggle="buttons-checkbox">
    <pre>{{checkboxes}}</pre>
    <companies ng-repeat="check in checkboxes">
      <button type="button" class="btn"  ng-model="check.truth" 
      btn-checkbox-true="check.value" btn-checkbox>{{check.value}}</button>
    </companies>
</div>

and js:

$scope.checkboxes = [{"value":"1","truth":false},{"value":"2","truth":false}, {"value":"3","truth":false},{"value":"4","truth":false}];


$scope.data = [{"name":"Some Name","value":"1"},{"name":"Another Name","value":"2"},{"name":"Cool Name","value":"3"},{"name":"Funky Name","value":"4"}]
share|improve this question
up vote 0 down vote accepted

You can create a custom filter

<tr ng-repeat="d in data | myfilter:checkboxes">

app.filter('myfilter', function () {
    return function (data, values) {
        var vs = [];
        angular.forEach(values, function (item) {
            if ( !! item.truth) {
                vs.push(item.value);
            }
        });

        if (vs.length === 0) return data;

        var result = [];
        angular.forEach(data, function (item) {
            if (vs.indexOf(item.value) >= 0) {
                result.push(item);
            }
        });
        return result;
    }
});

Working Demo

share|improve this answer
    
How would that work for strings (eg. the names column)? Why doesnt if(vs == item.value){result.push(item); work? – scr- Sep 5 '13 at 4:29
    
@scr- vs is an array, vs.indexOf(item.value) >= 0 checks whether the array contains the string. – zsong Sep 5 '13 at 4:31
    
Cant get it to work for the names column: plnkr.co/edit/TBXyBiYta84fyqNu4wJW?p=preview – scr- Sep 5 '13 at 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.