PDA

View Full Version : Nested Grid using rowexpander in extjs 4



renganathan
15 Oct 2011, 2:14 AM
Is anybody done nested grid option using rowexpander plugin? if yes please share the source code or example link ......

akiles83
15 Oct 2011, 5:22 AM
Have multiple solutions:

http://docs.sencha.com/ext-js/4-0/#!/example/grid/grid-plugins.html
http://docs.sencha.com/ext-js/4-0/#!/example/grid/group-summary-grid.html
http://docs.sencha.com/ext-js/4-0/#!/example/grid/groupgrid.html
http://docs.sencha.com/ext-js/4-0/#!/example/tree/treegrid.html

Only select the better solution for your project

I hope you help

renganathan
16 Oct 2011, 3:43 AM
Hi ,

Thanks for your suggestions. I want to implement nested grid by using rowexpander plugin only.

any suggestions?

stuchy
14 Nov 2011, 11:33 PM
I took this from my implementation - means: not tested standalone. I hope this helps.
I dont think this is the superb solution. I nearly read all posts related to nested grids and created this.
This is more a subrow plugin than a real nested grid with different column model.


Ext.define('Ext.ux.grid.ExpandPanel', {
extend: 'Ext.grid.Panel',
alias: 'widget.expandpanel',
plugins: [{
ptype: 'rowexpander',
rowBodyTpl: ['<div class="ux-row-expander-box"></div>'],
expandOnRender: true,
expandOnDblClick: false
}],
initComponent: function() {
var me = this;
var columns = [];
var fields = [];
Ext.each(me.columns, function(col, index) {
if (col.dataIndex) {
var column = Ext.clone(col);
var dataIndex = col.dataIndex;
var entity = dataIndex.split('.');
if (entity.length == 2 && entity[0] == me.model) {
dataIndex = entity[1];
Ext.apply(column, {
dataIndex: dataIndex
});
}
fields.push(dataIndex);
columns.push(column);
}
});
me.callParent(arguments);

me.getView().on('expandbody', function(node, record, eNode) {
var element = Ext.get(eNode).down('.ux-row-expander-box');
// alter the fields (swap the model data index with the child data index)
var store = Ext.create('Ext.data.Store', {
fields: fields,
data: record.data[me.expandable.dataIndex]
});

var grid = Ext.create('Ext.grid.Panel', {
model: me.model,
hideHeaders: true,
border: false,
columns: columns,
store: store,
});
Ext.apply(grid, me.expandable);
grid.on('itemclick', function(view) {
me.getView().getSelectionModel().deselectAll();
});

element.swallowEvent(['click', 'mousedown', 'mouseup', 'dblclick'], true);
grid.render(element);
});
me.getView().on('collapsebody', function(node, record, eNode) {
Ext.get(eNode).down('.ux-row-expander-box').down('div').destroy();
});
}
});

This is the default RowExpander Plugin with additional expandRow and collapseRow


/*

This file is part of Ext JS 4

Copyright (c) 2011 Sencha Inc

Contact: http://www.sencha.com/contact

GNU General Public License Usage
This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.

If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.

*/
// feature idea to enable Ajax loading and then the content
// cache would actually make sense. Should we dictate that they use
// data or support raw html as well?

/**
* @class Ext.ux.RowExpander
* @extends Ext.AbstractPlugin
* Plugin (ptype = 'rowexpander') that adds the ability to have a Column in a grid which enables
* a second row body which expands/contracts. The expand/contract behavior is configurable to react
* on clicking of the column, double click of the row, and/or hitting enter while a row is selected.
*
* @ptype rowexpander
*/
Ext.define('Ext.ux.RowExpander', {
extend: 'Ext.AbstractPlugin',

requires: [
'Ext.grid.feature.RowBody',
'Ext.grid.feature.RowWrap'
],

alias: 'plugin.rowexpander',

rowBodyTpl: null,

expandOnRender: false,

/**
* @cfg {Boolean} expandOnEnter
* <tt>true</tt> to toggle selected row(s) between expanded/collapsed when the enter
* key is pressed (defaults to <tt>true</tt>).
*/
expandOnEnter: true,

/**
* @cfg {Boolean} expandOnDblClick
* <tt>true</tt> to toggle a row between expanded/collapsed when double clicked
* (defaults to <tt>true</tt>).
*/
expandOnDblClick: true,

/**
* @cfg {Boolean} selectRowOnExpand
* <tt>true</tt> to select a row when clicking on the expander icon
* (defaults to <tt>false</tt>).
*/
selectRowOnExpand: false,

rowBodyTrSelector: '.x-grid-rowbody-tr',
rowBodyHiddenCls: 'x-grid-row-body-hidden',
rowCollapsedCls: 'x-grid-row-collapsed',



renderer: function(value, metadata, record, rowIdx, colIdx) {
if (colIdx === 0) {
metadata.tdCls = 'x-grid-td-expander';
}
return '<div class="x-grid-row-expander"> </div>';
},

/**
* @event expandbody
* <b<Fired through the grid's View</b>
* @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
*/
/**
* @event collapsebody
* <b<Fired through the grid's View.</b>
* @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
*/

constructor: function() {
this.callParent(arguments);
var grid = this.getCmp();
this.recordsExpanded = {};
// <debug>
if (!this.rowBodyTpl) {
Ext.Error.raise("The 'rowBodyTpl' config is required and is not defined.");
}
// </debug>
// TODO: if XTemplate/Template receives a template as an arg, should
// just return it back!
var rowBodyTpl = Ext.create('Ext.XTemplate', this.rowBodyTpl),
features = [{
ftype: 'rowbody',
columnId: this.getHeaderId(),
recordsExpanded: this.recordsExpanded,
rowBodyHiddenCls: this.rowBodyHiddenCls,
rowCollapsedCls: this.rowCollapsedCls,
getAdditionalData: this.getRowBodyFeatureData,
getRowBodyContents: function(data) {
return rowBodyTpl.applyTemplate(data);
}
},{
ftype: 'rowwrap'
}];

if (grid.features) {
grid.features = features.concat(grid.features);
} else {
grid.features = features;
}

// NOTE: features have to be added before init (before Table.initComponent)
},

init: function(grid) {
this.callParent(arguments);

// Columns have to be added in init (after columns has been used to create the
// headerCt). Otherwise, shared column configs get corrupted, e.g., if put in the
// prototype.
grid.headerCt.insert(0, this.getHeaderConfig());
grid.on('render', this.bindView, this, {single: true});
},

getHeaderId: function() {
if (!this.headerId) {
this.headerId = Ext.id();
}
return this.headerId;
},

getRowBodyFeatureData: function(data, idx, record, orig) {
var o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments),
id = this.columnId;
o.rowBodyColspan = o.rowBodyColspan - 1;
o.rowBody = this.getRowBodyContents(data);
o.rowCls = this.recordsExpanded[record.internalId] ? '' : this.rowCollapsedCls;
o.rowBodyCls = this.recordsExpanded[record.internalId] ? '' : this.rowBodyHiddenCls;
o[id + '-tdAttr'] = ' valign="top" rowspan="2" ';
if (orig[id+'-tdAttr']) {
o[id+'-tdAttr'] += orig[id+'-tdAttr'];
}
return o;
},

bindView: function() {
var me = this;
var view = this.getCmp().getView(),
viewEl;

if (!view.rendered) {
view.on('render', this.bindView, this, {single: true});
if (this.expandOnRender) {
this.getCmp().getStore().on('load', function() {
for (var i = 0; i < this.getCount(); i++) {
me.expandRow(i);
}
});
}
} else {
viewEl = view.getEl();
if (this.expandOnEnter) {
this.keyNav = Ext.create('Ext.KeyNav', viewEl, {
'enter' : this.onEnter,
scope: this
});
}
if (this.expandOnDblClick) {
view.on('itemdblclick', this.onDblClick, this);
}
this.view = view;
}
},

onEnter: function(e) {
var view = this.view,
ds = view.store,
sm = view.getSelectionModel(),
sels = sm.getSelection(),
ln = sels.length,
i = 0,
rowIdx;

for (; i < ln; i++) {
rowIdx = ds.indexOf(sels[i]);
this.toggleRow(rowIdx);
}
},

expandRow: function(rowIdx) {
var rowNode = this.view.getNode(rowIdx),
row = Ext.get(rowNode),
nextBd = Ext.get(row).down(this.rowBodyTrSelector),
record = this.view.getRecord(rowNode),
grid = this.getCmp();
if (row.hasCls(this.rowCollapsedCls)) {
row.removeCls(this.rowCollapsedCls);
nextBd.removeCls(this.rowBodyHiddenCls);
this.recordsExpanded[record.internalId] = true;
this.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
}
},

collapseRow: function(rowIdx) {
var rowNode = this.view.getNode(rowIdx),
row = Ext.get(rowNode),
nextBd = Ext.get(row).down(this.rowBodyTrSelector),
record = this.view.getRecord(rowNode),
grid = this.getCmp();
if (!row.hasCls(this.rowCollapsedCls)) {
row.addCls(this.rowCollapsedCls);
nextBd.addCls(this.rowBodyHiddenCls);
this.recordsExpanded[record.internalId] = false;
this.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
}
},

toggleRow: function(rowIdx) {
var rowNode = this.view.getNode(rowIdx),
row = Ext.get(rowNode),
nextBd = Ext.get(row).down(this.rowBodyTrSelector),
record = this.view.getRecord(rowNode),
grid = this.getCmp();

if (row.hasCls(this.rowCollapsedCls)) {
row.removeCls(this.rowCollapsedCls);
nextBd.removeCls(this.rowBodyHiddenCls);
this.recordsExpanded[record.internalId] = true;
this.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
} else {
row.addCls(this.rowCollapsedCls);
nextBd.addCls(this.rowBodyHiddenCls);
this.recordsExpanded[record.internalId] = false;
this.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
}


// If Grid is auto-heighting itself, then perform a component layhout to accommodate the new height
if (!grid.isFixedHeight()) {
grid.doComponentLayout();
}
this.view.up('gridpanel').invalidateScroller();
},

onDblClick: function(view, cell, rowIdx, cellIndex, e) {

this.toggleRow(rowIdx);
},

getHeaderConfig: function() {
var me = this,
toggleRow = Ext.Function.bind(me.toggleRow, me),
selectRowOnExpand = me.selectRowOnExpand;

return {
id: this.getHeaderId(),
width: 24,
sortable: false,
resizable: false,
draggable: false,
hideable: false,
menuDisabled: true,
cls: Ext.baseCSSPrefix + 'grid-header-special',
renderer: function(value, metadata) {
metadata.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';

return '<div class="' + Ext.baseCSSPrefix + 'grid-row-expander"> </div>';
},
processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
if (type == "mousedown" && e.getTarget('.x-grid-row-expander')) {
var row = e.getTarget('.x-grid-row');
toggleRow(row);
return selectRowOnExpand;
}
}
};
}
});

oturkan
12 Jun 2013, 12:29 PM
Hi,

i'm using 4.2.1. nextbd is getting null. what can i do

riyash
22 Jan 2014, 9:33 PM
am getting nextBd is null on double click of toggleRow. Is there any fix for that?