最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to build a simple table filter with jQuery? - Stack Overflow

programmeradmin3浏览0评论

How can I build a simple table filter with good effect using jQuery? I don't mind about pagination.

list -> select data of database.

I do not want to use a plugin, I prefer the use of short code.

Example:

How can I build a simple table filter with good effect using jQuery? I don't mind about pagination.

list -> select data of database.

I do not want to use a plugin, I prefer the use of short code.

Example:

Share Improve this question edited Aug 13, 2011 at 16:40 Sheikhasa Mozali asked Aug 13, 2011 at 16:34 Sheikhasa MozaliSheikhasa Mozali 2371 gold badge9 silver badges17 bronze badges 4
  • What do you mean with Good Effect? What do you have so far if you don't want plugins? – pimvdb Commented Aug 13, 2011 at 16:35
  • @pimvdb - Effect for hide().I can not use the plugin, because my code the size is high. – Sheikhasa Mozali Commented Aug 13, 2011 at 16:39
  • 2 I see but your question is a little broad. It does not differ much from a 'code this for me' question, which is not what Stack Overflow is for. You might want to show some code to get help for. – pimvdb Commented Aug 13, 2011 at 16:41
  • Example: andymatthews/code/tablefilter – Sheikhasa Mozali Commented Aug 13, 2011 at 16:56
Add a ment  | 

4 Answers 4

Reset to default 5
$('#inputFilter').keyup(function() {
    var that = this;
    $.each($('tr'),
    function(i, val) {
        if ($(val).text().indexOf($(that).val()) == -1) {
            $('tr').eq(i).hide();
        } else {
            $('tr').eq(i).show();
        }
    });
});

CHECH THIS

I don't normally help out with this, but I got bored this morning..

http://jsfiddle/hHJxP/

I know it's kinda late but hope this code helps.

<script>
$(document).ready(function(){
  $("#yourInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#yourTableId tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>

Try testing the innerHTML of the row to the value of the input field, showing / hiding the content depending on the test-result.

$('#test').bind('keyup', function() {
    var s = new RegExp(this.value);
    $('tr').each(function() {
        if(s.test(this.innerHTML)) $(this).show();
        else $(this).hide();
    });
});

JSFIDDLE with example table and input field.

edit

It might be better to use .text() instead of innerHTML. Performancewise innerHTML would be better, but .text() doesn't accept the html-tags as valid search results. JSFIDDLE #2.

发布评论

评论列表(0)

  1. 暂无评论