PDA

View Full Version : What is wrong with json format ?



jabba
23 Aug 2009, 2:47 AM
Hi everybody,
I have a little problem.

very simple window with ext 3 with grid:



var store = new Ext.data.Store({
mode:'remote',
root:"rows",
totalProperty:'total',


reader: new Ext.data.JsonReader({}, ['ID']),
url: 'ajax.store.kh.php',
fields: [
{name: 'ID', type: 'int'}//,
]
});
store.load();


var grid = new Ext.grid.GridPanel({
store: store,
mode:'remote',
columns: [
{id:'ID',header: "ID", width: 160, sortable: true, dataIndex: 'ID'}//,
],
stripeRows: true,
title:'Klienci',
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true
})
});



php script generating output is ok:




$o = array(
"success"=>true,
"total"=>"2000", // temporary seted to const value
"rows"=>$arr // data array
);

echo json_encode($o);


answer in firebug is

{"success":true,"total":"2000","rows":[{"ID":1509},{"ID":1497},{"ID":1496},{"ID":1495},
{"ID":1494}]}

I see grid with column ID and with calculated pages but without data.


if I change php to echo only array with data:



echo json_encode($arr);



grid is diplayed ok.



What is wrong ? How to setup paging ?

Best regards
Jabba

steffenk
23 Aug 2009, 3:03 AM
there is no property mode for store or grid, also JsonReader is configured wrong. Consult the API (http://extjs.com/deploy/dev/docs/?class=Ext.data.JsonReader), there is a sample also.

rbraddy
23 Aug 2009, 5:35 AM
Jabba,

Been down this road... ExtJS requires a specially-formed JSON string. Here's an example of one that works for me with my app:

{"project":[{"answers":"","name":"WinningWare QuickLaunch","email":"","created_at":"2009-07-03 03:44:06 UTC","projectstatus":"","updated_at":"2009-07-03 03:44:06 UTC","account_id":"1","id":"1","projecttype":"0"},
{"answers":"","name":"Precision Marketing System","email":"","created_at":"2009-07-03 04:31:00 UTC","projectstatus":"","updated_at":"2009-07-03 04:31:00 UTC","account_id":"1","id":"5","projecttype":"0"}],
"results":"15"}

If you're seeing the ID column in the grid, it's working, but you need to define the rest of your data fields (columns).

Hope that helps.

Rick

httpdotcom
23 Aug 2009, 6:45 AM
success flag is not required (a failure is returning incorrect JSON or, sometimes, 0 rows).

The following PHP code is sufficient for your needs.


$total = 2000 ; //should be total result from query
// loop through query returning selected rows into $array based on $start and $limit passed from PagingToolbar

$retval = array( "results" => $total, "rows" => $array ) ;
echo json_encode( $retval ) ;


Also, pay attention to steffan's comments about config options on the store and grid.

jabba
23 Aug 2009, 10:32 AM
thanks its working now.

missing properties in JsonReader was a key.

Jabba