Yahoo! Developer Network Home - Help

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataTable Control (beta): JSON Data Over XHR

# DataTable Control (beta): 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:YAHOO.util.DataSource.parseNumber},"ClickUrl"] }; this.myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs, this.myDataSource, {initialRequest:"query=pizza&zip=94089&results=10&output=json"}); var callback1 = { success : this.myDataTable.onDataReturnAppendRows, failure : this.myDataTable.onDataReturnAppendRows, scope : this.myDataTable }; this.myDataSource.sendRequest("query=mexican&zip=94089&results=10&output=json", callback1); var callback2 = { success : this.myDataTable.onDataReturnInsertRows, failure : this.myDataTable.onDataReturnInsertRows, scope : this.myDataTable }; this.myDataSource.sendRequest("query=chinese&zip=94089&results=10&output=json", callback2); }; });

### 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 (beta) Examples:

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

### More DataTable Control (beta) Resources:

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

Copyright © 2008 Yahoo! Inc. All rights reserved.

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