PDA

View Full Version : Ext.ux.grid.feature.RemoteSummary



BulletzBill
15 Dec 2011, 11:30 AM
Unlike the grid GroupingSummary feature, the grid Summary feature does not have the option to use remote, server-side calculated summaries, so I just extended the feature, duplicating GroupingSummary's remote summary functionality. Hope this helps anyone that was also looking for this!


/**
* Grid RemoteSummary feature
*
* Adds server-side summary calculation support to the Grid Summary,
* that is identical in functionality to Ext.grid.feature.GroupingSummary
*
* Set the remoteRoot property in the feature's config to use remote calculated summaries
* For more info, see: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.feature.GroupingSummary-cfg-remoteRoot
*
* @class Ext.ux.grid.feature.RemoteSummary
* @extends Ext.grid.feature.Summary
* @author Bill Dami
*/
Ext.define('Ext.ux.grid.feature.RemoteSummary', {
extend: 'Ext.grid.feature.Summary',
id: 'remotesummary',
alias: 'feature.remotesummary',
generateSummaryData: function() {
var me = this,
data ={},
remoteData ={},
store = me.view.store,
reader = store.proxy.reader,
columns = me.view.headerCt.getColumnsForTpl(),
i = 0,
length = columns.length,
root, comp;
if (me.remoteRoot && reader.rawData) {
// reset reader root and rebuild extractors to extract summaries data
root = reader.root;
reader.root = me.remoteRoot;
reader.buildExtractors(true);
remoteData = reader.getRoot(reader.rawData);
// restore initial reader configuration
reader.root = root;
reader.buildExtractors(true);
}
for (i = 0, length = columns.length; i < length; ++i) {
comp = Ext.getCmp(columns[i].id);
data[comp.id] = me.getSummary(store, comp.summaryType, comp.dataIndex, false);
if (remoteData.hasOwnProperty(comp.dataIndex)) {
data[comp.id] = remoteData[comp.dataIndex];
}
}
return data;
}
});

mitchellsimoens
16 Dec 2011, 8:08 AM
Here is a little formatting. In your for loop, couldn't comp just be columns[i] ?


Ext.define('Ext.ux.grid.feature.RemoteSummary', {
extend : 'Ext.grid.feature.Summary',
alias : 'feature.remotesummary',

generateSummaryData: function() {
var me = this,
data = {},
remoteData = {},
store = me.view.store,
reader = store.proxy.reader,
columns = me.view.headerCt.getColumnsForTpl(),
i = 0,
length = columns.length,
root, comp;

if (me.remoteRoot && reader.rawData) {
// reset reader root and rebuild extractors to extract summaries data
root = reader.root;
reader.root = me.remoteRoot;

reader.buildExtractors(true);

remoteData = reader.getRoot(reader.rawData);

// restore initial reader configuration
reader.root = root;
reader.buildExtractors(true);
}

for (; i < length; i++) {
comp = columns[i];
data[comp.id] = me.getSummary(store, comp.summaryType, comp.dataIndex, false);

if (remoteData.hasOwnProperty(comp.dataIndex)) {
data[comp.id] = remoteData[comp.dataIndex];
}
}

return data;
}
});

BulletzBill
16 Dec 2011, 8:13 AM
Thanks mitchell, and yeah, looks it could be. I admittedly didn't spend much time on making the code nice and clean, just threw this together really fast to accomplish what I needed.

mitchellsimoens
16 Dec 2011, 8:42 AM
Thanks mitchell, and yeah, looks it could be. I admittedly didn't spend much time on making the code nice and clean, just threw this together really fast to accomplish what I needed.

To be frank... I started getting a lot of traction once I made code readability a top priority (and documentation :D ). Putting out code for people to use that isn't readable or optimized isn't a great practice... I've had to redo classes I have made to be readable and I usually found bugs or further optimizations. DOn't be afraid of whitespaces or new lines... when someone compiles it, all that will be stripped out.

BulletzBill
16 Dec 2011, 9:37 AM
To be frank... I started getting a lot of traction once I made code readability a top priority (and documentation :D ). Putting out code for people to use that isn't readable or optimized isn't a great practice... I've had to redo classes I have made to be readable and I usually found bugs or further optimizations. DOn't be afraid of whitespaces or new lines... when someone compiles it, all that will be stripped out.
Good point, I'll be sure to keep that in mind for any other potential extensions/plugins I post. :)