Find data quickly and easily with a client-side filter.
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.
Data:
1 | YAHOO.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:
1 | YAHOO.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 | ? |
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.
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.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings