Hello!
I've needed a summary in grid that has no grouping.
So I've adjusted Ext.grid.GroupSummary to work with Ext.grid.GridView.
I'm using paging on grid so I don't need to calculate summary on client-side - I'm sending it from server as a command to my DictionaryController that controls filter & item forms and grid.
It works as grid plugin.
Code:
this.grid = new Ext.grid.GridPanel({
...
plugins: new CC.grid.GridSummary(),
...
});
...and after receiving data from server you can post summary data to plugin:
Code:
var q = Ext.DomQuery, ns = q.select('summary', doc), r = {data: []};
for(var i = 0, len = ns.length; i < len; i++){
var n = ns[i];
r.data[q.selectValue("col", n)] = q.selectValue("val", n);
}
this.grid.summary.setData(r);
this.grid.getView().refresh();
If you using JSON you can adjust source to get summary from store/reader as Ext.grid.GroupSummary does.
Code:
CC.grid.GridSummary = function(config){
Ext.apply(this, config);
};
Ext.extend(CC.grid.GridSummary, Ext.util.Observable, {
init : function(grid){
this.grid = grid;
this.cm = grid.getColumnModel();
this.view = grid.getView();
this.view.afterMethod('refresh', this.refresh, this);
this.grid.summary = this;
if(!this.rowTpl){
this.rowTpl = new Ext.Template(
'<div class="x-grid3-summary-row" style="{tstyle}">',
'<table class="x-grid3-summary-table" border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
'<tbody><tr>{cells}</tr>',
'</table></div>'
);
this.rowTpl.disableFormats = true;
}
this.rowTpl.compile();
if(!this.cellTpl){
this.cellTpl = new Ext.Template(
'<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}">',
'<div class="x-grid3-cell-inner x-grid3-col-{id}" unselectable="on" {attr}>{value}</div>',
"</td>"
);
this.cellTpl.disableFormats = true;
}
this.cellTpl.compile();
},
setData: function(o){
this.data = o;
},
renderSummary : function(o){
var cs = this.view.getColumnData();
var o = this.data;
var cfg = this.cm.config;
var buf = [], c, p = {}, cf, last = cs.length-1;
for(var i = 0, len = cs.length; i < len; i++){
c = cs[i];
cf = cfg[i];
p.id = c.id;
p.css = i == 0 ? 'x-grid3-cell-first ' : (i == last ? 'x-grid3-cell-last ' : '');
if (o.data[c.name] || cf.summaryRenderer){
p.value = (cf.summaryRenderer || c.renderer)(o.data[c.name], p, o);
}else{
p.value = '';
}
p.style = c.style;
if(p.value == undefined || p.value === "") p.value = " ";
buf[buf.length] = this.cellTpl.apply(p);
}
return this.rowTpl.apply({
tstyle: 'width:'+this.view.getTotalWidth()+';',
cells: buf.join('')
});
},
refresh : function(o){
if (this.data && this.grid.store.getCount() > 0)
Ext.DomHelper.insertHtml('beforeEnd', this.view.mainBody.dom, this.renderSummary());
}
});