This script adds to any html table a "filter by column" feature that enables users to filter and limit the data displayed within a long table. It even works on tables with uneven rows. The script automatically adds a filter grid bar at the top of the desired table. Users can use the following operators to filter columns containing numeric data: <, <=, >, >=.
- Description page at:
mguglielmi.free.fr/scripts/TableFilter/?l=en
- Demo pages at:
mguglielmi.free.fr/scripts/TableFilter/datademo.htm
mguglielmi.free.fr/scripts/TableFilter/datademo2.htm
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 a regular 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 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()
function accepts 2 additional parameters that
will be explained in the next tables. In the example below, by specifing a row
number as a "reference" row, we tell the function which 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 |
setFilterGrid("table2",1);
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 pass the mentioned parameter
in order to calculate the right number of columns and also define from which
row should start the filtering process. Note that merged cells (<td
colspan="2">
) are simply skipped.
By default, the script adds text boxes (<input>
). As you
will see in the next example, you can also decide to not display
a filter or use drop-down lists (<select>
)
instead of text boxes:
This is the table caption | ||||||
From | Destination | Road Distance (km) | By Air (hrs) | By Car/Coach (hrs) | By Rail (hrs) | |
1. | Sydney | Adelaide | 1412 | 1.4 | 24 | 25.3 |
2. | Sydney | Brisbane | 982 | 1.5 | 17 | 16 |
3. | Sydney | Canberra | 286 | .6 | 4.2 | 4.3 |
4. | Sydney | Melbourne | 872 | 1.1 | 14.3 | 10.5 |
5. | Adelaide | Perth | 2781 | 3.1 | 35 | 38 |
6. | Adelaide | Alice Springs | 1533 | 2 | 20 | 20.25 |
7. | Adelaide | Brisbane | 2045 | 2.15 | 33.3 | 40 |
To do that you just need to declare an Array
in which you specify which filters should not be displayed or displayed as drop-down lists:
<script language="javascript" type="text/javascript">
var table3Filters = {
col_0: "none",
col_2: "select",
btn_text: " > "
}
setFilterGrid("table3",1,table3Filters);
</script>
You can name the Array
as you want, but don't forget to add it
to the parameters of the setFilterGrid
() function. It is important
to respect the syntax and naming convention as shown above. There are only 2
values for col_n
: "none"
hides the text box for the designated column and "select"
creates a drop-down list with only 1 occurrence of each cell data. Similarly
to row designation, here the first column is column number 0:
col_0
.
The button "go" in the grid can also be changed. By adding to the
Array
the btn_text
property with
a desired value, you can modify the text of the button. The grid button can
also be hidden by setting the btn
property to
false
:
<script language="javascript" type="text/javascript">
var table3Filters = {
col_0: "none",
col_2: "select",
btn: false
}
setFilterGrid("table3",1,table3Filters);
</script>
In the following table you will find all the elements you can define in the grid:
Property Name | Value | Type | Description | Example |
---|---|---|---|---|
col_0 | "none" | string | hides text box (input) for a desired column | var MyTableFilter = { col_0: "none" } |
col_3 | "select" | string | generates a drop-down list for a desired column | var MyTableFilter = { col_3: "select" } |
btn | true / false | boolean | hides "Go" button in the grid (default - true) | var MyTableFilter = { btn: false } |
btn_text | "my button" | string | changes the text of the button | var MyTableFilter = { btn_text: "Filter" } |
enter_key | true / false | boolean | disables "enter" key (default - true) | var MyTableFilter = { enter_key: false } |
mod_filter_fn | function(){ myFunction(); } | function | calls another function instead of the default function ( Filter('mytable') ) at submission | var MyTableFilter = { mod_filter_fn: function(){ alert('Calls another function!!!'); Filter('table_3'); }} |
display_all_text | "Some text" | string | Sets message for "display all" in drop-down list; (default - empty) | var MyTableFilter = { display_all_text: "Display all towns" } |
on_change | true / false | boolean | If set true, it will filter the table as you change the value of drop-down list (default - false) | var MyTableFilter = { on_change: true } |
display_nrows | true / false | boolean | If set true, it will display the total # of rows displayed at the top of the table in left corner (default - false) | var MyTableFilter = { display_nrows: true } |
nrows_text | "Some text" | string | Sets text of the label for total # of rows displayed (default - "Data rows: ") | var MyTableFilter = { nrows_text: "Total items: " } |
btn_reset | true / false | boolean | If set true, it will show a "Reset" button at the top of the table in right corner (default - false) allowing users to re-initialise the table | var MyTableFilter = { btn_reset: true } |
btn_reset_text | "Some text" | string | Sets text for the "Reset" button (default - "Reset") | var MyTableFilter = { btn_reset_text: "Clear" } |
sort_select | true / false | boolean | If set true, it will sort options in the drop-down list(s) (default - false) | var MyTableFilter = { sort_select: true } |
paging | true / false | boolean | If set true, it will generate a paging feature | var MyTableFilter = { paging: true } |
paging_length | 25 | number | Sets # of rows displayed in a page (default - 10) | var MyTableFilter = { paging_length: 50 } |
loader | true / false | boolean | If set true, it will display a "loading" message (default - false) | var MyTableFilter = { loader: true } |
loader_text | "Some text" | string | Sets text for "loading" message (default - "Loading...") | var MyTableFilter = { loader_text: "Filtering data..." } |
exact_match | true / false | boolean | If set true, only exact matches will be displayed (default - false) | var MyTableFilter = { exact_match: true } |
I 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>
: