1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default 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:

    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
        });
    MySQL Table structure:
    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    |       |
    +----------+---------------+------+-----+---------+-------+
    storeRegole.php:
    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); 
    If my table (regole) is empy, I see my grid with 1 empty record:

    err.jpg
    I suppose it is for:

    PHP Code:
    if ($n == 0)
        
    $esito[] = array(); 
    but if I don't use that php code, I have the same result.

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

    Suggestions?

    Thanks in advance.

  2. PHP Code:
    $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
    PHP Code:
    $esito = array(); 

  3. #2
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    580
    Vote Rating
    269
    Answers
    62
    Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of

      1  

    Default


    edit--
    Retracted my first post after looking at your php some more.

  4. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default


    Quote Originally Posted by Tim Toady View Post
    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:

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

    My question remains alive...

  5. #4
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    580
    Vote Rating
    269
    Answers
    62
    Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of

      1  

    Default


    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?

  6. #5
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default


    I can synthesize my problem so:

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

    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:

    err.jpg

    Why?


  7. #6
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default


    Quote Originally Posted by Tim Toady View Post
    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:

    firebug.png

  8. #7
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    580
    Vote Rating
    269
    Answers
    62
    Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of

      1  

    Default


    PHP Code:
    $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
    PHP Code:
    $esito = array(); 

  9. #8
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default


    Thank you very much! Now it works:

    PHP Code:
    <?php
    $esito 
    = array();
    echo 
    json_encode($esito);
    ?>
    Here a live example: http://www.playersmagazine.it/smeraldo/empty_grid.html

  10. #9
    Sencha User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    580
    Vote Rating
    269
    Answers
    62
    Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of Tim Toady has much to be proud of

      1  

    Default


    Your json coming back is
    PHP Code:
    [[]] 
    Is should be the following if the grid is empty.
    PHP Code:
    [] 

  11. #10
    Sencha User
    Join Date
    Feb 2012
    Posts
    30
    Vote Rating
    1
    Answers
    2
    Dafram is on a distinguished road

      0  

    Default


    Thanks!

Thread Participants: 1

Tags for this Thread