0

I'm creating an application where you can filter items by clicking on category buttons. Most items have multiple categories. When pressing on the category buttons, I create an array with the pressed categories.

Each item to be filtered has an array for categories it belongs to. Example:

Item 1 - categories: animal, brown, tail

Item 2 - categories: animal, white

Item 3 - categories: human, tall

When I press on the categories buttons "animal", it shows only items 1 and 2, then when I press on brown, it shows only Item 1. When I deselect all items and press in this order - animal, tail. It doesn't show anything :/

I've looked trough some of the similar topics, but haven't found a solid way of doing this.

Is it possible at all to do it without creating custom filter?

3
  • Show us your code, what have you tried so far? Commented Aug 26, 2015 at 13:23
  • 1
    Is there a reason that you don't want a custom filter? This seems like exactly the case for one, and it would be pretty trivial to create. Commented Aug 26, 2015 at 13:24
  • check out this : fdietz.github.io/recipes-with-angular-js/… Commented Aug 26, 2015 at 13:30

1 Answer 1

1

In case you change your mind about adding a "custom filter", here's one that should work.

angular.module('myApp').filter('matchesAll', function() {
    return function(items, relevant, property) {
        if(!(relevant && relevant.length)){
            return items; // No categories to compare with, return everything
        }

        property = property || 'categories'; // By default look at categories

        return items.filter(function(item) {
            var itemProps = item[property];
            return relevant.every(function(relevantCategory){
                return itemProps.indexOf(relevantCategory) !== -1;
            });
        });
    };
});

This would be used like any ngRepeat filter:

ng-repeat="item in items | matchesAll:selectedCategories"

JSFiddle demonstrating it: http://jsfiddle.net/3an5wr0b/

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

10 Comments

It seems to throw an error: "TypeError: Cannot read property 'length' of undefined". Closer insepction shows the error here: "if(!relevant && relevant.length){" Also to note, I'm using AngularJS v1.2.9
Sorry, I introduced that error when I lengthened the variable prop to property to make the code clearer. I missed one spot.
Hmm, doesn't seem to help, even though I fixed the typo. It's as if it cannot get the value for "relevant".
When i do, "return function(items, relevant, property) {console.log(relevant);..." it outputs undefined.
I think you'll find that if you follow the JSFiddle, the example of items (A,B,C) , (B,C), and (A) will match (A,B,C) for selection (A,C). Which is exactly as you just described.
|

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.