JQuery Table Filter (jQuery 1.7+)

Posted: Saturday March 24th, 2012 in Javascript, jQuery, Project
I’ve created a system to filter the text of a TD within a table and, based in a specific algorythm, show only the desired TRs. It works with the following rules:
  • Given a table with thead and tbody, jQuery Table Filter creates an input below all TDs of theads that automatically filters the content based on the innerHTML of each TD of the tbody
  • The TRs that don’t match with desired filter are hidden and any input inside any TD is disabled (the input is enabled when the TR is visible again)
  • The filter verifies the content splitting each word as it is a different search (“A test” matches with “A – test” and “test A”)
  • The only requirement for the use of this plugin is that it is used ONLY in a table with at least a thead, th and tbody
  • It is created a TR that informs the user when a filter did not return any result. It is positioned below the last TR of the tfoot. If there’s no tfoot, jQuery Table Filter creates it.
Let’s say you’ve got a table with thead and tbody:
<table id="my_table">
	<thead>
		<tr>
			<th>Nome</th>
			<th>Idade</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Steve Sunders</td>
			<td>27</td>
		</tr>
		<tr>
			<td>Clark Kent Sunders</td>
			<td>27</td>
		</tr>
		<tr>
			<td>Bruce Wayne</td>
			<td>35</td>
		</tr>
	</tbody>
</table>
It is possible to use the following parameters:
//===========================================================================================================
$("#my_table").tableFilter({ ignoreCase: true }); //Verifies if jQuery Table Filter should ignore the case
$("#my_table").tableFilter({ delay: 500 }); //delay to apply the filter after typing (if the table has a huge number of content)
$("#my_table").tableFilter({ trClasses: "tr-filter" }); //custom classes that will be set in the TR(s) that will be created to the filters
$("#my_table").tableFilter({ inputClasses: "input-filter" }); //custom classes that will be set in the filter inputs that will be created
$("#my_table").tableFilter({ emptyMessage: "No result" }); //Message when there's no result
$("#my_table").tableFilter({ columns: [0, 3, 4] }); //Index of the columns that will be filtered. If not especified consider all columns.
//===========================================================================================================
Below is a simple example of how you could use it:
<style type="text/css">
input.table-filter-class {
    width: 100%;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tableFilter.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript">
$("#tableFilter_example").tableFilter({
    delay: 0,
    inputClasses: "table-filter-class"
});
</script>
And the result would be:
Name City Country Age
Fagner Brack Montenegro Brazil 21
Steve P. Jobs Los Altos United States of America 56
Richard Markson Surname Brack London England 56

To download it click here
Comments
  1. Bruno Angos says:

    Uma dica, falando por mim, seria legal se colocasse paginas com os exemplos funcionais, assim despertaria um interesse maior do pessoal :D Não sei se conhece o TryIt da w3schools (http://www.w3schools.com/js/tryit.asp?filename=tryjs_events) isso seria legal ate mesmo pro pessoal fazer algumas modificações e testes antes de fazer o download ^^ apenas uma sugestão =D

    • Fagner Martins Brack says:

      Opa, valeu pela sugestão.
      Também tem o http://jsfiddle.net/ ali tem vem dividido o CSS/HTML/Javascript. Também pode-se escolher as bibliotecas que vc pode usar nos testes. Existe a possibilidade de salvar um link e ao acessar vc já abre com o código, só não sei por quanto tempo eles mantém esse registro.

      Mas no caso do tableFilter vc pode baixar o zip no github e acessar a página tests/test.html, depois dos testes automáticos serem realizados vai ficar disponível algumas tabelas de exemplo para testar manualmente

  2. Paulo Ricardo says:

    Parabéns, Fagner.
    Esse site é tudo de bom! pelo seu trabalho.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>