Click here to Skip to main content
15,890,381 members
Articles / Web Development / HTML
Article

HTML Table Filter ala Excel

Rate me:
Please Sign up or sign in to vote.
4.75/5 (37 votes)
3 Aug 2005CPOL1 min read 223.9K   2.3K   56   71
A way to filter a large table in a simple manner.

Demo

<script language="javascript"></script>

<input id="chk" önclick="enableFilter();" type="checkbox" name="chk" />Enable Filter

Col1Col2
1string
1string2
2String
2STRING2
3
1string
1string2
2String
2STRING2
3

Introduction

When I was writing a web application, I needed a way to filter a large table in a simple manner. The users of the application are used to the auto filtering in MS Excel, so I wanted my filter to have the same look and feel.

Features

  • Once a column is filtered, the dropdown boxes for the rest of the columns are adapted to the changes. Just like the auto filter in Excel works!
  • If a row has the class noFilter, the row will not be touched by the filter.

Using the code

Using the code is very simple. Just include the script to the page:

HTML
<script language="javascript" src="tablefilter.js"></script>

And activate the filter in the OnLoad:

HTML
<body onload="attachFilter(document.getElementById('tbl'), 1)">

attachFilter expects two parameters:

  1. The table object to attach the filter to.
  2. The row number at which the filter row should be inserted.

Browser Compatibility

The script was initially written for IE. I did test it successfully on Firefox, but I do not know if the script works for Netscape browsers.

History

  • 2005/07/27: First version.

License

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


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFilter returns the whole code plus value. Pin
chunchuyo8-Jul-10 5:26
chunchuyo8-Jul-10 5:26 
Generaltablefilter and paging Pin
braveheart0131-May-10 1:28
braveheart0131-May-10 1:28 
GeneralRe: tablefilter and paging Pin
WoutL31-May-10 1:51
WoutL31-May-10 1:51 
QuestionHello ! Pin
ehuddrey11-Nov-09 10:19
ehuddrey11-Nov-09 10:19 
AnswerRe: Hello ! Pin
WoutL11-Nov-09 20:47
WoutL11-Nov-09 20:47 
QuestionCan the dropdown filter be restricted to a single column? Pin
klsblues1-Jul-09 4:20
klsblues1-Jul-09 4:20 
AnswerRe: Can the dropdown filter be restricted to a single column? Pin
WoutL12-Jul-09 22:11
WoutL12-Jul-09 22:11 
GeneralRe: Can the dropdown filter be restricted to a single column? Pin
Member 1247297620-Apr-16 3:57
Member 1247297620-Apr-16 3:57 
QuestionHow to make the filtering work for the dropdown list column Pin
vijay_kumar16-Dec-08 0:32
vijay_kumar16-Dec-08 0:32 
AnswerRe: How to make the filtering work for the dropdown list column Pin
WoutL16-Dec-08 1:12
WoutL16-Dec-08 1:12 
Generalupdated JS for CUSTOM Filtering Pin
Dhruv P Patel7-May-08 5:56
Dhruv P Patel7-May-08 5:56 
GeneralRe: updated JS for CUSTOM Filtering (More Info) Pin
Dhruv P Patel7-May-08 5:59
Dhruv P Patel7-May-08 5:59 
Generalupdated JS for table filtering with CUSTOM Pin
Dhruv P Patel7-May-08 5:53
Dhruv P Patel7-May-08 5:53 
Generalnew feature Pin
Dhruv P Patel6-May-08 21:05
Dhruv P Patel6-May-08 21:05 
GeneralRe: new feature Pin
WoutL6-May-08 21:20
WoutL6-May-08 21:20 
QuestionNewbie Help Pin
hgee5-Nov-07 8:54
hgee5-Nov-07 8:54 
AnswerRe: Newbie Help Pin
WoutL5-Nov-07 23:18
WoutL5-Nov-07 23:18 
Generalnice code, but could it do this... :) Pin
andrewmeda6231-Aug-07 11:33
andrewmeda6231-Aug-07 11:33 
GeneralRe: nice code, but could it do this... :) Pin
WoutL1-Aug-07 20:12
WoutL1-Aug-07 20:12 
GeneralStill Working Great. Pin
belesland19-Jul-07 2:34
belesland19-Jul-07 2:34 
QuestionTable size not changing? Pin
arniedows16-May-07 20:46
arniedows16-May-07 20:46 
AnswerRe: Table size not changing? Pin
WoutL16-May-07 21:56
WoutL16-May-07 21:56 
Questionhyperlinks Pin
arizen15-Jan-07 1:14
arizen15-Jan-07 1:14 
AnswerRe: hyperlinks Pin
WoutL15-Jan-07 1:26
WoutL15-Jan-07 1:26 
QuestionRe: hyperlinks Pin
arizen15-Jan-07 1:37
arizen15-Jan-07 1:37 

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

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