PDA

View Full Version : JSONDataModel Empty Rows



saasira
11 Dec 2006, 7:47 AM
Hi! everybody,
I
can some one help me in solving this issue?
I'm posting the code I've written below:



var UsersXMLGrid = {

onLoad : function(){
this.grid.getSelectionModel().selectFirstRow();
},

init : function() {


var schema = {
root: 'Users',
totalProperty: 'Total',
id: 'user',
fields: ['fullname','company','login','password']
};


var data ={'Users':[

{'user':[{'Name':'Samba Siva Rao','Company':'PrapanSol','Login':'skolusu','Password':'welcome123'}]},
{'user':[{'Name':'Anil','Company':'PrapanSol','Login':'agolla','Password':'welcome'}]}

]};




var columns= [{ header: "Name", width: 145, sortable: true },{ header: "Company", width: 165, sortable: true },{ header: "Login", width: 185, sortable: true },{ header: "Password", width: 100, sortable: true }];


this.dataModel = new YAHOO.ext.grid.JSONDataModel(schema);

this.colModel = new YAHOO.ext.grid.DefaultColumnModel(columns);
this.grid = new YAHOO.ext.grid.Grid('example-grid', this.dataModel, this.colModel);
this.grid.render();
this.grid.dataModel.loadData(data);


}
};

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






Where am I doing wrong?
I'm getting empty rows with out any data.

Can you please help me?
Thanking you in advance,
Samba

jack.slocum
11 Dec 2006, 12:25 PM
I think it has to do with the structure of your JSON data. JSONDataModel is looking for a structure like this:


var data ={'Users':[
{'UserId': 'user123', 'Name':'Samba Siva Rao','Company':'PrapanSol','Login':'skolusu','Password':'welcome123'},
{'UserId': 'user456', 'Name':'Anil','Company':'PrapanSol','Login':'agolla','Password':'welcome'}
]};

It expects an object containing an array of objects and it looks like you are passing in an object with an array of arrays with objects?

saasira
13 Dec 2006, 11:55 PM
Hi! Jack,
I thank you for your reply!

But I tried this earlier , but could not get the data in the rows , so I have tried in every possible way I can think of!
Still I'm not getting the data in the grid.
I'm getting empty rows.

Here is my Schema:



var schema = {
root: 'Users',
totalProperty: 'Total',
id: 'user',
fields: ['fullname','company','login','password']
};


Here is my data, as you suggested!:


var data ={'Users':[
{'UserId': 'user123', 'Name':'Samba Siva Rao','Company':'PrapanSol','Login':'skolusu','Password':'welcome123'},
{'UserId': 'user456', 'Name':'Anil','Company':'PrapanSol','Login':'agolla','Password':'welcome'}
]};

And here is my my Column model;:


var columns= [{ header: "UserId", width: 110, sortable: true },{ header: "Name", width: 130, sortable: true },{ header: "Company", width: 125, sortable: true },{ header: "Login", width: 75, sortable: true },{ header: "Password", width: 105, sortable: true }];


Can you please help me fix this problem for me?
Where am I dooing wrong?

tThanking you ,
Samba

manugoel2003
14 Dec 2006, 2:41 AM
Here is my data, as you suggested!:


var data ={'Users':[
{'UserId': 'user123', 'Name':'Samba Siva Rao','Company':'PrapanSol','Login':'skolusu','Password':'welcome123'},
{'UserId': 'user456', 'Name':'Anil','Company':'PrapanSol','Login':'agolla','Password':'welcome'}
]};



I think the data is not right here as well, try this


var data ={'Users':[
{'user': 'user123', 'fullname':'Samba Siva Rao','company':'PrapanSol','login':'skolusu','password':'welcome123'},
{'user': 'user456', 'fullname':'Anil','company':'PrapanSol','login':'agolla','password':'welcome'}
]};


Also try using double quotes instead of single quotes, that created some problem for me too.... hope it helps

tryanDLS
14 Dec 2006, 7:29 AM
a) singe quotes vs double quotes is not an issue unless you aren't consistent and don't match pairs.

b) the property names should not be quoted. So it should be


var data = {Users:[
{UserId: 'user123', Name:'Samba Siva Rao',Company:'PrapanSol',Login:'skolusu',Password:'welcome123'},
{UserId: 'user456', Name:'Anil',Company:'PrapanSol',Login:'agolla',Password:'welcome'}
]};

jack.slocum
14 Dec 2006, 7:31 AM
Or you can change your schema to match the data. The "fields" array on your schema should match the fields (aka properties) on the objects in your data.

You have the fields: ['fullname','company','login','password']

however your data has:

{'UserId': 'user123', 'Name':'Samba Siva Rao','Company':'PrapanSol','Login':'skolusu','Password':'welcome123'}

You are also referencing 5 fields in your column model, but your data model only has 4 (there is 5 in the json data though).

saasira
15 Dec 2006, 8:47 PM
Thankyou friends,
I understood where I'm wrong !

I could solve the problem iby changing the Scema, as well matching the no. of fieldnames with the no. of Column headers,and also with the no of data values!
Thanks for your co-operation!