3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.grid.feature.Summary
17 * @extends Ext.grid.feature.AbstractSummary
19 * This feature is used to place a summary row at the bottom of the grid. If using a grouping,
20 * see {@link Ext.grid.feature.GroupingSummary}. There are 2 aspects to calculating the summaries,
21 * calculation and rendering.
24 * The summary value needs to be calculated for each column in the grid. This is controlled
25 * by the summaryType option specified on the column. There are several built in summary types,
26 * which can be specified as a string on the column configuration. These call underlying methods
29 * - {@link Ext.data.Store#count count}
30 * - {@link Ext.data.Store#sum sum}
31 * - {@link Ext.data.Store#min min}
32 * - {@link Ext.data.Store#max max}
33 * - {@link Ext.data.Store#average average}
35 * Alternatively, the summaryType can be a function definition. If this is the case,
36 * the function is called with an array of records to calculate the summary value.
39 * Similar to a column, the summary also supports a summaryRenderer function. This
40 * summaryRenderer is called before displaying a value. The function is optional, if
41 * not specified the default calculated value is shown. The summaryRenderer is called with:
43 * - value {Object} - The calculated value.
44 * - summaryData {Object} - Contains all raw summary values for the row.
45 * - field {String} - The name of the field we are calculating
49 * Ext.define('TestResult', {
50 * extend: 'Ext.data.Model',
51 * fields: ['student', {
57 * Ext.create('Ext.grid.Panel', {
60 * renderTo: document.body,
65 * model: 'TestResult',
67 * student: 'Student 1',
70 * student: 'Student 2',
73 * student: 'Student 3',
76 * student: 'Student 4',
81 * dataIndex: 'student',
83 * summaryType: 'count',
84 * summaryRenderer: function(value, summaryData, dataIndex) {
85 * return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
90 * summaryType: 'average'
94 Ext.define('Ext.grid.feature.Summary', {
96 /* Begin Definitions */
98 extend: 'Ext.grid.feature.AbstractSummary',
100 alias: 'feature.summary',
102 /* End Definitions */
105 * Gets any fragments needed for the template.
107 * @return {Object} The fragments
109 getFragmentTpl: function() {
110 // this gets called before render, so we'll setup the data here.
111 this.summaryData = this.generateSummaryData();
112 return this.getSummaryFragments();
116 * Overrides the closeRows method on the template so we can include our own custom
119 * @return {Object} The custom fragments
121 getTableFragments: function(){
122 if (this.showSummaryRow) {
124 closeRows: this.closeRows
130 * Provide our own custom footer for the grid.
132 * @return {String} The custom footer
134 closeRows: function() {
135 return '</tpl>{[this.printSummaryRow()]}';
139 * Gets the data for printing a template row
141 * @param {Number} index The index in the template
142 * @return {Array} The template values
144 getPrintData: function(index){
146 columns = me.view.headerCt.getColumnsForTpl(),
148 length = columns.length,
150 active = me.summaryData,
153 for (; i < length; ++i) {
155 column.gridSummaryValue = this.getColumnValue(column, active);
162 * Generates all of the summary data to be used when processing the template
164 * @return {Object} The summary data
166 generateSummaryData: function(){
169 store = me.view.store,
170 columns = me.view.headerCt.getColumnsForTpl(),
172 length = columns.length,
177 for (i = 0, length = columns.length; i < length; ++i) {
178 comp = Ext.getCmp(columns[i].id);
179 data[comp.id] = me.getSummary(store, comp.summaryType, comp.dataIndex, false);