PDA

View Full Version : Ext.ux.RowExpander does't work



xingcy
11 Aug 2011, 7:15 PM
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.

loiane
16 Aug 2011, 8:44 AM
Please take a look at this example:

http://dev.sencha.com/deploy/ext-4.0.2a/examples/grid/grid-plugins.html

redraid
17 Aug 2011, 5:15 AM
I`m fix errors with 4.0.2a and add ability to load data for rowBodyTpl via AJAX.
Plugin:

/*

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 &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() {
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;
}
}
};
}
});

Example:
Grid plugins:


plugins: [{
ptype: 'rowexpander',
urlTpl: 'info.php?id={company}',
rowBodyTpl : [
'<p><b>Info:</b> {info}</p><br>',
'<p><b>Foo:</b> {foo}</p>'
]
}],

Server:


$id = $_GET['id'];
echo json_encode(array(
'info' => 'Info from server ID:'.$id,
'foo' => 'bar'
));

redraid
18 Aug 2011, 12:19 AM
fixed errors when add plugin in Ext.define(....);
PS.
plugin not works if added in initComponent function.

/*

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 &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.
*/

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

pmachner
24 Aug 2011, 10:26 PM
It seems to me that this is a duplicate of this thread:

http://www.sencha.com/forum/showthread.php?137418-RowExpander-doesn-t-work-on-ExtJs-4.0.2-(ext-all-debug.js)-but-worked-on-(ext-all.js)

daiei27
2 Sep 2011, 9:22 AM
Also, for anyone starting from the original RowExpander plugin, I'd like to point out this thread:
http://www.sencha.com/forum/showthread.php?96245-3.x-3.2.0-RowExpander-plugin-UPDATED-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.

xingcy
5 Sep 2011, 4:55 AM
thanks all!

http://www.sencha.com/forum/showthread.php?137418-RowExpander-doesn-t-work-on-ExtJs-4.0.2-(ext-all-debug.js)-but-worked-on-(ext-all.js


it works for me.:)

(http://www.sencha.com/forum/showthread.php?137418-RowExpander-doesn-t-work-on-ExtJs-4.0.2-(ext-all-debug.js)-but-worked-on-(ext-all.js)