GroupingSummary.html
8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-grid-feature-GroupingSummary'>/**
</span> * This feature adds an aggregate summary row at the bottom of each group that is provided
* by the {@link Ext.grid.feature.Grouping} feature. There are two aspects to the summary:
*
* ## Calculation
*
* The summary value needs to be calculated for each column in the grid. This is controlled
* by the summaryType option specified on the column. There are several built in summary types,
* which can be specified as a string on the column configuration. These call underlying methods
* on the store:
*
* - {@link Ext.data.Store#count count}
* - {@link Ext.data.Store#sum sum}
* - {@link Ext.data.Store#min min}
* - {@link Ext.data.Store#max max}
* - {@link Ext.data.Store#average average}
*
* Alternatively, the summaryType can be a function definition. If this is the case,
* the function is called with an array of records to calculate the summary value.
*
* ## Rendering
*
* Similar to a column, the summary also supports a summaryRenderer function. This
* summaryRenderer is called before displaying a value. The function is optional, if
* not specified the default calculated value is shown. The summaryRenderer is called with:
*
* - value {Object} - The calculated value.
* - summaryData {Object} - Contains all raw summary values for the row.
* - field {String} - The name of the field we are calculating
*
* ## Example Usage
*
* @example
* Ext.define('TestResult', {
* extend: 'Ext.data.Model',
* fields: ['student', 'subject', {
* name: 'mark',
* type: 'int'
* }]
* });
*
* Ext.create('Ext.grid.Panel', {
* width: 200,
* height: 240,
* renderTo: document.body,
* features: [{
* groupHeaderTpl: 'Subject: {name}',
* ftype: 'groupingsummary'
* }],
* store: {
* model: 'TestResult',
* groupField: 'subject',
* data: [{
* student: 'Student 1',
* subject: 'Math',
* mark: 84
* },{
* student: 'Student 1',
* subject: 'Science',
* mark: 72
* },{
* student: 'Student 2',
* subject: 'Math',
* mark: 96
* },{
* student: 'Student 2',
* subject: 'Science',
* mark: 68
* }]
* },
* columns: [{
* dataIndex: 'student',
* text: 'Name',
* summaryType: 'count',
* summaryRenderer: function(value){
* return Ext.String.format('{0} student{1}', value, value !== 1 ? 's' : '');
* }
* }, {
* dataIndex: 'mark',
* text: 'Mark',
* summaryType: 'average'
* }]
* });
*/
Ext.define('Ext.grid.feature.GroupingSummary', {
/* Begin Definitions */
extend: 'Ext.grid.feature.Grouping',
alias: 'feature.groupingsummary',
mixins: {
summary: 'Ext.grid.feature.AbstractSummary'
},
/* End Definitions */
init: function() {
this.mixins.summary.init.call(this);
},
<span id='Ext-grid-feature-GroupingSummary-method-getFeatureTpl'> /**
</span> * Modifies the row template to include the summary row.
* @private
* @return {String} The modified template
*/
getFeatureTpl: function() {
var tpl = this.callParent(arguments);
if (this.showSummaryRow) {
// lop off the end </tpl> so we can attach it
tpl = tpl.replace('</tpl>', '');
tpl += '{[this.printSummaryRow(xindex)]}</tpl>';
}
return tpl;
},
<span id='Ext-grid-feature-GroupingSummary-method-getFragmentTpl'> /**
</span> * Gets any fragments needed for the template.
* @private
* @return {Object} The fragments
*/
getFragmentTpl: function() {
var me = this,
fragments = me.callParent();
Ext.apply(fragments, me.getSummaryFragments());
if (me.showSummaryRow) {
// this gets called before render, so we'll setup the data here.
me.summaryGroups = me.view.store.getGroups();
me.summaryData = me.generateSummaryData();
}
return fragments;
},
<span id='Ext-grid-feature-GroupingSummary-method-getPrintData'> /**
</span> * Gets the data for printing a template row
* @private
* @param {Number} index The index in the template
* @return {Array} The template values
*/
getPrintData: function(index){
var me = this,
columns = me.view.headerCt.getColumnsForTpl(),
i = 0,
length = columns.length,
data = [],
name = me.summaryGroups[index - 1].name,
active = me.summaryData[name],
column;
for (; i < length; ++i) {
column = columns[i];
column.gridSummaryValue = this.getColumnValue(column, active);
data.push(column);
}
return data;
},
<span id='Ext-grid-feature-GroupingSummary-method-generateSummaryData'> /**
</span> * Generates all of the summary data to be used when processing the template
* @private
* @return {Object} The summary data
*/
generateSummaryData: function(){
var me = this,
data = {},
remoteData = {},
store = me.view.store,
groupField = this.getGroupField(),
reader = store.proxy.reader,
groups = me.summaryGroups,
columns = me.view.headerCt.getColumnsForTpl(),
remote,
i,
length,
fieldData,
root,
key,
comp,
summaryRows,
s,
sLen,
convertedSummaryRow;
for (i = 0, length = groups.length; i < length; ++i) {
data[groups[i].name] = {};
}
<span id='Ext-grid-feature-GroupingSummary-cfg-remoteRoot'> /**
</span> * @cfg {String} [remoteRoot=undefined]
* The name of the property which contains the Array of summary objects.
* It allows to use server-side calculated summaries.
*/
if (me.remoteRoot && reader.rawData) {
// reset reader root and rebuild extractors to extract summaries data
root = reader.root;
reader.root = me.remoteRoot;
reader.buildExtractors(true);
summaryRows = reader.getRoot(reader.rawData);
sLen = summaryRows.length;
// Ensure the Reader has a data conversion function to convert a raw data row into a Record data hash
if (!reader.convertRecordData) {
reader.buildExtractors();
}
for (s = 0; s < sLen; s++) {
convertedSummaryRow = {};
// Convert a raw data row into a Record's hash object using the Reader
reader.convertRecordData(convertedSummaryRow, summaryRows[s]);
remoteData[convertedSummaryRow[groupField]] = convertedSummaryRow;
}
// restore initial reader configuration
reader.root = root;
reader.buildExtractors(true);
}
for (i = 0, length = columns.length; i < length; ++i) {
comp = Ext.getCmp(columns[i].id);
fieldData = me.getSummary(store, comp.summaryType, comp.dataIndex, true);
for (key in fieldData) {
if (fieldData.hasOwnProperty(key)) {
data[key][comp.id] = fieldData[key];
}
}
for (key in remoteData) {
if (remoteData.hasOwnProperty(key)) {
remote = remoteData[key][comp.dataIndex];
if (remote !== undefined && data[key] !== undefined) {
data[key][comp.id] = remote;
}
}
}
}
return data;
}
});
</pre>
</body>
</html>