bjornmeansbear
bjornmeansbear

Reputation: 98

I can't seem to get list.js to filter items with multiple categories

I'm using list.js to sort a list of items by category. It's easy to make work if each list item only fits in one category, but I am struggling with making this sort correctly if something is assigned more than one category.

This is what I have so far on Codepen.io

Basically, I want to be able to tag things with both beverage AND game. I'm obviously not cycling through the array I've created for each item's categories correctly... So it only ever acknowledges the first item?

There isn't much help in the docs of List.js as to how to use it in this manner. I found an issue the list.js maintainer marked as closed that seemed related, but he basically just told the person to ask for help over here, so that's what I'm trying. https://github.com/javve/list.js/issues/189

Upvotes: 1

Views: 1272

Answers (1)

David Mårtensson
David Mårtensson

Reputation: 7600

I think this is the problem

for (var i=0, j=tryThis.length; i<j; i++) {
  if (tryThis[i] == selection) {
    return true;
  } else {
    return false;
  }
}

If the first category fits, you return true, but if the first does not fit you immediately return false, preventing any more compares.

You should not return false within the loop but only after the loop, when you know that no category was a match.

===== UPDATE

featureList.filter(function(item) {

This call, does it change the list, or does it return the filtered list?

If it returns the filtered list (as is the usual way) you never saves the returned values?

To save values returned from a filter function you usually just assign the return value.

list = list.filter(function(i) { });

If this works in this case depends on the design of the filter function, but try it.

Upvotes: 1

Related Questions