3
votes

I have a php-script, which returns the following JSON

[
   {
      "Code":0,
      "Message":"No problem"
   },
   {
      "name":"o016561",
      "status":1,
      "locks":[
         {
            "ztn":"155320",
            "dtn":"20131111",
            "idn":"78"
         },
         {
            "ztn":"155320",
            "dtn":"20131111",
            "idn":"91"
         }
      ]
   },
   {
      "name":"o011111",
      "status":1,
      "locks":[
         {
            "ztn":"155320",
            "dtn":"20131111",
            "idn":"91"
         }
      ]
   },
   {
      "name":"o019999",
      "status":0,
      "locks":[

      ]
   },
   {
      "name":"o020000",
      "status":0,
      "locks":[

      ]
   },
   {
      "name":"o020001",
      "status":0,
      "locks":[

      ]
   }
]

Edit: The grid should look something like this:

a bit smal...

I've been able to load name and status into my grid - so far so good. But the more important part is, that I need the nested data in the locks-array being loaded into my grid, but I just can't get my code working. Any help would be appreciated.

I'm using ExtJS 4.2 if that matters.

Edit 2:

I tried

Ext.define("Locks", {
    extend: 'Ext.data.Model',
    fields: [
        'ztn',
        'dtn',
        'idn'
    ]
});

Ext.define("ConnectionModel", {
    extend: 'Ext.data.Model',
    fields: ['name', 'status'],
    hasMany: [{
        model: 'Locks',
        name: 'locks'
    }]
});

var store = Ext.create('Ext.data.Store', {
    model: "ConnectionModel",
    autoLoad: true,
    proxy: {
        type: 'ajax',
        reader: {
            type: 'json',
            root: 'name'
        }
    }
});

but it seemed to be wrong in multiple ways... and it would be awesome if ztn and dtn could be displayed just seperated with a whitespace in the same column

1
How should the grid look like? What columns? - Shlomo
@Indianer I just edited the original post - p4b4
put your model definition in here.. - Oğuz Çelikdemir
you have an array of items possible in locks however in your grid it shows ztn + dtn, are you wanting to sum all corresponding values from the array into that field? - weeksdev

1 Answers

4
votes

You can add a renderer to the column. In that renderer you can do anything with the record...

Here's a working fiddle: http://jsfiddle.net/Vandeplas/MWeGa/3/

Ext.create('Ext.grid.Panel', {
    title: 'test',
    store: store,
    columns: [{
        text: 'Name',
        dataIndex: 'name'
    }, {
        text: 'Status',
        dataIndex: 'status'
    }, {
        text: 'idn',
        renderer: function (value, metaData, record, rowIdx, colIdx, store, view) {
            values = [];
            record.locks().each(function(lock){
                values.push(lock.get('idn'));
            });
            return values.join('<br\>');
        }
    }, {
        text: 'ztn + dtn',
        renderer: function (value, metaData, record, rowIdx, colIdx, store, view) {
            values = [];
            record.locks().each(function(lock){
                values.push(lock.get('ztn') + ' ' + lock.get('dtn'));
            });
            return values.join('<br\>');
        }
    }],
    height: 200,
    width: 600,
    renderTo: Ext.getBody()
});

Note

If you have control over your backend you better change the form of your data more like this:

{
    "code": 0,
    "message": "No problem",
    "success": true,
    "data": [
        {
            "name": "o016561",
            "status": 1,
            "locks": [
                {
                    "ztn": "155320",
                    "dtn": "20131111",
                    "idn": "78"
                },
                {
                    "ztn": "155320",
                    "dtn": "20131111",
                    "idn": "91"
                }
            ]
        },
        {
            "name": "o011111",
            "status": 1,
            "locks": [
                {
                    "ztn": "155320",
                    "dtn": "20131111",
                    "idn": "91"
                }
            ]
        }
    ]
}

That way you don't mix your control data (success, message, code,...) with your data and the proxy picks it up correctly (it can be a cause of the problems your experiencing). I added a success boolean => Ext picks it up and goes to the failure handler. It helps a lot with your exception handling.

Here is the proxy for it:

proxy: {
    type: 'ajax',
        api: {
        read: ___URL____
    },
    reader: {
        type: 'json',
        root: 'data',
        messageProperty: 'message'
    }
}