|
|
@@ -1,45 +1,37 @@
|
|
|
Ext.define('Ext.ux.feature.MySummary', {
|
|
|
|
|
|
- /* Begin Definitions */
|
|
|
-
|
|
|
- extend: 'Ext.grid.feature.AbstractSummary',
|
|
|
+ extend: 'Ext.grid.feature.Summary',
|
|
|
|
|
|
alias: 'feature.mysummary',
|
|
|
|
|
|
- /**
|
|
|
- * @cfg {String} dock
|
|
|
- * Configure `'top'` or `'bottom'` top create a fixed summary row either above or below the scrollable table.
|
|
|
- *
|
|
|
- */
|
|
|
- dock: undefined,
|
|
|
+ dock: 'bottom',
|
|
|
|
|
|
- summaryItemCls: Ext.baseCSSPrefix + 'grid-row-mysummary-item',
|
|
|
dockedSummaryCls: Ext.baseCSSPrefix + 'docked-mysummary',
|
|
|
|
|
|
- summaryRowCls: Ext.baseCSSPrefix + 'grid-row-summary ' + Ext.baseCSSPrefix + 'grid-row-total',
|
|
|
- summaryRowSelector: '.' + Ext.baseCSSPrefix + 'grid-row-summary.' + Ext.baseCSSPrefix + 'grid-row-total',
|
|
|
-
|
|
|
showSummaryRow: true,
|
|
|
|
|
|
init: function(grid) {
|
|
|
var me = this,
|
|
|
view = me.view,
|
|
|
dock = me.dock,
|
|
|
- summarys = me.getSummarys(grid);
|
|
|
-
|
|
|
- me.callParent([grid]);
|
|
|
-
|
|
|
- grid.store.on('load', function() {
|
|
|
- this.fireEvent('mysummarychange', this);
|
|
|
- }, grid);
|
|
|
-
|
|
|
- grid.on('mysummarychange', function() {
|
|
|
- me.refreshData(grid);
|
|
|
+ store = grid.getStore();
|
|
|
+
|
|
|
+ me.grid = grid;
|
|
|
+
|
|
|
+ me.callParent([
|
|
|
+ grid
|
|
|
+ ]);
|
|
|
+ grid.headerCt.on({
|
|
|
+ add: me.onStoreUpdate,
|
|
|
+ afterlayout: me.onStoreUpdate,
|
|
|
+ remove: me.onStoreUpdate,
|
|
|
+ scope: me
|
|
|
});
|
|
|
-
|
|
|
grid.on({
|
|
|
beforerender: function() {
|
|
|
- var tableCls = [me.summaryTableCls];
|
|
|
+ var tableCls = [
|
|
|
+ me.summaryTableCls
|
|
|
+ ];
|
|
|
if (view.columnLines) {
|
|
|
tableCls[tableCls.length] = view.ownerCt.colLinesCls;
|
|
|
}
|
|
|
@@ -68,7 +60,7 @@ Ext.define('Ext.ux.feature.MySummary', {
|
|
|
'</tbody>',
|
|
|
'</table>',
|
|
|
],
|
|
|
- data: summarys,
|
|
|
+ data: me.getSummarys(),
|
|
|
height: 36,
|
|
|
scrollable: {
|
|
|
x: false,
|
|
|
@@ -82,14 +74,104 @@ Ext.define('Ext.ux.feature.MySummary', {
|
|
|
weight: 10000000
|
|
|
})[0];
|
|
|
},
|
|
|
+ afterrender: function() {
|
|
|
+ grid.getView().getScrollable().addPartner(me.summaryBar.getScrollable(), 'x');
|
|
|
+ me.onStoreUpdate();
|
|
|
+ me.columnSizer = me.summaryBar.el;
|
|
|
+ },
|
|
|
single: true
|
|
|
});
|
|
|
+
|
|
|
+ store.on('load', function( s, records, successful, operation, eOpts) {
|
|
|
+ var _res = operation._response.responseJson,
|
|
|
+ _calculateProperty = grid.calculateProperty,
|
|
|
+ _rootProperty = grid.rootProperty;
|
|
|
+
|
|
|
+ var _root = _calculateProperty.split('.')[0];
|
|
|
+ eval('var ' + _root + ' = _res[_root];');
|
|
|
+ try {
|
|
|
+ var columns = grid.columns,
|
|
|
+ summaryData = [],
|
|
|
+ datas = [];
|
|
|
+ try {
|
|
|
+ summaryData = eval(_calculateProperty);
|
|
|
+ datas = eval(_rootProperty);
|
|
|
+
|
|
|
+ if(!Ext.isArray(summaryData)) {
|
|
|
+ summaryData = [];
|
|
|
+ }
|
|
|
+ if(!Ext.isArray(datas)) {
|
|
|
+ datas = [];
|
|
|
+ }
|
|
|
+ }catch(e) {
|
|
|
+ // don't care this...
|
|
|
+ }
|
|
|
+ Ext.Array.each(columns, function (c) {
|
|
|
+ var type = c.summaryType,
|
|
|
+ name = c.dataIndex;
|
|
|
+
|
|
|
+ var d = Ext.Array.findBy(summaryData, function (s) {
|
|
|
+ return s.hasOwnProperty(name);
|
|
|
+ })
|
|
|
+ if (type && d) {
|
|
|
+ if(typeof c.mySummaryRenderer == 'function') {
|
|
|
+ c.summaryValue = c.mySummaryRenderer(grid, c, datas);
|
|
|
+ }else if (type == 'count') {
|
|
|
+ c.summaryValue = Ext.util.Format.number(d[name], c.format || '0');
|
|
|
+ } else if (typeof c.summaryRenderer == 'function') {
|
|
|
+ c.summaryValue = c.summaryRenderer(d[name]);
|
|
|
+ } else if (typeof c.renderer == 'function') {
|
|
|
+ c.summaryValue = c.renderer(d[name]);
|
|
|
+ } else {
|
|
|
+ c.summaryValue = Ext.util.Format.number(d[name], c.format || '0.00');
|
|
|
+ }
|
|
|
+ } else if(type && !d) {
|
|
|
+ if(typeof c.mySummaryRenderer == 'function') {
|
|
|
+ c.summaryValue = c.mySummaryRenderer(grid, c, datas);
|
|
|
+ }else {
|
|
|
+ c.summaryValue = me.getSummaryValue(datas, c);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ c.summaryValue = null;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return datas;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ saas.util.BaseUtil.showErrorToast(e.message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ me.bindStore(grid, store);
|
|
|
},
|
|
|
|
|
|
- getSummarys: function(grid) {
|
|
|
+ onStoreUpdate: function(store) {
|
|
|
+ var me = this,
|
|
|
+ summaryBar = me.summaryBar,
|
|
|
+ newSummarys = me.getSummarys();
|
|
|
+
|
|
|
+ summaryBar && summaryBar.update(newSummarys);
|
|
|
+ },
|
|
|
+
|
|
|
+ getSummarys: function() {
|
|
|
+ var me = this,
|
|
|
+ view = me.view,
|
|
|
+ columns = view.headerCt.getGridColumns(),
|
|
|
+ summarys = me.summarys;
|
|
|
+
|
|
|
+ if(summarys && summarys.length > 0) {
|
|
|
+ return me.updateSummarys();
|
|
|
+ }else {
|
|
|
+ return me.initSummarys();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ initSummarys: function() {
|
|
|
var me = this,
|
|
|
summarys = [],
|
|
|
- columns = grid.columns;
|
|
|
+ view = me.view,
|
|
|
+ grid = me.grid,
|
|
|
+ columns = view.headerCt.getGridColumns();
|
|
|
|
|
|
var typeLabels = {
|
|
|
sum: '合计',
|
|
|
@@ -108,12 +190,12 @@ Ext.define('Ext.ux.feature.MySummary', {
|
|
|
label: c.text,
|
|
|
type: summaryType,
|
|
|
typeLabel: summaryType ? typeLabels[summaryType] : '',
|
|
|
- value: c.summaryValue || 0
|
|
|
+ value: 0
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- grid.summarys = summarys;
|
|
|
+ grid.mysummarys = summarys;
|
|
|
|
|
|
if(summarys.length == 0) {
|
|
|
me.showSummaryRow = false;
|
|
|
@@ -121,18 +203,48 @@ Ext.define('Ext.ux.feature.MySummary', {
|
|
|
me.showSummaryRow = true;
|
|
|
}
|
|
|
|
|
|
+ me.summarys = summarys;
|
|
|
return summarys;
|
|
|
},
|
|
|
|
|
|
- refreshData: function(grid) {
|
|
|
+ updateSummarys: function() {
|
|
|
var me = this,
|
|
|
- summaryBar = me.summaryBar,
|
|
|
- newSummarys = me.getSummarys(grid);
|
|
|
+ view = me.view,
|
|
|
+ store = view.store,
|
|
|
+ columns = view.headerCt.getGridColumns(),
|
|
|
+ summarys = me.summarys;
|
|
|
+
|
|
|
+ Ext.Array.each(summarys, function(s) {
|
|
|
+ var col = Ext.Array.findBy(columns, function(c) {
|
|
|
+ return c.dataIndex == s.name;
|
|
|
+ });
|
|
|
+ if(col) {
|
|
|
+ s.value = col.summaryValue || 0;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return summarys;
|
|
|
+ },
|
|
|
|
|
|
- summaryBar.update(newSummarys);
|
|
|
+ getSummaryValue: function(datas, c) {
|
|
|
+ var dataIndex = c.dataIndex,
|
|
|
+ type = c.summaryType,
|
|
|
+ values = datas.map(function(d) {
|
|
|
+ return d[c.dataIndex];
|
|
|
+ }),
|
|
|
+ sum = Ext.Array.sum(values);
|
|
|
+ if (type == 'count') {
|
|
|
+ return Ext.util.Format.number(values.length, '0');
|
|
|
+ } else if (typeof c.summaryRenderer == 'function') {
|
|
|
+ return c.summaryRenderer(sum);
|
|
|
+ } else if (typeof c.renderer == 'function') {
|
|
|
+ return c.renderer(sum);
|
|
|
+ } else {
|
|
|
+ return Ext.util.Format.number(sum, c.format || '0.00');
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- getSummaryBar: function() {
|
|
|
- return this.summaryBar;
|
|
|
+ afterHeaderCtLayout: function(headerCt) {
|
|
|
+ // do nothing...
|
|
|
},
|
|
|
+
|
|
|
});
|