简体   繁体   中英

JQuery - Filter nested list based on textbox input

I have a nested list of checkboxes which I need to filter them down based on textbox input compared to the label's value. The list looks like this but a lot larger:

在此处输入图片说明

Fiddle: https://jsfiddle.net/y9qqgjp5/

However, I'm having trouble with the nested part of this. As I have it now, if you go to search a user (2nd level item), the parent becomes hidden because it doesn't match and thus makes the children hidden, as well. It works fine with filtering on a parent.

Here's the simplified structure I'm filtering on.

HTML:

<input type="text" id="filter" />
<span class="checkbox-list">
<ul>
    <li>
        <label>Item1</label>
        <ul>
            <li>
                <label>Item2</label>
            </li>
            <li>
                <label>Item3</label>
            </li>
            <li>
                <label>Item4</label>
            </li>
            <li>
                <label>Item5</label>
            </li>
        </ul>
    </li>
    <li>
        <label>Item6</label>
    </li>
    <li>
        <label>Item7</label>
        <ul>
            <li>
                <label>Item4</label>
            </li>
            <li>
                <label>Item8</label>
            </li>
        </ul>
    </li>
</ul>
</span>

JQuery:

$('#filter').keyup(function() {
  var valThis = $(this).val().toLowerCase();
  if (valThis == "") {
    $('.checkbox-list > ul > li > label').parent().show();
  } else {
    $('.checkbox-list > ul > li > label').each(function() {
      var text = $(this).text().toLowerCase();
      (text.indexOf(valThis) >= 0) ? $(this).parent().show(): $(this).parent().hide();
    });
  };

  if (valThis == "") {
    $('.checkbox-list > ul > li > ul > li > label').parent().show();
  } else {
    $('.checkbox-list > ul > li > > ul > li > label').each(function() {
      var text = $(this).text().toLowerCase();
      (text.indexOf(valThis) >= 0) ? $(this).parent().show(): $(this).parent().hide();
    });
  };
});

Any help is appreciated. I can't wrap my head around the nested part of this.

Try using parents()

 var labels = $('label'); // cache this for better performance $('#filter').keyup(function() { var valThis = $(this).val().toLowerCase(); if (valThis == "") { labels.parent().show(); // show all lis } else { labels.each(function() { var label = $(this); // cache this var text = label.text().toLowerCase(); if (text.indexOf(valThis) > -1) { label.parents('li').show() // show all li parents up the ancestor tree } else { label.parent().hide(); // hide current li as it doesn't match } }); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="filter" /> <span class="checkbox-list"> <ul> <li> <label>Item 1</label> <ul> <li> <label>Item 12</label> </li> <li> <label>Item3</label> </li> <li> <label>Item4</label> </li> <li> <label>Item5</label> </li> </ul> </li> <li> <label>Item6</label> </li> <li> <label>Item7</label> <ul> <li> <label>Item4</label> </li> <li> <label>Item8</label> </li> </ul> </li> </ul> </span> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM