4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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 two 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
54 * Ext.define('TestResult', {
55 * extend: 'Ext.data.Model',
56 * fields: ['student', 'subject', {
62 * Ext.create('Ext.grid.Panel', {
65 * renderTo: document.body,
67 * groupHeaderTpl: 'Subject: {name}',
68 * ftype: 'groupingsummary'
71 * model: 'TestResult',
72 * groupField: 'subject',
74 * student: 'Student 1',
78 * student: 'Student 1',
82 * student: 'Student 2',
86 * student: 'Student 2',
92 * dataIndex: 'student',
94 * summaryType: 'count',
95 * summaryRenderer: function(value){
96 * return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
101 * summaryType: 'average'
105 Ext.define('Ext.grid.feature.GroupingSummary', {
107 /* Begin Definitions */
109 extend: 'Ext.grid.feature.Grouping',
111 alias: 'feature.groupingsummary',
114 summary: 'Ext.grid.feature.AbstractSummary'
117 /* End Definitions */
120 <span id='Ext-grid-feature-GroupingSummary-method-getFeatureTpl'> /**
121 </span> * Modifies the row template to include the summary row.
123 * @return {String} The modified template
125 getFeatureTpl: function() {
126 var tpl = this.callParent(arguments);
128 if (this.showSummaryRow) {
129 // lop off the end </tpl> so we can attach it
130 tpl = tpl.replace('</tpl>', '');
131 tpl += '{[this.printSummaryRow(xindex)]}</tpl>';
136 <span id='Ext-grid-feature-GroupingSummary-method-getFragmentTpl'> /**
137 </span> * Gets any fragments needed for the template.
139 * @return {Object} The fragments
141 getFragmentTpl: function() {
143 fragments = me.callParent();
145 Ext.apply(fragments, me.getSummaryFragments());
146 if (me.showSummaryRow) {
147 // this gets called before render, so we'll setup the data here.
148 me.summaryGroups = me.view.store.getGroups();
149 me.summaryData = me.generateSummaryData();
154 <span id='Ext-grid-feature-GroupingSummary-method-getPrintData'> /**
155 </span> * Gets the data for printing a template row
157 * @param {Number} index The index in the template
158 * @return {Array} The template values
160 getPrintData: function(index){
162 columns = me.view.headerCt.getColumnsForTpl(),
164 length = columns.length,
166 name = me.summaryGroups[index - 1].name,
167 active = me.summaryData[name],
170 for (; i < length; ++i) {
172 column.gridSummaryValue = this.getColumnValue(column, active);
178 <span id='Ext-grid-feature-GroupingSummary-method-generateSummaryData'> /**
179 </span> * Generates all of the summary data to be used when processing the template
181 * @return {Object} The summary data
183 generateSummaryData: function(){
187 store = me.view.store,
188 groupField = this.getGroupField(),
189 reader = store.proxy.reader,
190 groups = me.summaryGroups,
191 columns = me.view.headerCt.getColumnsForTpl(),
200 for (i = 0, length = groups.length; i < length; ++i) {
201 data[groups[i].name] = {};
204 <span id='Ext-grid-feature-GroupingSummary-cfg-remoteRoot'> /**
205 </span> * @cfg {String} [remoteRoot=undefined] The name of the property which contains the Array of
206 * summary objects. 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 && data[key] !== undefined) {
235 data[key][comp.id] = remote;