-
11 Aug 2011 7:15 PM #1
Ext.ux.RowExpander does't work
Ext.ux.RowExpander does't work
i use Ext.ux.RowExpander in Ext4 ,
but got an error,
" var rowNode = this.view.getNode(rowIdx)",
the rowNode is null,
so the ux doesn't work.
can anybody give me some advice! thanks.
-
16 Aug 2011 8:44 AM #2
Sencha/Java evangelist
Author of ExtJS 4 First Look and Mastering Ext JS books
English blog: http://loianegroner.com
Portuguese blog: http://loiane.com
Sencha Examples: https://github.com/loiane
-
17 Aug 2011 5:15 AM #3
I`m fix errors with 4.0.2a and add ability to load data for rowBodyTpl via AJAX.
Plugin:
Example:PHP Code:/*
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?
// Sergei Pochtovyi:
// fixed error with ExtJS 4.0.2a
// add AJAX data load
/**
* @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.grid.RowExpander', {
extend: 'Ext.AbstractPlugin',
alias: 'plugin.rowexpander',
rowBodyTpl: null,
urlTpl: null,
/**
* @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',
rowBodyDivSelector: '.x-grid-rowbody',
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 <tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The <tr> element containing the expanded data.
*/
/**
* @event collapsebody
* <b<Fired through the grid's View.</b>
* @param {HtmlElement} rowNode The <tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The <tr> element containing the expanded data.
*/
constructor: function() {
var me = this,
grid;
me.callParent(arguments);
grid = me.grid = this.getCmp();
me.recordsExpanded = {};
if (me.urlTpl) {
me.recordsLoaded = {};
}
// <debug>
if (!me.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!
me.urlXTpl = Ext.create('Ext.XTemplate', me.urlTpl);
var rowBodyTpl = me.rowBodyXTpl = Ext.create('Ext.XTemplate', me.rowBodyTpl),
features = [
{
ftype: 'rowbody',
columnId: me.getHeaderId(),
recordsExpanded: me.recordsExpanded,
rowBodyHiddenCls: me.rowBodyHiddenCls,
rowCollapsedCls: me.rowCollapsedCls,
getAdditionalData: me.getRowBodyFeatureData,
getRowBodyContents: function(data, record) {
if (me.urlTpl) {
return '';
} else {
return rowBodyTpl.applyTemplate(data);
}
}
},
{
ftype: 'rowwrap'
}
];
if (grid.features) {
grid.features = features.concat(grid.features);
} else {
grid.features = features;
}
grid.columns.unshift(me.getHeaderConfig());
grid.on('afterrender', me.onGridAfterRender, me, {single: true});
},
getHeaderId: function() {
var me = this;
if (!me.headerId) {
me.headerId = Ext.id();
}
return me.headerId;
},
getRowBodyFeatureData: function(data, idx, record, orig) {
var me = this,
o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments),
id = me.columnId;
o.rowBodyColspan = o.rowBodyColspan - 1;
o.rowBody = me.getRowBodyContents(data, record);
o.rowCls = me.recordsExpanded[record.internalId] ? '' : me.rowCollapsedCls;
o.rowBodyCls = me.recordsExpanded[record.internalId] ? '' : me.rowBodyHiddenCls;
o[id + '-tdAttr'] = ' valign="top" rowspan="2" ';
if (orig[id+'-tdAttr']) {
o[id+'-tdAttr'] += orig[id+'-tdAttr'];
}
return o;
},
onGridAfterRender: function(grid, options) {
var me = this,
view = me.view = me.grid.view;
viewEl = view.getEl();
if (me.expandOnEnter) {
me.keyNav = Ext.create('Ext.KeyNav', viewEl, {
'enter' : me.onEnter,
scope: me
});
}
if (me.expandOnDblClick) {
view.on('itemdblclick', me.onDblClick, me);
}
},
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);
}
},
loadBodyAjax: function(rowNode, record, rowBody, el)
{
var me = this,
url = me.urlXTpl.applyTemplate(record.data);
Ext.Ajax.request({
url: url,
success: function (response) {
var data = Ext.JSON.decode(response.responseText);
rowBody.update(me.rowBodyXTpl.applyTemplate(data));
me.recordsLoaded[record.internalId] = true;
me.view.fireEvent('expandbody', rowNode, record, el);
}
});
},
toggleRow: function(rowIdx) {
var me = this,
rowNode = me.view.getNode(rowIdx),
row = Ext.get(rowNode),
nextBd = Ext.get(row).down(me.rowBodyTrSelector),
rowBody = Ext.get(row).down(me.rowBodyDivSelector),
record = me.view.getRecord(rowNode);
if (row.hasCls(this.rowCollapsedCls)) {
row.removeCls(this.rowCollapsedCls);
nextBd.removeCls(this.rowBodyHiddenCls);
me.recordsExpanded[record.internalId] = true;
if (me.urlTpl && !me.recordsLoaded[record.internalId]) {
me.loadBodyAjax(rowNode, record, rowBody, nextBd.dom);
} else {
me.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
}
} else {
row.addCls(this.rowCollapsedCls);
nextBd.addCls(this.rowBodyHiddenCls);
me.recordsExpanded[record.internalId] = false;
me.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
}
me.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,
fixed: true,
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;
}
}
};
}
});
Grid plugins:
Server:PHP Code:plugins: [{
ptype: 'rowexpander',
urlTpl: 'info.php?id={company}',
rowBodyTpl : [
'<p><b>Info:</b> {info}</p><br>',
'<p><b>Foo:</b> {foo}</p>'
]
}],
PHP Code:$id = $_GET['id'];
echo json_encode(array(
'info' => 'Info from server ID:'.$id,
'foo' => 'bar'
));
-
18 Aug 2011 12:19 AM #4
fixed errors when add plugin in Ext.define(....);
PS.
plugin not works if added in initComponent function.
PHP Code:/*
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?
// Sergei Pochtovyi:
// fixed error with ExtJS 4.0.2a
// fixed errors in defined grid
// add AJAX data load
/**
* @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',
alias: 'plugin.rowexpander',
uses: [
'Ext.grid.feature.RowBody',
'Ext.grid.feature.RowWrap'
],
rowBodyTpl: null,
urlTpl: null,
/**
* @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',
rowBodyDivSelector: '.x-grid-rowbody',
rowBodyHiddenCls: 'x-grid-row-body-hidden',
rowCollapsedCls: 'x-grid-row-collapsed',
/**
* @event expandbody
* <b<Fired through the grid's View</b>
* @param {HtmlElement} rowNode The <tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The <tr> element containing the expanded data.
*/
/**
* @event collapsebody
* <b<Fired through the grid's View.</b>
* @param {HtmlElement} rowNode The <tr> element which owns the expanded row.
* @param {Ext.data.Model} record The record providing the data.
* @param {HtmlElement} expandRow The <tr> element containing the expanded data.
*/
renderer: function(value, metadata, record, rowIdx, colIdx) {
if (colIdx === 0) {
metadata.tdCls = 'x-grid-td-expander';
}
return '<div class="x-grid-row-expander"> </div>';
},
init: function(grid) {
var me = this;
// <debug>
if (!me.rowBodyTpl) {
Ext.Error.raise("The 'rowBodyTpl' config is required and is not defined.");
}
// </debug>
me.callParent(arguments);
me.view = grid.view;
me.grid.view.on('render', me.bindEvents, me, {single: true});
},
constructor: function(config) {
//<debug>
if (!config.cmp && Ext.global.console) {
Ext.global.console.warn("Attempted to attach a plugin ");
}
//</debug>
Ext.apply(this, config);
this.recordsExpanded = {};
var me = this,
rowBodyTpl = me.rowBodyXTpl = Ext.create('Ext.XTemplate', me.rowBodyTpl),
features = [
{
ftype: 'rowbody',
columnId: me.getHeaderId(),
recordsExpanded: me.recordsExpanded,
rowBodyHiddenCls: me.rowBodyHiddenCls,
rowCollapsedCls: me.rowCollapsedCls,
getAdditionalData: me.getRowBodyFeatureData,
getRowBodyContents: function(data, record) {
if (me.urlTpl) {
return '';
} else {
return rowBodyTpl.applyTemplate(data);
}
}
},
{
ftype: 'rowwrap'
}
];
me.grid = me.getCmp();
me.urlXTpl = Ext.create('Ext.XTemplate', me.urlTpl);
if (me.urlTpl) {
me.recordsLoaded = {};
}
if (me.grid.features) {
me.grid.features = features.concat(me.grid.features);
} else {
me.grid.features = features;
}
if (me.grid.columns) {
me.addExpanderColumn();
} else {
me.grid.on('render', me.addExpanderColumn, me, {single: true});
}
},
destroy: function() {
delete me.recordsExpanded;
delete me.recordsLoaded;
delete me.rowBodyXTpl;
delete me.urlXTpl;
},
bindEvents: function() {
var me = this,
view = me.view,
viewEl = view.getEl();
if (me.expandOnEnter) {
me.keyNav = Ext.create('Ext.KeyNav', viewEl, {
'enter' : me.onEnter,
scope: me
});
}
if (me.expandOnDblClick) {
view.on('itemdblclick', me.onDblClick, me);
}
},
addExpanderColumn: function() {
this.grid.columns.unshift(this.getExpanderColumn());
},
getHeaderId: function() {
var me = this;
if (!me.headerId) {
me.headerId = Ext.id();
}
return me.headerId;
},
getRowBodyFeatureData: function(data, idx, record, orig) {
var me = this,
o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments),
id = me.columnId;
o.rowBodyColspan = o.rowBodyColspan - 1;
o.rowBody = me.getRowBodyContents(data, record);
o.rowCls = me.recordsExpanded[record.internalId] ? '' : me.rowCollapsedCls;
o.rowBodyCls = me.recordsExpanded[record.internalId] ? '' : me.rowBodyHiddenCls;
o[id + '-tdAttr'] = ' valign="top" rowspan="2" ';
if (orig[id+'-tdAttr']) {
o[id+'-tdAttr'] += orig[id+'-tdAttr'];
}
return o;
},
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);
}
},
loadBodyAjax: function(rowNode, record, rowBody, el)
{
var me = this,
url = me.urlXTpl.applyTemplate(record.data);
Ext.Ajax.request({
url: url,
success: function (response) {
var data = Ext.JSON.decode(response.responseText);
rowBody.update(me.rowBodyXTpl.applyTemplate(data));
me.recordsLoaded[record.internalId] = true;
me.view.fireEvent('expandbody', rowNode, record, el);
}
});
},
toggleRow: function(rowIdx) {
var me = this,
rowNode = me.view.getNode(rowIdx),
row = Ext.get(rowNode),
nextBd = Ext.get(row).down(me.rowBodyTrSelector),
rowBody = Ext.get(row).down(me.rowBodyDivSelector),
record = me.view.getRecord(rowNode);
if (row.hasCls(me.rowCollapsedCls)) {
row.removeCls(me.rowCollapsedCls);
nextBd.removeCls(me.rowBodyHiddenCls);
me.recordsExpanded[me.internalId] = true;
if (me.urlTpl && !me.recordsLoaded[record.internalId]) {
me.loadBodyAjax(rowNode, record, rowBody, nextBd.dom);
} else {
me.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
}
} else {
row.addCls(me.rowCollapsedCls);
nextBd.addCls(me.rowBodyHiddenCls);
me.recordsExpanded[record.internalId] = false;
me.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
}
me.view.up('gridpanel').invalidateScroller();
},
onDblClick: function(view, cell, rowIdx, cellIndex, e) {
this.toggleRow(rowIdx);
},
getExpanderColumn: function() {
var me = this,
toggleRow = Ext.Function.bind(me.toggleRow, me),
selectRowOnExpand = me.selectRowOnExpand,
column = Ext.create('Ext.grid.column.Column', {
id: this.getHeaderId(),
width: 24,
sortable: false,
fixed: true,
draggable: false,
hideable: false,
ownerCt: me.grid.headerCt,
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;
}
}
});
return column;
}
});
-
24 Aug 2011 10:26 PM #5
It seems to me that this is a duplicate of this thread:
http://www.sencha.com/forum/showthre...on-(ext-all.js)
-
2 Sep 2011 9:22 AM #6
Consider using updated RowExpander
Consider using updated RowExpander
Also, for anyone starting from the original RowExpander plugin, I'd like to point out this thread:
http://www.sencha.com/forum/showthre...D-April-5-2010
Fixes memory leaks and such. If you want to take it from 3.x to 4.x, it would help the community to start from that. Please spread the word in any related RowExpander threads you may find.
I would update it to 4.x, but apparently 4.0.2a broke RowExpander and I want to wait for the ExtJS fix to be released rather than implementing a workaround.
-
5 Sep 2011 4:55 AM #7


Reply With Quote