2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
9 Ext.grid.GroupSummary = function(config){
\r
10 Ext.apply(this, config);
\r
13 Ext.extend(Ext.grid.GroupSummary, Ext.util.Observable, {
\r
14 init : function(grid){
\r
16 this.cm = grid.getColumnModel();
\r
17 this.view = grid.getView();
\r
20 v.doGroupEnd = this.doGroupEnd.createDelegate(this);
\r
22 v.afterMethod('onColumnWidthUpdated', this.doWidth, this);
\r
23 v.afterMethod('onAllColumnWidthsUpdated', this.doAllWidths, this);
\r
24 v.afterMethod('onColumnHiddenUpdated', this.doHidden, this);
\r
25 v.afterMethod('onUpdate', this.doUpdate, this);
\r
26 v.afterMethod('onRemove', this.doRemove, this);
\r
29 this.rowTpl = new Ext.Template(
\r
30 '<div class="x-grid3-summary-row" style="{tstyle}">',
\r
31 '<table class="x-grid3-summary-table" border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
\r
32 '<tbody><tr>{cells}</tr></tbody>',
\r
35 this.rowTpl.disableFormats = true;
\r
37 this.rowTpl.compile();
\r
40 this.cellTpl = new Ext.Template(
\r
41 '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}">',
\r
42 '<div class="x-grid3-cell-inner x-grid3-col-{id}" unselectable="on">{value}</div>',
\r
45 this.cellTpl.disableFormats = true;
\r
47 this.cellTpl.compile();
\r
50 toggleSummaries : function(visible){
\r
51 var el = this.grid.getGridEl();
\r
53 if(visible === undefined){
\r
54 visible = el.hasClass('x-grid-hide-summary');
\r
56 el[visible ? 'removeClass' : 'addClass']('x-grid-hide-summary');
\r
60 renderSummary : function(o, cs){
\r
61 cs = cs || this.view.getColumnData();
\r
62 var cfg = this.cm.config;
\r
64 var buf = [], c, p = {}, cf, last = cs.length-1;
\r
65 for(var i = 0, len = cs.length; i < len; i++){
\r
70 p.css = i == 0 ? 'x-grid3-cell-first ' : (i == last ? 'x-grid3-cell-last ' : '');
\r
71 if(cf.summaryType || cf.summaryRenderer){
\r
72 p.value = (cf.summaryRenderer || c.renderer)(o.data[c.name], p, o);
\r
76 if(p.value == undefined || p.value === "") p.value = " ";
\r
77 buf[buf.length] = this.cellTpl.apply(p);
\r
80 return this.rowTpl.apply({
\r
81 tstyle: 'width:'+this.view.getTotalWidth()+';',
\r
86 calculate : function(rs, cs){
\r
87 var data = {}, r, c, cfg = this.cm.config, cf;
\r
88 for(var j = 0, jlen = rs.length; j < jlen; j++){
\r
90 for(var i = 0, len = cs.length; i < len; i++){
\r
94 data[c.name] = Ext.grid.GroupSummary.Calculations[cf.summaryType](data[c.name] || 0, r, c.name, data);
\r
101 doGroupEnd : function(buf, g, cs, ds, colCount){
\r
102 var data = this.calculate(g.rs, cs);
\r
103 buf.push('</div>', this.renderSummary({data: data}, cs), '</div>');
\r
106 doWidth : function(col, w, tw){
\r
107 var gs = this.view.getGroups(), s;
\r
108 for(var i = 0, len = gs.length; i < len; i++){
\r
109 s = gs[i].childNodes[2];
\r
110 s.style.width = tw;
\r
111 s.firstChild.style.width = tw;
\r
112 s.firstChild.rows[0].childNodes[col].style.width = w;
\r
116 doAllWidths : function(ws, tw){
\r
117 var gs = this.view.getGroups(), s, cells, wlen = ws.length;
\r
118 for(var i = 0, len = gs.length; i < len; i++){
\r
119 s = gs[i].childNodes[2];
\r
120 s.style.width = tw;
\r
121 s.firstChild.style.width = tw;
\r
122 cells = s.firstChild.rows[0].childNodes;
\r
123 for(var j = 0; j < wlen; j++){
\r
124 cells[j].style.width = ws[j];
\r
129 doHidden : function(col, hidden, tw){
\r
130 var gs = this.view.getGroups(), s, display = hidden ? 'none' : '';
\r
131 for(var i = 0, len = gs.length; i < len; i++){
\r
132 s = gs[i].childNodes[2];
\r
133 s.style.width = tw;
\r
134 s.firstChild.style.width = tw;
\r
135 s.firstChild.rows[0].childNodes[col].style.display = display;
\r
139 // Note: requires that all (or the first) record in the
\r
140 // group share the same group value. Returns false if the group
\r
141 // could not be found.
\r
142 refreshSummary : function(groupValue){
\r
143 return this.refreshSummaryById(this.view.getGroupId(groupValue));
\r
146 getSummaryNode : function(gid){
\r
147 var g = Ext.fly(gid, '_gsummary');
\r
149 return g.down('.x-grid3-summary-row', true);
\r
154 refreshSummaryById : function(gid){
\r
155 var g = document.getElementById(gid);
\r
160 this.grid.store.each(function(r){
\r
161 if(r._groupId == gid){
\r
165 var cs = this.view.getColumnData();
\r
166 var data = this.calculate(rs, cs);
\r
167 var markup = this.renderSummary({data: data}, cs);
\r
169 var existing = this.getSummaryNode(gid);
\r
171 g.removeChild(existing);
\r
173 Ext.DomHelper.append(g, markup);
\r
177 doUpdate : function(ds, record){
\r
178 this.refreshSummaryById(record._groupId);
\r
181 doRemove : function(ds, record, index, isUpdate){
\r
183 this.refreshSummaryById(record._groupId);
\r
187 showSummaryMsg : function(groupValue, msg){
\r
188 var gid = this.view.getGroupId(groupValue);
\r
189 var node = this.getSummaryNode(gid);
\r
191 node.innerHTML = '<div class="x-grid3-summary-msg">' + msg + '</div>';
\r
196 Ext.grid.GroupSummary.Calculations = {
\r
197 'sum' : function(v, record, field){
\r
198 return v + (record.data[field]||0);
\r
201 'count' : function(v, record, field, data){
\r
202 return data[field+'count'] ? ++data[field+'count'] : (data[field+'count'] = 1);
\r
205 'max' : function(v, record, field, data){
\r
206 var v = record.data[field];
\r
207 var max = data[field+'max'] === undefined ? (data[field+'max'] = v) : data[field+'max'];
\r
208 return v > max ? (data[field+'max'] = v) : max;
\r
211 'min' : function(v, record, field, data){
\r
212 var v = record.data[field];
\r
213 var min = data[field+'min'] === undefined ? (data[field+'min'] = v) : data[field+'min'];
\r
214 return v < min ? (data[field+'min'] = v) : min;
\r
217 'average' : function(v, record, field, data){
\r
218 var c = data[field+'count'] ? ++data[field+'count'] : (data[field+'count'] = 1);
\r
219 var t = (data[field+'total'] = ((data[field+'total']||0) + (record.data[field]||0)));
\r
220 return t === 0 ? 0 : t / c;
\r
224 Ext.grid.HybridSummary = Ext.extend(Ext.grid.GroupSummary, {
\r
225 calculate : function(rs, cs){
\r
226 var gcol = this.view.getGroupField();
\r
227 var gvalue = rs[0].data[gcol];
\r
228 var gdata = this.getSummaryData(gvalue);
\r
229 return gdata || Ext.grid.HybridSummary.superclass.calculate.call(this, rs, cs);
\r
232 updateSummaryData : function(groupValue, data, skipRefresh){
\r
233 var json = this.grid.store.reader.jsonData;
\r
234 if(!json.summaryData){
\r
235 json.summaryData = {};
\r
237 json.summaryData[groupValue] = data;
\r
239 this.refreshSummary(groupValue);
\r
243 getSummaryData : function(groupValue){
\r
244 var json = this.grid.store.reader.jsonData;
\r
245 if(json && json.summaryData){
\r
246 return json.summaryData[groupValue];
\r