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
50 * Ext.define('TestResult', {
51 * extend: 'Ext.data.Model',
52 * fields: ['student', {
58 * Ext.create('Ext.grid.Panel', {
61 * renderTo: document.body,
66 * model: 'TestResult',
68 * student: 'Student 1',
71 * student: 'Student 2',
74 * student: 'Student 3',
77 * student: 'Student 4',
82 * dataIndex: 'student',
84 * summaryType: 'count',
85 * summaryRenderer: function(value, summaryData, dataIndex) {
86 * return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
91 * summaryType: 'average'
95 Ext.define('Ext.grid.feature.Summary', {
97 /* Begin Definitions */
99 extend: 'Ext.grid.feature.AbstractSummary',
101 alias: 'feature.summary',
103 /* End Definitions */
106 * Gets any fragments needed for the template.
108 * @return {Object} The fragments
110 getFragmentTpl: function() {
111 // this gets called before render, so we'll setup the data here.
112 this.summaryData = this.generateSummaryData();
113 return this.getSummaryFragments();
117 * Overrides the closeRows method on the template so we can include our own custom
120 * @return {Object} The custom fragments
122 getTableFragments: function(){
123 if (this.showSummaryRow) {
125 closeRows: this.closeRows
131 * Provide our own custom footer for the grid.
133 * @return {String} The custom footer
135 closeRows: function() {
136 return '</tpl>{[this.printSummaryRow()]}';
140 * Gets the data for printing a template row
142 * @param {Number} index The index in the template
143 * @return {Array} The template values
145 getPrintData: function(index){
147 columns = me.view.headerCt.getColumnsForTpl(),
149 length = columns.length,
151 active = me.summaryData,
154 for (; i < length; ++i) {
156 column.gridSummaryValue = this.getColumnValue(column, active);
163 * Generates all of the summary data to be used when processing the template
165 * @return {Object} The summary data
167 generateSummaryData: function(){
170 store = me.view.store,
171 columns = me.view.headerCt.getColumnsForTpl(),
173 length = columns.length,
178 for (i = 0, length = columns.length; i < length; ++i) {
179 comp = Ext.getCmp(columns[i].id);
180 data[comp.id] = me.getSummary(store, comp.summaryType, comp.dataIndex, false);