YUI Library Home

YUI Library Examples: DataTable Control: Client-side Filtering of Local Data

DataTable Control: Client-side Filtering of Local Data

Find data quickly and easily with a client-side filter.

<< first < prev 12345 next > last >>
201
New Jersey
202
Washington, DC
203
Connecticut
204
Manitoba, Canada
205
Alabama
206
Washington
207
Maine
208
Idaho
209
California
210
Texas
<< first < prev 12345 next > last >>

In this example, a local data set of area codes can be filtered by entering state names (case insensitive). A simple keyup listener on the input element refreshes the DataTable after a slight delay—to allow for more input.

The filtering is accomplished by defining the DataSource's doBeforeCallback hook and replacing the results array in the response.

Sample Code for this Example

Data:

1YAHOO.example.Data = { 
2    areacodes: [ 
3        {areacode: "201", state: "New Jersey"}, 
4        {areacode: "202", state: "Washington, DC"}, 
5        {areacode: "203", state: "Connecticut"}, 
6        ... 
7    ] 
8
view plain | print | ?

CSS:

1/* no custom styles for this example */ 
view plain | print | ?

Markup:

1<div class="markup"
2    <label for="filter">Filter by state:</label> <input type="text" id="filter" value=""
3 
4    <div id="tbl"></div> 
5</div> 
view plain | print | ?

JavaScript:

1YAHOO.util.Event.addListener(window, "load"function() { 
2    var Ex = YAHOO.namespace('example'); 
3 
4    Ex.dataSource = new YAHOO.util.DataSource(YAHOO.example.Data.areacodes,{ 
5        responseType : YAHOO.util.DataSource.TYPE_JSARRAY, 
6        responseSchema : { 
7            fields : ['areacode','state'
8        }, 
9        doBeforeCallback : function (req,raw,res,cb) { 
10            // This is the filter function 
11            var data     = res.results || [], 
12                filtered = [], 
13                i,l; 
14 
15            if (req) { 
16                req = req.toLowerCase(); 
17                for (i = 0, l = data.length; i < l; ++i) { 
18                    if (!data[i].state.toLowerCase().indexOf(req)) { 
19                        filtered.push(data[i]); 
20                    } 
21                } 
22                res.results = filtered; 
23            } 
24 
25            return res; 
26        } 
27    }); 
28 
29    Ex.cols = [ 
30        { key: 'areacode', sortable: true }, 
31        { key: 'state', sortable: true } 
32    ]; 
33 
34    Ex.paginator = new YAHOO.widget.Paginator({ 
35        rowsPerPage   : 10, 
36        pageLinks     : 5 
37    }); 
38 
39    Ex.conf = { 
40        paginator : Ex.paginator, 
41        sortedBy: {key:'areacode', dir:YAHOO.widget.DataTable.CLASS_ASC} 
42    }; 
43 
44    Ex.dataTable = new YAHOO.widget.DataTable('tbl',Ex.cols,Ex.dataSource,Ex.conf); 
45 
46    Ex.filterTimeout = null
47    Ex.updateFilter  = function () { 
48        // Reset timeout 
49        Ex.filterTimeout = null
50 
51        // Reset sort 
52        var state = Ex.dataTable.getState(); 
53            state.sortedBy = {key:'areacode', dir:YAHOO.widget.DataTable.CLASS_ASC}; 
54 
55        // Get filtered data 
56        Ex.dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{ 
57            success : Ex.dataTable.onDataReturnInitializeTable, 
58            failure : Ex.dataTable.onDataReturnInitializeTable, 
59            scope   : Ex.dataTable, 
60            argument: state 
61        }); 
62    }; 
63 
64    YAHOO.util.Event.on('filter','keyup',function (e) { 
65        clearTimeout(Ex.filterTimeout); 
66        setTimeout(Ex.updateFilter,600); 
67    }); 
68}); 
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Logger Console

INFO 130ms (+130) 4:36:53 PM:

LogReader instance0

LogReader initialized

INFO 0ms (+0) 4:36:53 PM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More DataTable Control Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings