PDA

View Full Version : Grid with empy JSON store shows 1 record



Dafram
25 Jun 2012, 5:38 AM
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:



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
});


MySQL Table structure:


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 | |
+----------+---------------+------+-----+---------+-------+


storeRegole.php:


$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);


If my table (regole) is empy, I see my grid with 1 empty record:

36508
I suppose it is for:


if ($n == 0)
$esito[] = array();

but if I don't use that php code, I have the same result.


$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);
}


Suggestions?

Thanks in advance.

Tim Toady
25 Jun 2012, 7:21 AM
edit--
Retracted my first post after looking at your php some more.

Dafram
25 Jun 2012, 7:25 AM
I am assuming you are intending posart to be your root and not specifying it in your reader.
Ops, thank you, I made a mistake, the correct php code is:


$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);
}

My question remains alive...

Tim Toady
25 Jun 2012, 7:32 AM
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?

Dafram
25 Jun 2012, 7:38 AM
I can synthesize my problem so:

if I make a JSON grid store with this php code:



$esito[] = array();
echo json_encode($esito);


I don't see an empty grid, but I see a grid with 1 (empty) record like this:

36512

Why?

:-/

Dafram
25 Jun 2012, 7:44 AM
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?

If I make the echo of json_encode of an empty php array I have BLANK output (blank string).

See firebug:

36513

Tim Toady
25 Jun 2012, 7:48 AM
$esito[] = array();

This should create a record. It is a 2 dimensional array. If there are no records it should be a single empty array.

--edit--

Just to clarify what you want for when there are no records is

$esito = array();

Dafram
25 Jun 2012, 8:01 AM
Thank you very much! Now it works:


<?php
$esito = array();
echo json_encode($esito);
?>

Here a live example: http://www.playersmagazine.it/smeraldo/empty_grid.html

Tim Toady
25 Jun 2012, 8:03 AM
Your json coming back is

[[]]

Is should be the following if the grid is empty.

[]

Dafram
25 Jun 2012, 11:38 PM
Thanks!