PDA

View Full Version : jsonreader not working for me in a Ext.data.Store



jonesmc123
25 Nov 2007, 1:01 PM
my payload for the proxy is:
{"totalCount": "1", "records": [{"Agent.First Name": "Nell", "Agent.Annual Salary": "540000.00", "Agent.Last Name": "Carpenter"}]}

and my store init code:

reader: new Ext.data.JsonReader({
root: "records",
totalProperty: "totalCount",
id: "Agent.First Name",
fields: ["Agent.First Name", "Agent.Annual Salary", "Agent.Last Name"]
})

the example for paging looks like:
{"totalCount":"17467","topics":[{"threadid":"12631","forumid":"5","forumtitle":"Help","title":"Can't load values"}]} //cut off for brevity

the example's store init code

reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'threadid',
fields: [
'title', 'forumtitle', 'forumid', 'author',
{name: 'replycount', type: 'int'},
{name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
'lastposter', 'excerpt'
]
}),

the example works but I get "invalid label" :(( on the fetch of the data store.load(...)

tdondich
25 Nov 2007, 1:04 PM
The root config property specifies which element will contain the array of records. In the example, the root element is topics, which matches up with the example data. Make sure you have an appropriate root config property which matches your JSON data.

jonesmc123
25 Nov 2007, 1:41 PM
in the example I sent you can see my root is "records" and that's what I have in the jsonreader init code, I tried changing that to something else and that failed with the same error. I also went back and extended my fields definition in case it wasn't enough to put the field names in exactly as they are in the json payload:

so now my store init looks like:

store = new Ext.data.Store({
// load using script tags for cross domain, if the data in on the same domain as
// this page, an HttpProxy would be better
proxy: new Ext.data.ScriptTagProxy({
url: 'http://127.0.0.1:5000/home/get_table_data/'
}),
// data: dummyData,
// reader: reader
// create reader that reads the data records
reader: new Ext.data.JsonReader({
root: "records",
totalProperty: "totalCount",
id: "FirstName",
fields: [{name:'FirstName',type:'string'},{name:'Salary',type:'float'},{name:'LastName',type:'string'}]
})
});


still getting invalid label error. The grid is loaded up on the right element and the paging toolbar is there and I get the loading mask and animation just never loads the data into the grid and I quickly get the invalid label error, I know it's getting to my controller and getting the data back and that data is:
{"totalCount": "1", "records": [{"FirstName": "Nell", "Salary": "540000.00", "LastName": "Carpenter"}]}

It's driving me nuts! I know it's probably something very simple and obvious to someone, but I can't see anything missing here

hendricd
25 Nov 2007, 4:17 PM
Periods ( . ) are not allowed in property names. Convert them to _.

jonesmc123
25 Nov 2007, 5:02 PM
thank's for the info on period in the field names hendricd, suspected that might be an issue but I had removed those and still wasn't getting the grid to load data and had that invalid label error. What does work for me now is specifying JsonStore explicitly instead of base Store and ScriptTagProxy

so


jstore = new Ext.data.JsonStore({
url: '/home/get_table_data/',
root: 'datarows',
totalProperty: "totalCount",
fields: ['FirstName','Salary','LastName']
});

works for me. Now to make the fields and columModel built by json data...~o)

mind you this is using ext.js 2.0 rc1

oldroy
5 Dec 2007, 3:06 PM
Hello,
I'm have the same issues. Is there any way that you could post your finished code (store, reader, grid and your working returned json? This seems to be a recurring issue with new users and users going from 1.x to 2.0 as there have been big changes in the grid and there is no actual working gridpanel loaded from returned json example anywhere.

Also - did you have to dump your naming convention to get this to work? agent.firstname is now agent_firstname or firstname?

:-/:-/

Thanks

tryanDLS
5 Dec 2007, 5:34 PM
You can't use periods in the names b/c javascript will try to interpret it as a object property.

vertigoMX
5 Dec 2007, 8:26 PM
Yes, please post a working sample of all pages.

thanx

ffzhuang
6 Dec 2007, 7:14 PM
seems your serve side code have problem because of using ScriptTagProxy

check to see whether your serve side add:



boolean scriptTag = false;
String cb = request.getParameter("callback");
if (cb != null) {
scriptTag = true;
response.setContentType("text/javascript");
} else {
response.setContentType("application/x-json");
}