Skip to content

Instantly share code, notes, and snippets.

@stlsmiths
Created June 15, 2011 16:02
Show Gist options
  • Save stlsmiths/1027414 to your computer and use it in GitHub Desktop.
Save stlsmiths/1027414 to your computer and use it in GitHub Desktop.
YUI2 local JSON complex parse with DataTable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>YUI DataTable Example</title>
<script src="http://yui.yahooapis.com/2.9.0/build/yuiloader/yuiloader-min.js"></script>
<script type="text/javascript">
var loader = new YAHOO.util.YUILoader({
require: [ 'reset', 'fonts', 'base', "datatable", "datasource", "json", "yahoo", "dom", "event" ],
loadOptional: true,
// The function to call when all script/css resources have been loaded
onSuccess: function() {
//
// Aliases for YUI placeholders
//
var YDom = YAHOO.util.Dom,
YUtil = YAHOO.util,
YEvent = YAHOO.util.Event,
YLang = YAHOO.lang;
YAHOO.util.Event.onDOMReady( function() {
//
// Define the JSON "string"
//
var JSONString = '{"myResults":[{ "color_no" : "1", "color_code" : "#0000ff", "cname" : "Blue" }, { "child_to_use" : "2", "sub_child":[ { "blank":""}, { "blank":""}, {"color_no" : "2", "color_code" : "#ff6347", "cname" : "Tomato" }] }, { "color_no" : "13", "color_code" : "#98fb98", "cname" : "PaleGreen" }, { "color_no" : "4", "color_code" : "#ffd700", "cname" : "Gold" } ]}';
/*
* The JSON object actually is this ...
{
"myResults":[
{
"color_no":"1",
"color_code":"#0000ff",
"cname":"Blue"
},
{
"child_to_use" : "2",
"sub_child":[ // we only want to use sub_child[2] in our data ...
{
"blank":""
},
{
"blank":""
},
{
"color_no":"2",
"color_code":"#ff6347",
"cname":"Tomato"
}
]
},
{
"color_no":"13",
"color_code":"#98fb98",
"cname":"PaleGreen"
},
{
"color_no":"4",
"color_code":"#ffd700",
"cname":"Gold"
}
]
}
*/
//
// YOU DON'T HAVE TO PARSE the JSON, DS is smart enought to know the difference !!!
//
// Now parse the JSON string to make a local JSON object ...
//
//
// var JSONObj = YAHOO.lang.JSON.parse(JSONString);
var myDS = new YAHOO.util.LocalDataSource( JSONString );
myDS.responseType = YAHOO.util.DataSource.TYPE_JSON;
myDS.responseSchema = {
resultsList:'myResults', // This is CRUCIAL if you use TYPE_JSON ...
fields : [ {key:"color_no", parser:"number"}, "color_code", "cname" ]
};
myDS.doBeforeParseData = function( oReq, oFullResp, oCB ) {
//
// on entry, oFullResp contains a JS array (after JSON parsing the "raw" JSON string ...)
//
var newFullResp = { myResults:[] }; // create an empty JS return object array
//
// Loop thru the results array, convert our "sub_child" member if encountered
//
for(var i=0; i<oFullResp.myResults.length; i++) {
var arow = oFullResp.myResults[i];
if ( YLang.hasOwnProperty( arow, 'sub_child') && arow.child_to_use )
newFullResp.myResults.push( arow.sub_child[ arow.child_to_use] ); // only come here if sub_child exists
else
newFullResp.myResults.push( arow ); // otherwise just append the row
}
//
// Return the "modified" response object
//
return newFullResp;
}
var myCols = [
{ key: "color_no", label: "Id", sortable:true },
{ key: "color_code", label: "Hex Code", sortable:true },
{ key: "cname", label: "CSS Color Name", sortable:true }
];
var myDT = new YAHOO.widget.DataTable( "divTable", myCols, myDS );
});
},
timeout: 5000,
combine: true
});
loader.insert();
</script>
</head>
<body class='yui-skin-sam'>
<h1>YUI DataTable Example - Local JSON with complex data</h1>
<div id="divTable"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment