【问题标题】:Simple query table search filter简单查询表搜索过滤器
【发布时间】:2013-01-03 08:46:32
【问题描述】:

打击是表格的基本过滤器搜索。目前它过滤和搜索整个表(thead & tbody)。有没有办法让这个脚本只搜索 tbody?我想从过滤结果中排除thead,我尝试将“id="kwd_search"”附加到标签而不是标签上,但这会使脚本不起作用。另一个我不希望它做的事情是在找到结果时将表格行缩小。

非常感谢您的帮助并感谢您的阅读。 Here is a live demo of what I have。我对这些东西真的很慢,所以如果我能得到一个活生生的例子,如果它不是太麻烦的话,我真的会很好。

// When document is ready: this gets fired before body onload <img src='http://blogs.digitss.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
$(document).ready(function(){
    // Write on keyup event of keyword input element
    $("#kwd_search").keyup(function(){
        // When value of the input is not blank
        if( $(this).val() != "")
        {
            // Show only matching TR, hide rest of them
            $("#my-table tbody>tr").hide();
            $("#my-table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
        }
        else
        {
            // When there is no input or clean again, show everything back
            $("#my-table tbody>tr").show();
        }
    });
});
// jQuery expression for case-insensitive filter
$.extend($.expr[":"], 
{
    "contains-ci": function(elem, i, match, array) 
    {
        return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});

【问题讨论】:

    标签: jquery search filter


    【解决方案1】:

    我不想调试contains-ci 方法。可以换成filter()

    // When document is ready: this gets fired before body onload <img src='http://blogs.digitss.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
    $(document).ready(function() {
         /* cache td elements to improve search performance*/
          var $rows=$("#my-table tbody>tr"), $cells=$rows.children();
        // Write on keyup event of keyword input element
        $("#kwd_search").keyup(function() {
            var term = $(this).val()
            // When value of the input is not blank
            if(term != "") {
                // Show only matching TR, hide rest of them
                $rows.hide();
                $cells.filter(function() {
                    return $(this).text().toLowerCase().indexOf(term) > -1;
                }).parent("tr").show();
            } else {
                // When there is no input or clean again, show everything back
                $rows.show();
            }
        });
    });
    

    演示:http://jsfiddle.net/aPLLF/2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多