4

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: Table Filter With JQuery

Sheikhasa Mozali
  • 237
  • 1
  • 9
  • 16

4 Answers4

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

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
3

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

http://jsfiddle.net/hHJxP/

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
2

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>
davinceleecode
  • 777
  • 3
  • 10
  • 31
0

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.

  • if there is `pagination` this work for all dataes or only for same page of `pagination`?(i want work for `all page in pagination`) this is ok? – Sheikhasa Mozali Aug 13 '11 at 17:04
  • This will only work for the current table page. So if you're on page 5, only results on page 5 will be found and filtered. –  Aug 13 '11 at 17:06