2017-05-30 421 views
1

我想在我的treetable中實現搜索過濾器。我正在使用JQuery treetable庫,這是我迄今爲止所做的,但它似乎並沒有工作。任何人有任何想法爲什麼?jQuery中的搜索過濾器treetable

這是我的HTML:

<table id="example"> 
    <tbody> 
    <thead> 
     <tr> 
     <th>Tree data</th> 
     </tr> 
     <input class="search" placeholder="Search" /> 
     <p class="log"></p> 
    </thead> 
</tbody> 

這是我的搜索過濾器實現的功能:

var $rows = $('#example tr').treetable({expandable: true}); 
$('#search').keyup(function() { 
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 

    $rows.show().filter(function() { 
    var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
    return !~text.indexOf(val); 
    }).hide(); 
}); 

回答

0

第一,它應該是$('.search').keyup ...而不是$('#search').keyup...

第二,我認爲,更好的辦法是:

var $rows = $('#tree_table tbody tr').treetable({expandable: true}); 
$('.search').keyup(function() { 
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 
    $rows.show().filter(function() { 
     var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
     return !~text.indexOf(val); 
    }).hide(); 
}); 

這樣,頭部始終保持。