PDA

View Full Version : grid problem with json answer



Raidri
4 Feb 2010, 1:20 AM
Hey,

I hope you can help me. I load data from my Database(with PHP) and then i return the rows with a json answer.
But the rows won't shown in my grid.
So i don't see my fault

Thanks for help.

Don't cry if u see my code, i'm new in the extJS world ;)
Here the PHP Code:


if($total > 0){
$results = array();

foreach($posts as $post){
$postArray = array();
$postArray['id'] = $post->getUid();
$postArray['title'] = $post->getTitle();
$postArray['description'] = $post->getDescription();
$postArray['author'] = $post->getAuthor();
$postArray['date'] = $post->getDate()->format('d/M/Y H:i');
$results[] = $postArray;
}
$jsonResults = json_encode($results);
echo '({"total":"'.$total.'","results":'.$jsonResults.'})';
}
else {
echo '({"total":"0","results":})';
}
That returns PHP, i thinks thats right.


({"total":"29","results":[{"id":89,"title":"Post #29","description":"Das ist Post #29","author":"Raidri","date":"02\/Feb\/2010 11:57"},{"id":88,"title":"Post #29","description":"Das ist Post #29","author":"Raidri","date":"02\/Feb\/2010 10:14"},{"id":87,"title":"Post #28","description":"Das ist Post #28","author":"Raidri","date":"02\/Feb\/2010 10:12"},{"id":66,"title":"Post #27","description":"Das ist Post #27","author":"Raidri","date":"01\/Feb\/2010 12:36"},{"id":62,"title":"Post #26","description":"Das ist Post #26","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":61,"title":"Post #25","description":"Das ist Post #25","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":60,"title":"Post #24","description":"Das ist Post #24","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":59,"title":"Post #23","description":"Das ist Post #23","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":58,"title":"Post #22","description":"Das ist Post #22","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":57,"title":"Post #21","description":"Das ist Post #21","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":56,"title":"Post #20","description":"Das ist Post #20","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":55,"title":"Post #19","description":"Das ist Post #19","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":54,"title":"Post #18","description":"Das ist Post #18","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":53,"title":"Post #17","description":"Das ist Post #17","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":52,"title":"Post #16","description":"Das ist Post #16","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":51,"title":"Post #15","description":"Das ist Post #15","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":50,"title":"Post #14","description":"Das ist Post #14","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":49,"title":"Post #13","description":"Das ist Post #13","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":48,"title":"Post #12","description":"Das ist Post #12","author":"Raidri","date":"20\/Jan\/2010 00:00"},{"id":47,"title":"Post #11","description":"Das ist Post #11","author":"Raidri","date":"20\/Jan\/2010 00:00"}]})Here the extJS code :


Ext.onReady(function(){ var store = new Ext.data.Store({
autoLoad:true,
proxy: new Ext.data.HttpProxy({
url: 'mod.php?M=web_MfBlogTxMfblogM1& id=&tx_mfblog_web_mfblogtxmfblogm1%5Bblog%5D=5&tx_mfblog_web_mfblogtxmfblogm1%5Baction%5D=index&tx_mfblog_web_mfblogtxmfblogm1%5Bcontroller%5D=Ajax',
method: 'POST'
}),
baseParams: {start: 0, limit: 20},
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
},
[
{name: 'id', type:'int'},
{name: 'title', type:'string'},
{name: 'description', type:'string'},
{name: 'author', type:'string'},
{name: 'date', type:'date'},
])
});
var grid = new Ext.grid.GridPanel({
height: 600,
width: 650,
title: 'Show Posts',
store: store,
bbar: new Ext.PagingToolbar({
store: store,
displayInfo: true,
pageSize: 15,
prependButtons: true
}),
colModel: new Ext.grid.ColumnModel({
defaults: {
sortable: true
},
columns: [
{id: 'id', header: 'Post ID', width: 20, xtype: 'gridcolumn', dataIndex: 'id'},
{header: 'Title', width: 50, xtype: 'gridcolumn', dataIndex: 'title'},
{header: 'Description', width: 100, xtype: 'gridcolumn',dataIndex:'description'}, {header: 'Author', width: 20, xtype: 'gridcolumn', dataIndex: 'author'},
{header: 'Creation date', width: 50, xtype: 'datecolumn', format: 'd.M.Y H:i',
dataIndex: 'date'}
]
}),
viewConfig: {
forceFit: true
}
});
grid.render('tx_mfblog');});

fay
4 Feb 2010, 2:11 AM
There are two things you need to fix first:

1. The JSON isn't "exactly" valid. You need to remove the opening and closing round brackets "(" and ")":


echo '({"total":"'.$total.'","results":'.$jsonResults.'})'; // <-- remove ( and )

2. You have some extra commas that will cause problems with IE:


// ...
totalProperty: 'total', // <-- remove comma

// ...
{name: 'date', type:'date'}, // <-- remove comma

Raidri
4 Feb 2010, 2:53 AM
oh man, thanks a lot.

It was the round brackets ;).