Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been looking for severeal days how to create a dynamic filter with multiple checkboxes in AngularJS easily and none of the solutions I found satisfied me.

Here is an example of a filter that will display everything when the checkboxes are unchecked and filter the data when checkboxes are checked. (Good for search engine to look for a hotel with wifi, balcony... for example)

Filter :

app.filter('conveniences_filter', function() {
    return function( items, types) {
        var filtered = [];
        var displayItem;
        angular.forEach(items, function(item) {
            displayItem = true;
            angular.forEach(types, function(type, key) {         
                if(type == true && item[key] == false) {
                   displayItem = false;
                }
            });
            if(displayItem == true) {
                filtered.push(item);   
            }
        });
        return filtered;
    };
});

In your controller :

    $scope.types = {wifi: false, balcony: false}

In your HTML

ng-repeat="property in properties | conveniences_filter:types"

Important : Your keys in your $scope.types have to be the same as your key in your properties items or it's not gonna work.

Example of a property here :

Object { name: property, wifi: false, balcony: true }

I hope it's gonna be useful :)

share|improve this question

1 Answer 1

I think it's not 100% clear what your requirements are here. At least from what I get from the code you've written is that you'd like to filter a list of objects based on their property values and that you use a "schema" object that is used to compare against the objects.

To fill the gaps in the requirements I'm assuming you want to validate against an "schema" object that contains properties that also need to match in your items. I'm also assuming that if a property is not in the "schema" object then it will not be relevant for the comparison. Also I assumed that you want all present properties in the "schema" to be valid / matching otherwise the item is not considered.

I've put together a small example solving the above requirements. In general you should use the array extra functions that come with ECMA5.1 and make things much simpler and functional style.

Example: http://jsbin.com/velobi/1/edit

Cheers Gion

share|improve this answer
    
I was just showing a way to do it. But thank you for your answer I'm a begginer with AngularJS and it's good to see other and better way to do it. The difference is that with the solution I use, the filter is working only when checkboxes are checked. For example without any filters, you can see all the properties even them without wifi. And when you check the wifi checkbox, the filter hide the properties without wifi. Which can be useful sometimes –  Maxime Plancke Jul 23 '14 at 12:43
    
Hi, thats cool i think it's always nice to see other approaches. Not necessarily one being better than the other one. You could still implement your approach in my solution but need to be aware that in the properties object CHECKED means key: true and UNCKECKED means that the property should not be present. key: false would mean that the property should be false explicitly in the item. –  gkunz Jul 23 '14 at 14:25

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.