Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: DataTable Control (beta): Server-side Sorting

# DataTable Control (beta): Server-side Sorting

This example integrates with server-side logic for customized sorting.

View example in new window.

## Sample Code for this Example

Data:

{"recordsReturned":25, "totalRecords":1397, "startIndex":0, "sort":null, "dir":"asc", "records":[ {"id":"0", "name":"xmlqoyzgmykrphvyiz", "date":"13-Sep-2002", "price":"8370", "number":"8056", "address":"qdfbc", "company":"taufrid", "desc":"pppzhfhcdqcvbirw", "age":"5512", "title":"zticbcd", "phone":"hvdkltabshgakjqmfrvxo", "email":"eodnqepua", "zip":"eodnqepua", "country":"pdibxicpqipbsgnxyjumsza"}, ... ] }

CSS:

/* No custom CSS. */

Markup:

<div id="serversorting"></div>

JavaScript:

YAHOO.util.Event.addListener(window, "load", function() { YAHOO.example.ServerSorting = new function() { // Column definitions var myColumnDefs = [ {key:"id", label:"ID", sortable:true}, {key:"name", label:"Name", sortable:true}, {key:"date", label:"Date", sortable:true}, {key:"price", label:"Price", sortable:true}, {key:"number", label:"Number", sortable:true}, {key:"address", label:"Address", sortable:true}, {key:"company", label:"Company", sortable:true}, {key:"desc", label:"Description", sortable:true}, {key:"age", label:"Age", sortable:true}, {key:"title", label:"Title", sortable:true}, {key:"phone", label:"Phone", sortable:true}, {key:"email", label:"Email", sortable:true}, {key:"zip", label:"Zip", sortable:true}, {key:"country", label:"Country", sortable:true} ]; // DataSource instance this.myDataSource = new YAHOO.util.DataSource("assets/php/json_proxy.php?"); this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON; this.myDataSource.responseSchema = { resultsList: "records", fields: ["id","name","date","price","number","address","company","desc","age","title","phone","email","zip","country"] }; // DataTable instance var oConfigs = { initialRequest: "sort=id&dir=asc&results=100", // Server parameters sortedBy:{key:"id", dir:"asc"} // Set up initial column headers }; this.myDataTable = new YAHOO.widget.DataTable("serversorting", myColumnDefs, this.myDataSource, oConfigs); // Custom code to parse the raw server data for sortedby values this.myDataSource.doBeforeCallback = function(oRequest, oRawResponse, oParsedResponse) { var oSelf = YAHOO.example.ServerSorting; var oDataTable = oSelf.myDataTable; // Get sortedby values var oRawResponse = oRawResponse.parseJSON(); //JSON.parse(oRawResponse); // Parse the JSON data var sortCol = oRawResponse.sort; // Which column is sorted var sortDir = oRawResponse.dir; // Which sort direction // Update the config sortedBy with new values var newSortedBy = { key: sortCol, dir: sortDir } oDataTable.set("sortedBy", newSortedBy); // Let the DataSource parse the rest of the response return oParsedResponse; }; // Override function for custom sorting this.myDataTable.sortColumn = function(oColumn) { // Which direction var sDir = "asc"; // Already sorted? if(oColumn.key === this.get("sortedBy").key) { sDir = (this.get("sortedBy").dir === "asc") ? "desc" : "asc"; } var newRequest = "sort=" + oColumn.key + "&dir=" + sDir + "&results=100&startIndex=0"; this.getDataSource().sendRequest(newRequest, this.onDataReturnInitializeTable, this); }; }; });

### DataTable Control (beta) Examples:

- Basic Example
- Progressive Enhancement
- Custom Cell Formatting
- Nested headers
- JSON Data Over XHR
- XML Data Over XHR With POST
- Textual Data Over XHR
- Local XML Data
- Client-side Pagination
- Client-side Sorting
- Server-side Pagination
- Server-side Sorting
- Server-side Pagination and Sorting, with Browser History Manager
- Fixed Scrolling
- Row Selection
- Cell Selection
- Inline Cell Editing
- Context Menu Integration
- Complex Example of Multiple Features
- Datatable with Autocomplete
- Skinning Model

### More DataTable Control (beta) Resources:

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

Copyright © 2007 Yahoo! Inc. All rights reserved.

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