PDA

View Full Version : Problem getting my Json Data into a GridPanel



ryu
22 Apr 2010, 1:27 AM
hi there!

i've a problem getting my json data into my grid panel.

i screencast tutorial about data stores and grids here: http://tdg-i.com/325/ext-js-screencast-data-stores-and-gridpanels
my code looks nearly the same. - and with his json my script works perfectly! at first i tried to generate my own json from the sql database. - didn't work. - so i copied the result of his json request at http://extjsinaction.com/dataQuery.php?limit=3 and put it into my own php file - didn't work! :(
but i don't think there's any mistake while doing the ajax request. if i open his and my json stream url in firefox it looks just the same. the only difference is the encoding. his stream is encoded ISO-8859-1, my stream is UTF-8. - but i need UTF-8 because my whole project is encoded with UTF-8. even the page content type of his stream isn't set as application/json, it's set as text/html. - so this shouldn't be a problem i think.

at this point i'm a little bit frustrated. i hope anyone has had the same problem before.

here is my javascript code:

<script type="text/javascript" language="javascript">
Ext.onReady(function(){

var fields = [
{
name: 'lastName',
mapping: 'lastname'
},
{
name: 'firstName',
mapping: 'firstname'
},
];

var proxy = new Ext.data.ScriptTagProxy ({
url: './?m=attendance&a=json_list'
});

var store = new Ext.data.JsonStore({
proxy: proxy,
fields: fields,
root: 'records',
autoLoad: true
});

var columns = [
{
header: 'First Name',
dataIndex: 'firstName'
},
{
header: 'Last Name',
dataIndex: 'lastName'
}
];

var grid = new Ext.grid.GridPanel({
title: 'My Grid Panel',
autoWidth: true,
autoHeight: true,
store: store,
loadMask: true,
columns: columns,
viewConfig: {
forceFit: true
}
});

grid.render('grid-example');
});
</script>
<div id="grid-example"></div>

ryu
22 Apr 2010, 5:18 AM
is there really no one with a clue?

cmathers
4 May 2010, 12:24 AM
I had a similar problem until I tracked down a copy of Jay's "dataQuery.php".

Here is a very basic copy of one that I wrote. I think you may be missing the "callback" logic.


<?php
$callback = $_REQUEST['callback'];

$username = "XXX";
$password = "XXX";
$database = "XXX";
$sql = "SELECT firstname FROM PARTY";
$encodable = array();

mysql_connect(localhost, $username, $password);
mysql_select_db($database)or die("Unable to select database");

$result = mysql_query($sql);

$totalCount = mysql_num_rows($result);

$sql = "SELECT firstname FROM PARTY LIMIT 5";

$result = mysql_query($sql);

while($obj = mysql_fetch_object($result))
{
$encodable[] = $obj;
}


mysql_close();

$encoded = '';

if($callback) $encoded = $callback . "(";

$encoded .= '({"totalCount" : "' . $totalCount . '", "records" : ' . json_encode($encodable) . '})';

if($callback) $encoded .= ");";

echo $encoded;
?>


Regards

Cam