I’m so happy with myself, I want to share some nice and easy code that can search and filter in a table.
Simple, nothing to add in your html but for the javascript file and a simple filterable class to set onto tables and to define with a color (or any other style you want).
<style>
.highlighted { background: yellow; }
</style>
<script type="text/javascript"
src="attachment/file/filterTable/filterTable.js"></script>
<table class="filterable">
<thead>
<tr>
<th></th>
<th>Title 1</th>
<th>Title 2</th>
<th>Title 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
<td>Data again</td>
<td>Data yet other</td>
<td>Data still</td>
</tr>
</tbody>
</table>
| Country | Current US$ | 1990 US$ |
|---|---|---|
| United States | $18.5billion | $5.4bn |
| Russia | $4.6billion | $6.2bn |
| France | $4.4bn | $2.1bn |
| UK | $1.9bn | $985million |
| Germany | $900m | $1.1bn |
| Canada | $900m | $543m |
| China | $700m | $125m |
| Israel | $500m | $283m |
See also here for some of the original code.
Here is the javascript file:
+2
New discussion
Answer
-1
-1
-1
Hey Echarp,
this is a really cool tool.
But I have a problem to transfer it to a different page. Even if I save your page to my desktop with the .js-file (path is adjusted) it doesnt work.
The example from “vonloesch” works, but I want the highlighting thing.
Can somebody be so kind a post or send me the naked .html-file and the .js-file? You would really do me a favor!
Thanks in advance and kind regards!
Johannes
+2
New discussion
Answer
I’ll gladly send it to you, to what email address?
Normally just clicking on the following link should open the javascript file for you: http://blog.echarp.org/attachment/file/filterTable/filterTable.js
Then save it locally and move this file into whatever directory you use for your javascript files.
Once this is done, you can test the feature with the html code present in this article: it include the js file in the html file, and uses it through the addition of class “filterable” to the html table itself. The highlight is managed through a bit of css.
You can also contact me on irc, server irc.freenode.net, I’m generally on #parlement.
+1 Thanks for your feedback on this!
+1
New discussion
Answer
Hi Echarp
i like the filtertable js and have used it in one of my apps, but find it to slow when filtering a large table, is it possible to change to a click serach button or hit enter so it doesnt filter live
simpleinvoices.sf.net/demo
Cheers
Justin Kelly
justin@kelly.org.au
+1
New discussion
Answer
-1
+2
New discussion
Answer
You could try removing some of the features.
For example the script is checking and highlighting individual words, can be removed easily (see the “dehighlight” and highlight" methods).
Already this should give you more reactivity over a large table.
+1
+0
New discussion
Answer
-1
+1
New discussion
Answer
It’s definitely something you can do, yes
This implies some modifications to the javascript, so that instead of filtering one table, it would filter all tables which are contained in a specific part of the page.
For example:
document.getElementById('toFilter');
And wherever there is a loop over the table’s rows, you should add an extra loop around it, to loop over all sub tables.
I’ll gladly help you if you want to try it.
+1
New discussion
Answer
So would that in effect get the filter function to repeat the process over a number of table references? Would ‘toFilter’ have to be classified as a collection of table Id’s? Sorry I’m very much a beginner when it comes to this, trying to learn though.
Do you think something along the lines below would work, please be as critical as you can. Thankyou so much for your help.
function filter (phrase, id1,id2,id3){ var words = phrase.value.toLowerCase().split(" "); var table = document.getElementById(id1); var ele; for (var r = 1; r < table.rows.length; r++){ ele = table.rows®.innerHTML.replace(/<[^>]>/g,""); var displayStyle = ‘none’; for (var i = 0; i < words.length; i+) { if (ele.toLowerCase().indexOf(words[i])>=0) displayStyle = ’’;
var words = phrase.value.toLowerCase().split(" ");var table = document.getElementById(_id2); var ele; for (var r = 1; r < table.rows.length; r++){ ele = table.rows®.innerHTML.replace(/<[^>]>/g,""); var displayStyle = ‘none’; for (var i = 0; i < words.length; i+) { if (ele.toLowerCase().indexOf(words[i])>=0) displayStyle = ’’;
var words = phrase.value.toLowerCase().split(" ");var table = document.getElementById(_id3); var ele; for (var r = 1; r < table.rows.length; r++){ ele = table.rows®.innerHTML.replace(/<[^>]>/g,""); var displayStyle = ‘none’; for (var i = 0; i < words.length; i+) { if (ele.toLowerCase().indexOf(words[i])>=0) displayStyle = ’’;
else { displayStyle = ‘none’; break; } } table.rows®.style.display = displayStyle; } }
+1
New discussion
Answer
I’m not sure about your goal, you want “one” filter to filter over many tables? Input filter should be present above the first one then?
First you can “only” add the filter form if you are in the first table:
<pre>
if (t == 0 && element.attributes[‘class’] && …) { … }
(at the end of the javascript file)
Then in the filterTable(input, element); part, reloop over all tables to add each to the onkeyup event.
Something like:
<pre>
It’s just one way to do it, simple. You tell me if it’s good.
+1
New discussion
Answer
Sorry it tried to compile the form at the end of the script,
I had tried to use the same basic version of your form with the form calling the function filter(this,‘Id1’,‘Id2’,‘Id3’,1)
+1
New discussion
Answer
+2
New discussion
Answer
Hi Echarp
i like the filtertable js and have used it in one of my apps, but find it to slow when filtering a large table, is it possible to change to a ‘click serach button or hit enter’ so it doesnt filter live
simpleinvoices.sf.net/demo
Cheers
Justin Kelly
justin@kelly.org.au
+1
New discussion
Answer
It’s a pleasure to create something that can be useful!!!
To improve it on a large table, yes, you would need to move the filtering code toward a “submit” button.
Just modify the javascript, something like:
var form = document.createElement(‘form’); form.setAttribute(‘class’, ‘filter’); __ For ie… form.attributes[‘class’].value = ‘filter’; var input = document.createElement(‘input’); form.appendChild(input); var input2 = document.createElement(‘input’); input2.setAttribute(‘type’, ‘submit’); input2.attributes[‘type’] = ‘submit’; input2.onkeyup = function() { filterTable(input, element); } form.appendChild(input2); element.parentNode.insertBefore(form, element);Have fun.
+1
New discussion
Answer
+0
-1
+0
-1
-1
-1
+0
+0
-1
New discussion
Answer
-1
+0
-1
New discussion
Answer
-1
Hi Echarp
i like the filtertable js and have used it in one of my apps, but find it to slow when filtering a large table, is it possible to change to a click serach button or hit enter so it doesnt filter live
simpleinvoices.sf.net/demo
Cheers
Justin Kelly
justin@kelly.org.au
+1
New discussion
Answer
-1
-1
-1