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: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### DataTable Control Examples:

- Basic Example
- Progressive Enhancement
- Custom Cell Formatting
- Conditional row coloring
- Nested Headers
- JSON Data Over XHR
- XML Data Over XHR With POST
- XML Data with XPath
- Textual Data Over XHR
- Polling the DataSource
- Adding, Updating, and Deleting Rows
- Client-side Pagination
- Client-side Sorting
- Server-side Pagination and Sorting for Dynamic Data
- Integrating Browser History Manager with Server-side Pagination and Sorting
- XY-scrolling, Y-scrolling, and X-scrolling
- Row Selection
- Cell Selection
- Inline Cell Editing
- Showing, Hiding, and Reordering Columns.
- Highlighting Cells, Rows, or Columns
- Reorder Rows with Drag and Drop
- Row Expansion
- Context Menu Integration
- Configuring the Paginator (included with examples for Paginator)
- TabView Integration
- Complex Example of Multiple Features
- Client-side Filtering of Local Data
- Filtering of Dynamic Data
- Datatable with Autocomplete
- Skinning Model
- Ratings (included with examples for ProgressBar Control)
- YQLDataSource (included with examples for DataSource Utility)
- Complex Application (included with examples for Layout Manager)

### More DataTable Control Resources:

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

Copyright © 2009 Yahoo! Inc. All rights reserved.

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