-
25 Jun 2012 5:38 AM #1
Answered: Grid with empy JSON store shows 1 record
Answered: Grid with empy JSON store shows 1 record
Hi!
I'm using ExtJs 4.1.0 and I have a grid with a JSON Store that shows the records of a MySQL table:
MySQL Table structure:Code:Ext.define('modelRegole', { extend: 'Ext.data.Model', fields: [ {name: 'idrego', type: 'int'}, {name: 'descri', type: 'string'}, {name: 'idtipo_m', type: 'string'}, {name: 'active', type: 'boolean'} ] }); var storeRegole = Ext.create('Ext.data.Store', { autoDestroy: true, model: 'modelRegole', proxy: { type: 'ajax', url : 'storeRegole.php', reader: { type: 'json' }, actionMethods: { read: 'POST' } }, autoLoad: false }); var gridRegole = Ext.create('Ext.grid.Panel', { id: 'id_gridRegole', store: storeRegole, title: 'Regole', frame: true, columns: [ { text : 'ID Regola', width : 70, dataIndex : 'idrego' }, { text : 'Descrizione', width : 400, dataIndex : 'descri' }, { text : 'Tipologia/e', width : 400, dataIndex : 'idtipo_m' }, { text : 'Attiva', width : 100, dataIndex : 'active' } ], height: 500, width: 900 });
storeRegole.php:Code:mysql> desc regole; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | idrego | int(11) | NO | PRI | NULL | | | idtipo_m | varchar(2500) | NO | | NULL | | | descri | varchar(255) | NO | | NULL | | | active | smallint(6) | NO | | NULL | | +----------+---------------+------+-----+---------+-------+
If my table (regole) is empy, I see my grid with 1 empty record:PHP Code:$str = "select * from regole order by idrego";
$rst = mysql_query($str);
while ( $arr = mysql_fetch_array($rst) )
{
$n++;
$esito[] = $arr;
}
if ($n == 0)
$esito[] = array();
echo json_encode($esito);
err.jpg
I suppose it is for:
but if I don't use that php code, I have the same result.PHP Code:if ($n == 0)
$esito[] = array();
Suggestions?PHP Code:$str = "select * from regole order by idrego";
$rst = mysql_query($str);
while ( $arr = mysql_fetch_array($rst) )
{
$n++;
$esito[] = $arr;
}
if ($n == 0)
{
}
else
{
echo json_encode($esito);
}
Thanks in advance.
-
Best Answer Posted by Tim ToadyThis should create a record. It is a 2 dimensional array. If there are no records it should be a single empty array.PHP Code:
$esito[] = array();
--edit--
Just to clarify what you want for when there are no records is
PHP Code:$esito = array();
-
25 Jun 2012 7:21 AM #2
edit--
Retracted my first post after looking at your php some more.
-
25 Jun 2012 7:25 AM #3
Ops, thank you, I made a mistake, the correct php code is:
My question remains alive...PHP Code:$str = "select * from regole order by idrego";
$rst = mysql_query($str);
while ( $arr = mysql_fetch_array($rst) )
{
$n++;
$esito[] = $arr;
}
if ($n == 0)
{
}
else
{
echo json_encode($esito);
}
-
25 Jun 2012 7:32 AM #4
I actually think I answered too quickly and when I looked at it again I didn't see the issue I thought I did. What is the json that is coming back?
-
25 Jun 2012 7:38 AM #5
I can synthesize my problem so:
if I make a JSON grid store with this php code:
I don't see an empty grid, but I see a grid with 1 (empty) record like this:PHP Code:$esito[] = array();
echo json_encode($esito);
err.jpg
Why?

-
25 Jun 2012 7:44 AM #6
If I make the echo of json_encode of an empty php array I have BLANK output (blank string).
See firebug:
firebug.png
-
25 Jun 2012 7:48 AM #7
This should create a record. It is a 2 dimensional array. If there are no records it should be a single empty array.PHP Code:$esito[] = array();
--edit--
Just to clarify what you want for when there are no records is
PHP Code:$esito = array();
-
25 Jun 2012 8:01 AM #8
Thank you very much! Now it works:
Here a live example: http://www.playersmagazine.it/smeraldo/empty_grid.htmlPHP Code:<?php
$esito = array();
echo json_encode($esito);
?>
-
25 Jun 2012 8:03 AM #9
Your json coming back is
Is should be the following if the grid is empty.PHP Code:[[]]
PHP Code:[]
-
25 Jun 2012 11:38 PM #10


Reply With Quote