Sunday, November 22, 2009

Javascript function to generate yui datatable with json data

The following Javascript function to generate yui datatable with json data,

function generateDatatable(target,jsonObj,myColumnDefs,hfId){
var root;
for(key in jsonObj){
root = key; break;
}
var rootId = "id";
if(jsonObj[root].length>0){
for(key in jsonObj[root][0]){
rootId = key; break;
}
}
YAHOO.example.DynamicData = function() {
var myPaginator = new YAHOO.widget.Paginator({
rowsPerPage: 5,
template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
rowsPerPageOptions: [10,25,50,100],
pageLinks: 10 });
// DataSource instance
var myDataSource = new YAHOO.util.DataSource(jsonObj);
myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
myDataSource.responseSchema = {resultsList: root,fields:new Array()};
myDataSource.responseSchema.fields[0]=rootId;
for(var i=0;i
myDataSource.responseSchema.fields[i+1] = myColumnDefs[i].key;
}
// DataTable configuration
var myConfigs = {
sortedBy : {key:myDataSource.responseSchema.fields[1], dir:YAHOO.widget.DataTable.CLASS_ASC}, // Sets UI initial sort arrow
paginator : myPaginator
};
// DataTable instance
var myDataTable = new YAHOO.widget.DataTable(target, myColumnDefs, myDataSource, myConfigs);
myDataTable.subscribe("rowMouseoverEvent", myDataTable.onEventHighlightRow);
myDataTable.subscribe("rowMouseoutEvent", myDataTable.onEventUnhighlightRow);
myDataTable.subscribe("rowClickEvent", myDataTable.onEventSelectRow);
myDataTable.subscribe("checkboxClickEvent", function(oArgs){
var hidObj = document.getElementById(hfId);
var elCheckbox = oArgs.target;
var oRecord = this.getRecord(elCheckbox);
var id=oRecord.getData(rootId);
if(elCheckbox.checked){
if(hidObj.value == ""){
hidObj.value = id;
}
else{
hidObj.value += "," + id;
}
}
else{
hidObj.value = removeIdFromArray(""+hfId,id);
}
});
myPaginator.subscribe("changeRequest", function (){
if(document.getElementById(hfId).value != "")
{
if(document.getElementById("ConfirmationPanel").style.display=='block')
{
document.getElementById("ConfirmationPanel").style.display='none';
}
document.getElementById(hfId).value="";
}
return true;
});
myDataTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
oPayload.totalRecords = oResponse.meta.totalRecords;
return oPayload;
}
return {
ds: myDataSource,
dt: myDataTable
};
}();
}
function removeIdFromArray(values,id){
values=document.getElementById(values).value;
if(values.indexOf(',')==0){
values=values.substring(1);
}
if(values.indexOf(values.length-1)==","){
values=values.substring(0,values.length-1);
}
var ids=values.split(',');
var rtnValue="";
for(var i=0;i
if(ids[i]!=id){
rtnValue+=","+ids[i];
}
}
if(rtnValue.indexOf(",")==0){
rtnValue=rtnValue.substring(1);
}
return rtnValue;
}

3 comments:

  1. hi Pandiya
    good thought..
    but i am using sql server..so can u send
    sample code sql server.

    advance thanks
    murugaperumal s

    ReplyDelete
  2. Hai perumal,
    Just call this function from your server side code....
    For ex:

    ReplyDelete
  3. I drop a comment whenever I appreciate a post on a website or
    if I have something to contribute to the conversation.
    Usually it is triggered by the passion displayed in the post I
    looked at. And after this article "Javascript function to generate yui datatable with json data".
    I was excited enough to create a comment :) I actually do have 2 questions for you if you don't mind. Is it simply me or does it appear like some of the responses come across as if they are left by brain dead visitors? :-P And, if you are posting at other sites, I'd like to keep up with you.
    Would you list all of your shared pages like your twitter feed, Facebook page or
    linkedin profile?

    my web site: bancuri cu ciobani

    ReplyDelete