YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataTable Control: JSON Data Over XHR

# DataTable Control: JSON Data Over XHR

This example populates a DataTable with data received via XHR from the Yahoo! Local webservice.

View example in new window.

## Sample Code for this Example

Data:

{"ResultSet":{ "totalResultsAvailable":665, "totalResultsReturned":10, "firstResultPosition":1, "ResultSetMapUrl":"http:\/\/local.yahoo.com\/mapview?stx=pizza&csz=Sunnyvale%2C+CA+94089&city=Sunnyvale&state=CA&radius=15&ed=9brhZa131DwigChqKlCo22kM1H_9WgoouCr87Ao-", "Result":[{ "Title":"Pizza Depot", "Address":"919 E Duane Ave", "City":"Sunnyvale", "State":"CA", "Phone":"(408) 245-7760", "Latitude":"37.388537", "Longitude":"-122.003972", "Rating":{ "AverageRating":"3.5", "TotalRatings":"5", "TotalReviews":"5", "LastReviewDate":"1161495667"}, "Distance":"0.93", "Url":"http:\/\/local.yahoo.com\/details?id=21332021&stx=pizza&csz=Sunnyvale+CA&ed=6tiAL6160Sx1XVIEu1zIWPu6fD8rJDV4.offJLNUTb1Ri2Q.R5oLTYvDCz8YmzivI7Bz0gfrpw--", "ClickUrl":"http:\/\/local.yahoo.com\/details?id=21332021&stx=pizza&csz=Sunnyvale+CA&ed=6tiAL6160Sx1XVIEu1zIWPu6fD8rJDV4.offJLNUTb1Ri2Q.R5oLTYvDCz8YmzivI7Bz0gfrpw--", "MapUrl":"http:\/\/maps.yahoo.com\/maps_result?name=Pizza+Depot&desc=4082457760&csz=Sunnyvale+CA&qty=9&cs=9&ed=6tiAL6160Sx1XVIEu1zIWPu6fD8rJDV4.offJLNUTb1Ri2Q.R5oLTYvDCz8YmzivI7Bz0gfrpw--&gid1=21332021", "BusinessUrl":"http:\/\/pizza-depot.com\/", "BusinessClickUrl":"http:\/\/pizza-depot.com\/"}, ... ]} }

CSS:

/* No custom CSS. */

Markup:

<div id="json"></div>

JavaScript:

YAHOO.util.Event.addListener(window, "load", function() { YAHOO.example.XHR_JSON = new function() { this.formatUrl = function(elCell, oRecord, oColumn, sData) { elCell.innerHTML = "<a href='" + oRecord.getData("ClickUrl") + "' target='_blank'>" + sData + "</a>"; }; var myColumnDefs = [ {key:"Title", label:"Name", sortable:true, formatter:this.formatUrl}, {key:"Phone"}, {key:"City"}, {key:"Rating.AverageRating", label:"Rating", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true} ]; this.myDataSource = new YAHOO.util.DataSource("assets/php/ylocal_proxy.php?"); this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON; this.myDataSource.connXhrMode = "queueRequests"; this.myDataSource.responseSchema = { resultsList: "ResultSet.Result", fields: ["Title","Phone","City",{key:"Rating.AverageRating",parser:"number"},"ClickUrl"] }; this.myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs, this.myDataSource, {initialRequest:"query=pizza&zip=94089&results=10&output=json"}); var myCallback = function() { this.set("sortedBy", null); this.onDataReturnAppendRows.apply(this,arguments); }; var callback1 = { success : myCallback, failure : myCallback, scope : this.myDataTable }; this.myDataSource.sendRequest("query=mexican&zip=94089&results=10&output=json", callback1); var callback2 = { success : myCallback, failure : myCallback, scope : this.myDataTable }; this.myDataSource.sendRequest("query=chinese&zip=94089&results=10&output=json", callback2); }; });

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

- 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 © 2010 Yahoo! Inc. All rights reserved.

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