YUI Library Home

YUI Library Examples: DataTable Control: XML Data Over XHR With POST

DataTable Control: XML Data Over XHR With POST

This examples makes a POST query to the Yahoo! Local webservice to populate a DataTable.

Please note: XML is not currently supported on Android devices.

Phone
City
Loading...

Sample Code for this Example

Data:

1<?xml version="1.0"?> 
2<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:lcl" xsi:schemaLocation="urn:yahoo:lcl http://api.local.yahoo.com/LocalSearchService/V2/LocalSearchResponse.xsd" totalResultsAvailable="501" totalResultsReturned="10" firstResultPosition="1"
3<ResultSetMapUrl>http://maps.yahoo.com/broadband/?q1=Sunnyvale%2C+CA+94089&tt=pizza&tp=1</ResultSetMapUrl
4    <Result> 
5        <Title>Giovannis Pizzeria</Title> 
6        <Address>1127 N Lawrence Expy</Address> 
7        <City>Sunnyvale</City> 
8        <State>CA</State> 
9        <Phone>(408) 734-4221</Phone> 
10        <Latitude>37.396953</Latitude> 
11        <Longitude>-121.995986</Longitude> 
12        <Rating> 
13            <AverageRating>4</AverageRating> 
14            <TotalRatings>51</TotalRatings> 
15            <TotalReviews>33</TotalReviews> 
16            <LastReviewDate>1191207644</LastReviewDate> 
17        </Rating> 
18        <Distance>0.62</Distance> 
19        <Url>http://local.yahoo.com/details?id=21341983&stx=pizza&csz=Sunnyvale+CA&ed=kYc.Ba160SxZddWADEWWMRsGo0KgZ6X22_QAgTZxq3OdfrVCfCdLU9mvvJeybt8XpDhMC58HjElJAiWi</Url> 
20        <ClickUrl>http://local.yahoo.com/details?id=21341983&stx=pizza&csz=Sunnyvale+CA&ed=kYc.Ba160SxZddWADEWWMRsGo0KgZ6X22_QAgTZxq3OdfrVCfCdLU9mvvJeybt8XpDhMC58HjElJAiWi</ClickUrl> 
21        <MapUrl>http://maps.yahoo.com/maps_result?name=Giovannis+Pizzeria&desc=4087344221&csz=Sunnyvale+CA&qty=9&cs=9&ed=kYc.Ba160SxZddWADEWWMRsGo0KgZ6X22_QAgTZxq3OdfrVCfCdLU9mvvJeybt8XpDhMC58HjElJAiWi&gid1=21341983</MapUrl
22        <BusinessUrl></BusinessUrl
23        <BusinessClickUrl></BusinessClickUrl
24    </Result> 
25    ... 
26</ResultSet> 
view plain | print | ?

CSS:

1/* No custom CSS. */ 
view plain | print | ?

Markup:

1<div id="xml"></div> 
view plain | print | ?

JavaScript:

1YAHOO.util.Event.addListener(window, "load"function() { 
2    YAHOO.example.XHR_XML = function() { 
3        var formatUrl = function(elCell, oRecord, oColumn, sData) { 
4            elCell.innerHTML = "<a href='" + oRecord.getData("ClickUrl") + "' target='_blank'>" + sData + "</a>"
5        }; 
6 
7        var formatRating = function(elCell, oRecord, oColumn, sData) { 
8            elCell.innerHTML = (sData === null) ? "N/A" : YAHOO.util.Number.format(sData); 
9        }; 
10 
11        var myColumnDefs = [ 
12            {key:"Title", label:"Name", sortable:true, formatter:formatUrl}, 
13            {key:"Phone"}, 
14            {key:"City"}, 
15            {key:"AverageRating", label:"Rating",formatter:formatRating, sortable:true
16        ]; 
17 
18        var myDataSource = new YAHOO.util.DataSource("assets/php/ylocal_proxy.php?"); 
19        myDataSource.connMethodPost = true
20        myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML; 
21        myDataSource.responseSchema = { 
22            resultNode: "Result"
23            fields: ["Title","Phone","City",{key:"AverageRating",parser:"number"},"ClickUrl"
24        }; 
25 
26        var myDataTable = new YAHOO.widget.DataTable("xml", myColumnDefs, 
27                myDataSource, {initialRequest:"query=pizza&zip=94089&results=10"}); 
28 
29        return { 
30            oDS: myDataSource, 
31            oDT: myDataTable 
32        }; 
33    }(); 
34}); 
view plain | print | ?

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.

Copyright © 2011 Yahoo! Inc. All rights reserved.

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