PDA

View Full Version : How display child model in same grid as parent?



bryan10
13 Jan 2012, 12:41 PM
Is it possible to display the child model instance in a model association in
the same grid as the parent model instance?


I am returning using a json proxy reader records in a parent-child relationship.


e.g.
{"data":[{"id":1,"description":"test1", "sub":[{"id":5,"amount":50}]}],"totrecs":1}


I also have defined parent & child models and grid as below - (stores are also defined):


The parent values are displayed correctly in the grid - the child values are not
displayed in the grid. Is there a way of displaying the child values?
Thank you,
bryan



Ext.define('Parent', {
extend: 'Ext.data.Model',


fields: [
{name: 'id', type: 'long'},
{name: 'description', type: 'string'}
],
...
hasMany: { model: 'Child', name: 'sub' }

});


Ext.define('Child', {
extend: 'Ext.data.Model',


fields: [
{name: 'id', type: 'long'},
{name: 'amount', type: 'int'}
],
...
belongsTo: 'Parent'
});


A grid is defined as follows:
Ext.define('GridTest' ,{
extend: 'Ext.grid.Panel',
store:...


initComponent: function() {

this.columns = [
{header: 'Parent Id', dataIndex: 'id', flex: 1},
{header: 'Description', dataIndex: 'description', flex: 1},
{header: 'Child Id', dataIndex: 'sub.id', flex: 1},
{header: 'Child Amount', dataIndex: 'sub.amount', flex: 1},
...
]
...

mitchellsimoens
13 Jan 2012, 12:46 PM
Not out of the box.

kskrumpet
14 Jan 2012, 1:04 PM
Yeah, you need to do a lot of fudging of the model so that it can display the multiple children into a single field of the parent model. I've written some code to do it in the past, but in the end I decided that in fact it was totally the wrong approach to take for most problems - it's far better to show the child data properly. I couldn't think of a reason to need to do this in the first place.

skirtle
14 Jan 2012, 6:02 PM
There's no support for this in dataIndex but couldn't you do it pretty simply using a renderer on the column? The renderer function gets passed the record, use that to grab the store of children then return whatever text you want based on the records in that store. If it's a one-to-one relationship then that's no problem, for multiple children you'll need to decide exactly how you want to represent them in a single cell.