How to build a simple table filter with jQuery?

11,670

Solution 1

$('#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

Solution 2

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

http://jsfiddle.net/hHJxP/

Solution 3

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>
Share:
11,670
Sheikhasa Mozali
Author by

Sheikhasa Mozali

Updated on June 19, 2022

Comments

  • Sheikhasa Mozali
    Sheikhasa Mozali almost 2 years

    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
    Sheikhasa Mozali over 12 years
    please give me example in jsfiddle.net. did this is for all paginations?
  • thecodeparadox
    thecodeparadox over 12 years
    @Sheikhasa Mozali i gave you the fiddle. please check
  • calexandru
    calexandru about 10 years
    why do disappear <th></th> ?
  • Joaquín L. Robles
    Joaquín L. Robles almost 9 years
    to prevent <th> from disappearing use $("tbody tr") as selector.. Also adding toLowerCase() to both strings would result in case insensitive filtering