html - Table filtering/searching with javascript/jquery based on tags -


i have table each row includes list of tags. able search these tags , narrow table down rows contain specific tag/s. example, if type in search box 2tag1 tagg 2 rows contain these tags.

my table looks this:

the table

my table html looks this:

<table class="table table-hover" id="tbl2">             <thead>                 <tr>                     <th class="span1">merktu við spurningar</th>                     <th class="span4"><strong>spurning</strong></th>                     <th class="span4"><strong>vægi spurningar</strong></th>                     <th class="span4">tegund spurningar</th>                     <th class="span2">búin til</th>                     <th class="span4">tags</th>                 </tr>             </thead>             <tbody id="tbl2body">                 @for(question <- questions){                     <tr id="row@question.spurningar_id">                      <td><input type="checkbox" name="spurningar_id" id="@{question.spurningar_id}" onclick="enable(@question.spurningar_id)" value="@{question.spurningar_id}"></td>                     <td>@{question.texti}</td>                     <td><input type="number" min="0" max="100" value="0" size="6" name="vaegi" disabled="true" id="s@{question.spurningar_id}"></td>                     <td><p>hehehe</p></td>                     <td><strong>@{question.dagurbuidtil}</strong></td>                     <td><ul id="tags"">                         @for(tag <- question.tags){                             <li class="well well-small">                                 @tag.leitarskilyrdi                             </li>                         }                     </ul></td>                     </tr>}             </tbody>         </table> 

i have tried following jquery, of course searches whole row, not tags. if select #tags li instead of #tbl2body tr, able search tags, rows not dissapear.

how can solve problem?

    $(document).ready(function() {     $('li strong').click(function(e) {         $(this).parent().find('em').slidetoggle();     });      $('#search').on("input", function(e) {         var words = $(this).val().tolowercase().match(/\s+/g);         if (!words) {             $('#tbl2body tr').show();         } else {             $('#tbl2body tr').each(function() {                 var text = $(this).text().tolowercase();                 var show = words.some(function(word) {                     return ~text.indexof(word);                 });                 $(this).toggle(show);             });         }     });  }); // end document ready 

you can't repeat id in page, instead of <ul id="tags"> use class instead <ul class="tags">.

now search text within tags ul

$('#tbl2body tr').each(function() {             var text = $(this).find('ul.tags').text().tolowercase();             var show = words.some(function(word) {                 return ~text.indexof(word);             });             $(this).toggle(show);         }); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -