4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-grid-feature-GroupingSummary'>/**
19 </span> * @class Ext.grid.feature.GroupingSummary
20 * @extends Ext.grid.feature.Grouping
22 * This feature adds an aggregate summary row at the bottom of each group that is provided
23 * by the {@link Ext.grid.feature.Grouping} feature. There are 2 aspects to the summary:
27 * The summary value needs to be calculated for each column in the grid. This is controlled
28 * by the summaryType option specified on the column. There are several built in summary types,
29 * which can be specified as a string on the column configuration. These call underlying methods
32 * - {@link Ext.data.Store#count count}
33 * - {@link Ext.data.Store#sum sum}
34 * - {@link Ext.data.Store#min min}
35 * - {@link Ext.data.Store#max max}
36 * - {@link Ext.data.Store#average average}
38 * Alternatively, the summaryType can be a function definition. If this is the case,
39 * the function is called with an array of records to calculate the summary value.
43 * Similar to a column, the summary also supports a summaryRenderer function. This
44 * summaryRenderer is called before displaying a value. The function is optional, if
45 * not specified the default calculated value is shown. The summaryRenderer is called with:
47 * - value {Object} - The calculated value.
48 * - summaryData {Object} - Contains all raw summary values for the row.
49 * - field {String} - The name of the field we are calculating
53 * Ext.define('TestResult', {
54 * extend: 'Ext.data.Model',
55 * fields: ['student', 'subject', {
61 * Ext.create('Ext.grid.Panel', {
64 * renderTo: document.body,
66 * groupHeaderTpl: 'Subject: {name}',
67 * ftype: 'groupingsummary'
70 * model: 'TestResult',
71 * groupField: 'subject',
73 * student: 'Student 1',
77 * student: 'Student 1',
81 * student: 'Student 2',
85 * student: 'Student 2',
91 * dataIndex: 'student',
93 * summaryType: 'count',
94 * summaryRenderer: function(value){
95 * return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
100 * summaryType: 'average'
104 Ext.define('Ext.grid.feature.GroupingSummary', {
106 /* Begin Definitions */
108 extend: 'Ext.grid.feature.Grouping',
110 alias: 'feature.groupingsummary',
113 summary: 'Ext.grid.feature.AbstractSummary'
116 /* End Definitions */
119 <span id='Ext-grid-feature-GroupingSummary-method-getFeatureTpl'> /**
120 </span> * Modifies the row template to include the summary row.
122 * @return {String} The modified template
124 getFeatureTpl: function() {
125 var tpl = this.callParent(arguments);
127 if (this.showSummaryRow) {
128 // lop off the end </tpl> so we can attach it
129 tpl = tpl.replace('</tpl>', '');
130 tpl += '{[this.printSummaryRow(xindex)]}</tpl>';
135 <span id='Ext-grid-feature-GroupingSummary-method-getFragmentTpl'> /**
136 </span> * Gets any fragments needed for the template.
138 * @return {Object} The fragments
140 getFragmentTpl: function() {
142 fragments = me.callParent();
144 Ext.apply(fragments, me.getSummaryFragments());
145 if (me.showSummaryRow) {
146 // this gets called before render, so we'll setup the data here.
147 me.summaryGroups = me.view.store.getGroups();
148 me.summaryData = me.generateSummaryData();
153 <span id='Ext-grid-feature-GroupingSummary-method-getPrintData'> /**
154 </span> * Gets the data for printing a template row
156 * @param {Number} index The index in the template
157 * @return {Array} The template values
159 getPrintData: function(index){
161 columns = me.view.headerCt.getColumnsForTpl(),
163 length = columns.length,
165 name = me.summaryGroups[index - 1].name,
166 active = me.summaryData[name],
169 for (; i < length; ++i) {
171 column.gridSummaryValue = this.getColumnValue(column, active);
177 <span id='Ext-grid-feature-GroupingSummary-method-generateSummaryData'> /**
178 </span> * Generates all of the summary data to be used when processing the template
180 * @return {Object} The summary data
182 generateSummaryData: function(){
186 store = me.view.store,
187 groupField = this.getGroupField(),
188 reader = store.proxy.reader,
189 groups = me.summaryGroups,
190 columns = me.view.headerCt.getColumnsForTpl(),
199 for (i = 0, length = groups.length; i < length; ++i) {
200 data[groups[i].name] = {};
203 <span id='Ext-grid-feature-GroupingSummary-cfg-remoteRoot'> /**
204 </span> * @cfg {String} remoteRoot. The name of the property
205 * which contains the Array of summary objects. Defaults to <tt>undefined</tt>.
206 * It allows to use server-side calculated summaries.
208 if (me.remoteRoot && reader.rawData) {
209 // reset reader root and rebuild extractors to extract summaries data
211 reader.root = me.remoteRoot;
212 reader.buildExtractors(true);
213 Ext.Array.each(reader.getRoot(reader.rawData), function(value) {
214 remoteData[value[groupField]] = value;
216 // restore initial reader configuration
218 reader.buildExtractors(true);
221 for (i = 0, length = columns.length; i < length; ++i) {
222 comp = Ext.getCmp(columns[i].id);
223 fieldData = me.getSummary(store, comp.summaryType, comp.dataIndex, true);
225 for (key in fieldData) {
226 if (fieldData.hasOwnProperty(key)) {
227 data[key][comp.id] = fieldData[key];
231 for (key in remoteData) {
232 if (remoteData.hasOwnProperty(key)) {
233 remote = remoteData[key][comp.dataIndex];
234 if (remote !== undefined) {
235 data[key][comp.id] = remote;