This JavaScript adds to any HTML table a grid enabling users to perform multi-criteria searches. This feature can be useful with tables containing large quantities of data. |
Over 2000+ free Javascript
at JavaScriptBank.com Website
This script adds to any html table a grid enabling users to perform multi-criteria searches. This feature can be useful with tables containing large quantities of data.
You just need to define the id attribute of a table and insert a piece of javascript code in the head of the html document or in an external ".js" file.
Here you have an example of an html table:
From | Destination | Road Distance (km) | By Air (hrs) | By Car/Coach (hrs) | By Rail (hrs) |
---|---|---|---|---|---|
Sydney | Adelaide | 1412 | 1.4 | 24 | 25.3 |
Sydney | Brisbane | 982 | 1.5 | 17 | 16 |
Sydney | Canberra | 286 | .6 | 4.2 | 4.3 |
Sydney | Melbourne | 872 | 1.1 | 14.3 | 10.5 |
Adelaide | Perth | 2781 | 3.1 | 35 | 38 |
Adelaide | Alice Springs | 1533 | 2 | 20 | 20.25 |
Adelaide | Brisbane | 2045 | 2.15 | 33.3 | 40 |
Below is the same table with a filtering grid generated automatically:
From | Destination | Road Distance (km) | By Air (hrs) | By Car/Coach (hrs) | By Rail (hrs) |
---|---|---|---|---|---|
Sydney | Adelaide | 1412 | 1.4 | 24 | 25.3 |
Sydney | Brisbane | 982 | 1.5 | 17 | 16 |
Sydney | Canberra | 286 | .6 | 4.2 | 4.3 |
Sydney | Melbourne | 872 | 1.1 | 14.3 | 10.5 |
Adelaide | Perth | 2781 | 3.1 | 35 | 38 |
Adelaide | Alice Springs | 1533 | 2 | 20 | 20.25 |
Adelaide | Brisbane | 2045 | 2.15 | 33.3 | 40 |
By adding an id (id="table1") to the table and inserting the script below in the <body> section:
<script language="javascript" type="text/javascript"> setFilterGrid("table1"); </script>
the grid will be generated automatically. The number of filters (<input>) is equal to the number of columns (<td>).
If your document contains several tables (like this page), it is important to define unique ids, otherwise the script will not work properly.
The setFilterGrid("table2",1) function accepts an additional parameter that tells the function which reference row to use in order to generate the right number of filters:
This is the table caption | |||||
From | Destination | Road Distance (km) | By Air (hrs) | By Car/Coach (hrs) | By Rail (hrs) |
Sydney | Adelaide | 1412 | 1.4 | 24 | 25.3 |
Sydney | Brisbane | 982 | 1.5 | 17 | 16 |
Sydney | Canberra | 286 | .6 | 4.2 | 4.3 |
Sydney | Melbourne | 872 | 1.1 | 14.3 | 10.5 |
Adelaide | Perth | 2781 | 3.1 | 35 | 38 |
Adelaide | Alice Springs | 1533 | 2 | 20 | 20.25 |
Adelaide | Brisbane2045 | 2.15 | 33.3 | 40 |
Here we have specified row number 1, that is the second row from the top. The 1st row is number 0. Since the 1st row doesn't contain the right number of columns, we need to use the second parameter of the function in order to calculate the right number of columns and also to define from which row should start the filtering process.
Finally, merged cells (<td colspan="2">) are simply skipped.
We hope you will find this script useful. Feel free to use and change this script, however I will be grateful if you could inform me about any usage or modification.
Copy and paste this code in the <head></head> section of your html document:
and the following code in the <body></body>: