Click here to Skip to main content
Click here to Skip to main content

Tagged as

Filter HTML table

, 27 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Select only those rows in table which meet search criteria.

Introduction

In this post I will tell you how you can filter an HTML table using jQuery.  

Using the code

All you need is create an HTML table and a textbox. The search function will be called on the textbox's onkeyup event. Here is the function: 

function Search() {
    var value = $('input[id$="txtSearch"]').val();
    if (value) {
        $('#MyTable tr:not(:first)').each(function () {
            var index = -1;
            $(this).children('td').each(function () {
                var text = $(this).text();
                if (text.toLowerCase().indexOf(value.toLowerCase()) != -1) {
                    index = 0;
                    return false;
                }
            });
            if (index == 0)
                $(this).show();
            else
                $(this).hide();
        });
    }
    else
        $('#MyTable tr').show();
}
<input type="text" onkeyup="Search()" id="txtSearch" /> 

This function checks if the input criteria matches the text of any td, it makes the row visible and if input criteria is not found in whole row, then that row is hidden. If there is no value in input criteria all table rows are displayed. 

This is a very basic code. Any suggestions will be greatly appreciated. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

MairajAhmed
Web Developer F3 Technologies
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
Questionhighly inefficient code PinmemberSeishin#30-Dec-12 23:21 
in your code Search is fired on EVERY keyup event so this means a lot.
you should store jQuery objects in variables initialized outside Search.
Also attribute selector is much slower than id selector.
And you might want to go with the unobtrusive way of attaching js.
 
Why do you select all rows except first? is it the header? if so then maybe you should use thead and tbody sections.
 
if (text.toLowerCase().indexOf(value.toLowerCase()) != -1)
you run this code on every key hit, for every row, for every cell - can you see the problem?
 
var txtSearch = $('input#txtSearch').keyup(Search);
var rows = $('#MyTable tbody tr');
 
function Search(){
   var value = txtSearch.val().toLowerCase();
   if (value) {
      rows.each(function () {
         var index = -1;
         var row = $(this);
         row.children('td').each(function () {
            if ($(this).text().toLowerCase().indexOf(value) != -1) {
               index = 0;
               return false;
            }
         });
         row[index == 0 ? "show" ? "hide"]();
      });
   }else{
      rows.show();
   }
}
<input type="text" id="txtSearch" /> 
 
to decrease the firing rate you could delay the search ex.
var txtSearch = $('input#txtSearch').keyup(DelayedSearch);
 
var delayedSearchTimeout = null;
function DelayedSearch(){
   if(delayedSearchTimeout != null){
      clearTimeout(delayedSearchTimeout);
   }
   delayedSearchTimeout = setTimeout(Search, 500); 
}
 
function Search(){
   delayedSearchTimeout = null;
   ...
}
life is study!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 27 Mar 2013
Article Copyright 2012 by MairajAhmed
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid