0

I'm using list.js to do some simple filtering on a list - but running in to a little snag.

Some items in the list may look like this:

    <li class="grid-item" data-attribute="attr1, attr2, attr3" data-attributeTwo="a2">

The filters that you click look like this:

<li data-filter="attr1">Attr1</li>
<li data-filter="attr2">Attr1</li>
<li data-filter="attr3">Attt3</li>

And I have my filter function set up like this:

$('.filter-nav ul li').on('click', function() {

            var filterValue = $( this ).attr('data-filter');

            list.filter(function(item) {
              if (
                item.values().attribute == filterValue ||
                item.values().attributeTwo == filterValue ||
              ) {
                return true;
              } else {
                return false;
              }
            });
            return false;
          });

Is it possible to filter those comma separated attributes by their values respectively? As in, I could click Attr1, Attr2, or Attr3 and the filter would spit out what's appropriate?

1 Answer 1

1

Never mind, I think I got it. I took my filterValue and turned it in to a string (called solutionValue and then checked it against indexOf

so

var filterValue = $( this ).attr('data-filter');
var solutionValue = String(filterValue);

list.filter(function(item) {

              if (
                item.values().solution.indexOf(solutionValue) >= 0
              ) {
                return true;
              } else {
                return false;
              }
            });
            return false;
Sign up to request clarification or add additional context in comments.

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.