PDA

View Full Version : How can I add responseText directly to load() method



burgleboy711
25 Dec 2006, 10:49 PM
How can I add an ajax response responseText directly to load() method of JSONDataModel.My responseText is a json string.

jack.slocum
26 Dec 2006, 3:52 AM
loadData() will take an object and load it as if it was loaded through calling load(). That means you will need to eval the JSON first.

burgleboy711
26 Dec 2006, 11:07 PM
Thanks for helping me Jack, but I have a problem with this code:

UsersXMLGrid = function(){
var dataModel = new YAHOO.ext.grid.JSONDataModel({
root: 'customers',
totalProperty: 'Total',
id: 'customer',
fields: ['id','firstname','lastname','age']});

return {
init : function() {
var data ={"customers":[
{"lastname":"Liebzeit","age":25,"firstname":"Carsten","id":1},
{"lastname":"Nghiem","age":23,"firstname":"Hoang","id":2},
{"lastname":"Vu","age":23,"firstname":"Hung","id":3},
{"lastname":"Tran","age":25,"firstname":"Kha","id":4},
{"lastname":"sdfasf","age":12,"firstname":"abc","id":5}]};

var columns= [{ header: "ID", width: 15, sortable: true },
{ header: "First Name", width: 185, sortable: true },
{ header: "Last Name", width: 145, sortable: true },
{ header: "Age", width: 30, sortable: true }];
//dataModel = new YAHOO.ext.grid.JSONDataModel(schema);
this.colModel = new YAHOO.ext.grid.DefaultColumnModel(columns);
this.grid = new YAHOO.ext.grid.Grid('example-grid',
dataModel,
this.colModel);
this.grid.render();
//************
//this.grid.dataModel.loadData(data);
//**************
loadData();
}
}
function loadData(){
var lcallback={ success:loadSucess};
YAHOO.util.Connect.asyncRequest('GET', 'getResponse.do', lcallback);
}
function loadSucess(res){
dataModel.loadData(res.responseText);
}
}();

YAHOO.util.Event.on(window, 'load', UsersXMLGrid.init, UsersXMLGrid, true);


This code, I'm using a request to a struts action that return a json string in res.responseText and I want to load it directly through dataModel.loadData(res.responseText). When I run this code, the grid is empty. But when I load by exist json string this.grid.dataModel.loadData(data), the grid has data. Can you show me my error and How to fix it?[/code]

burgleboy711
26 Dec 2006, 11:14 PM
I have solve my problem. My error code is dataModel.loadData(res.responseText). When I place with dataModel.loadData(eval('('+ res.responseText+')') it work. Thanks for helping me.