1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-grid.feature.GroupingSummary'>/**
2 </span> * @class Ext.grid.feature.GroupingSummary
3 * @extends Ext.grid.feature.Grouping
5 * This feature adds an aggregate summary row at the bottom of each group that is provided
6 * by the {@link Ext.grid.feature.Grouping} feature. There are 2 aspects to the summary:
10 * The summary value needs to be calculated for each column in the grid. This is controlled
11 * by the summaryType option specified on the column. There are several built in summary types,
12 * which can be specified as a string on the column configuration. These call underlying methods
15 * - {@link Ext.data.Store#count count}
16 * - {@link Ext.data.Store#sum sum}
17 * - {@link Ext.data.Store#min min}
18 * - {@link Ext.data.Store#max max}
19 * - {@link Ext.data.Store#average average}
21 * Alternatively, the summaryType can be a function definition. If this is the case,
22 * the function is called with an array of records to calculate the summary value.
26 * Similar to a column, the summary also supports a summaryRenderer function. This
27 * summaryRenderer is called before displaying a value. The function is optional, if
28 * not specified the default calculated value is shown. The summaryRenderer is called with:
30 * - value {Object} - The calculated value.
31 * - data {Object} - Contains all raw summary values for the row.
32 * - field {String} - The name of the field we are calculating
36 * Ext.define('TestResult', {
37 * extend: 'Ext.data.Model',
38 * fields: ['student', 'subject', {
44 * Ext.create('Ext.grid.Panel', {
47 * renderTo: document.body,
49 * groupHeaderTpl: 'Subject: {name}',
50 * ftype: 'groupingsummary'
53 * model: 'TestResult',
54 * groupField: 'subject',
56 * student: 'Student 1',
60 * student: 'Student 1',
64 * student: 'Student 2',
68 * student: 'Student 2',
74 * dataIndex: 'student',
76 * summaryType: 'count',
77 * summaryRenderer: function(value){
78 * return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
83 * summaryType: 'average'
87 Ext.define('Ext.grid.feature.GroupingSummary', {
89 /* Begin Definitions */
91 extend: 'Ext.grid.feature.Grouping',
93 alias: 'feature.groupingsummary',
96 summary: 'Ext.grid.feature.AbstractSummary'
102 <span id='Ext-grid.feature.GroupingSummary-method-getFeatureTpl'> /**
103 </span> * Modifies the row template to include the summary row.
105 * @return {String} The modified template
107 getFeatureTpl: function() {
108 var tpl = this.callParent(arguments);
110 if (this.showSummaryRow) {
111 // lop off the end </tpl> so we can attach it
112 tpl = tpl.replace('</tpl>', '');
113 tpl += '{[this.printSummaryRow(xindex)]}</tpl>';
118 <span id='Ext-grid.feature.GroupingSummary-method-getFragmentTpl'> /**
119 </span> * Gets any fragments needed for the template.
121 * @return {Object} The fragments
123 getFragmentTpl: function() {
125 fragments = me.callParent();
127 Ext.apply(fragments, me.getSummaryFragments());
128 if (me.showSummaryRow) {
129 // this gets called before render, so we'll setup the data here.
130 me.summaryGroups = me.view.store.getGroups();
131 me.summaryData = me.generateSummaryData();
136 <span id='Ext-grid.feature.GroupingSummary-method-getPrintData'> /**
137 </span> * Gets the data for printing a template row
139 * @param {Number} index The index in the template
140 * @return {Array} The template values
142 getPrintData: function(index){
144 columns = me.view.headerCt.getColumnsForTpl(),
146 length = columns.length,
148 name = me.summaryGroups[index - 1].name,
149 active = me.summaryData[name],
152 for (; i < length; ++i) {
154 column.gridSummaryValue = this.getColumnValue(column, active);
160 <span id='Ext-grid.feature.GroupingSummary-method-generateSummaryData'> /**
161 </span> * Generates all of the summary data to be used when processing the template
163 * @return {Object} The summary data
165 generateSummaryData: function(){
169 store = me.view.store,
170 groupField = this.getGroupField(),
171 reader = store.proxy.reader,
172 groups = me.summaryGroups,
173 columns = me.view.headerCt.getColumnsForTpl(),
181 for (i = 0, length = groups.length; i < length; ++i) {
182 data[groups[i].name] = {};
185 <span id='Ext-grid.feature.GroupingSummary-cfg-remoteRoot'> /**
186 </span> * @cfg {String} remoteRoot. The name of the property
187 * which contains the Array of summary objects. Defaults to <tt>undefined</tt>.
188 * It allows to use server-side calculated summaries.
190 if (me.remoteRoot && reader.rawData) {
191 // reset reader root and rebuild extractors to extract summaries data
193 reader.root = me.remoteRoot;
194 reader.buildExtractors(true);
195 Ext.Array.each(reader.getRoot(reader.rawData), function(value) {
196 data[value[groupField]] = value;
197 data[value[groupField]]._remote = true;
199 // restore initial reader configuration
201 reader.buildExtractors(true);
204 for (i = 0, length = columns.length; i < length; ++i) {
205 comp = Ext.getCmp(columns[i].id);
206 fieldData = me.getSummary(store, comp.summaryType, comp.dataIndex, true);
208 for (key in fieldData) {
209 if (fieldData.hasOwnProperty(key)) {
210 if (!data[key]._remote) {
211 data[key][comp.dataIndex] = fieldData[key];
219 </pre></pre></body></html>