YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

Filter by state:

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:

YAHOO.example.Data = { areacodes: [ {areacode: "201", state: "New Jersey"}, {areacode: "202", state: "Washington, DC"}, {areacode: "203", state: "Connecticut"}, ... ] }

CSS:

/* no custom styles for this example */

Markup:

<div class="markup"> <label for="filter">Filter by state:</label> <input type="text" id="filter" value=""> <div id="tbl"></div> </div>

JavaScript:

YAHOO.util.Event.addListener(window, "load", function() { var Ex = YAHOO.namespace('example'); Ex.dataSource = new YAHOO.util.DataSource(YAHOO.example.Data.areacodes,{ responseType : YAHOO.util.DataSource.TYPE_JSARRAY, responseSchema : { fields : ['areacode','state'] }, doBeforeCallback : function (req,raw,res,cb) { // This is the filter function var data = res.results || [], filtered = [], i,l; if (req) { req = req.toLowerCase(); for (i = 0, l = data.length; i < l; ++i) { if (!data[i].state.toLowerCase().indexOf(req)) { filtered.push(data[i]); } } res.results = filtered; } return res; } }); Ex.cols = [ { key: 'areacode', sortable: true }, { key: 'state', sortable: true } ]; Ex.paginator = new YAHOO.widget.Paginator({ rowsPerPage : 10, pageLinks : 5 }); Ex.conf = { paginator : Ex.paginator, sortedBy: {key:'areacode', dir:YAHOO.widget.DataTable.CLASS_ASC} }; Ex.dataTable = new YAHOO.widget.DataTable('tbl',Ex.cols,Ex.dataSource,Ex.conf); Ex.filterTimeout = null; Ex.updateFilter = function () { // Reset timeout Ex.filterTimeout = null; // Reset sort var state = Ex.dataTable.getState(); state.sortedBy = {key:'areacode', dir:YAHOO.widget.DataTable.CLASS_ASC}; // Get filtered data Ex.dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{ success : Ex.dataTable.onDataReturnInitializeTable, failure : Ex.dataTable.onDataReturnInitializeTable, scope : Ex.dataTable, argument: state }); }; YAHOO.util.Event.on('filter','keyup',function (e) { clearTimeout(Ex.filterTimeout); setTimeout(Ex.updateFilter,600); }); });

## 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:

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

Reload with logging
and debugging disabled.

### DataTable Control Examples:

### More DataTable Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

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