3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.grid.GroupingView"></div>/**
\r
15 * @class Ext.grid.GroupingView
\r
16 * @extends Ext.grid.GridView
\r
17 * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}
\r
18 * must be used to enable grouping. Some grouping characteristics may also be configured at the
\r
19 * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>
\r
20 * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</li>
\r
21 * <li><code>{@link Ext.grid.Column#groupable groupable}</li>
\r
22 * <li><code>{@link Ext.grid.Column#groupName groupName}</li>
\r
23 * <li><code>{@link Ext.grid.Column#groupRender groupRender}</li>
\r
25 * <p>Sample usage:</p>
\r
27 var grid = new Ext.grid.GridPanel({
\r
28 // A groupingStore is required for a GroupingView
\r
29 store: new {@link Ext.data.GroupingStore}({
\r
33 sortInfo: {field: 'company', direction: 'ASC'},
\r
34 {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,
\r
35 {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,
\r
36 {@link Ext.data.GroupingStore#groupField groupField}: 'industry'
\r
38 colModel: new {@link Ext.grid.ColumnModel}({
\r
40 {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
\r
41 // {@link Ext.grid.Column#groupable groupable}, {@link Ext.grid.Column#groupName groupName}, {@link Ext.grid.Column#groupRender groupRender} are also configurable at column level
\r
42 {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},
\r
43 {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
\r
44 {header: 'Industry', dataIndex: 'industry'},
\r
45 {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
\r
49 menuDisabled: false,
\r
54 view: new Ext.grid.GroupingView({
\r
55 {@link Ext.grid.GridView#forceFit forceFit}: true,
\r
56 // custom grouping text template to display the number of items per group
\r
57 {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
64 animCollapse: false,
\r
65 title: 'Grouping Example',
\r
66 iconCls: 'icon-grid',
\r
67 renderTo: document.body
\r
71 * @param {Object} config
\r
73 Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {
\r
75 <div id="cfg-Ext.grid.GroupingView-groupByText"></div>/**
\r
76 * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column
\r
77 * (defaults to 'Group By This Field').
\r
79 groupByText : 'Group By This Field',
\r
80 <div id="cfg-Ext.grid.GroupingView-showGroupsText"></div>/**
\r
81 * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping
\r
82 * (defaults to 'Show in Groups').
\r
84 showGroupsText : 'Show in Groups',
\r
85 <div id="cfg-Ext.grid.GroupingView-hideGroupedColumn"></div>/**
\r
86 * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)
\r
88 hideGroupedColumn : false,
\r
89 <div id="cfg-Ext.grid.GroupingView-showGroupName"></div>/**
\r
90 * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value
\r
91 * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>
\r
92 * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the
\r
93 * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).
\r
95 showGroupName : true,
\r
96 <div id="cfg-Ext.grid.GroupingView-startCollapsed"></div>/**
\r
97 * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)
\r
99 startCollapsed : false,
\r
100 <div id="cfg-Ext.grid.GroupingView-enableGrouping"></div>/**
\r
101 * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)
\r
103 enableGrouping : true,
\r
104 <div id="cfg-Ext.grid.GroupingView-enableGroupingMenu"></div>/**
\r
105 * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)
\r
107 enableGroupingMenu : true,
\r
108 <div id="cfg-Ext.grid.GroupingView-enableNoGroups"></div>/**
\r
109 * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)
\r
111 enableNoGroups : true,
\r
112 <div id="cfg-Ext.grid.GroupingView-emptyGroupText"></div>/**
\r
113 * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).
\r
114 * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.
\r
116 emptyGroupText : '(None)',
\r
117 <div id="cfg-Ext.grid.GroupingView-ignoreAdd"></div>/**
\r
118 * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)
\r
121 <div id="cfg-Ext.grid.GroupingView-groupTextTpl"></div>/**
\r
122 * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).
\r
123 * This is used to format an object which contains the following properties:
\r
124 * <div class="mdetail-params"><ul>
\r
125 * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.
\r
126 * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>
\r
127 * is specified, it is the result of a call to that function.</p></li>
\r
128 * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>
\r
129 * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>
\r
130 * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>
\r
131 * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the
\r
132 * View Element which contains the group.</p></li>
\r
133 * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>
\r
134 * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data
\r
135 * for the row which caused group change.</p></li>
\r
136 * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>
\r
137 * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>
\r
139 * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>
\r
140 var grid = new Ext.grid.GridPanel({
\r
142 view: new Ext.grid.GroupingView({
\r
143 groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
148 groupTextTpl : '{text}',
\r
150 <div id="cfg-Ext.grid.GroupingView-groupMode"></div>/**
\r
151 * @cfg {String} groupMode Indicates how to construct the group identifier. <tt>'value'</tt> constructs the id using
\r
152 * raw value, <tt>'display'</tt> constructs the id using the rendered value. Defaults to <tt>'value'</tt>.
\r
154 groupMode: 'value',
\r
156 <div id="cfg-Ext.grid.GroupingView-groupRenderer"></div>/**
\r
157 * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for
\r
165 initTemplates : function(){
\r
166 Ext.grid.GroupingView.superclass.initTemplates.call(this);
\r
169 var sm = this.grid.getSelectionModel();
\r
170 sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
\r
171 this.onBeforeRowSelect, this);
\r
173 if(!this.startGroup){
\r
174 this.startGroup = new Ext.XTemplate(
\r
175 '<div id="{groupId}" class="x-grid-group {cls}">',
\r
176 '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
\r
177 '<div id="{groupId}-bd" class="x-grid-group-body">'
\r
180 this.startGroup.compile();
\r
181 this.endGroup = '</div></div>';
\r
185 findGroup : function(el){
\r
186 return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
\r
190 getGroups : function(){
\r
191 return this.hasRows() ? this.mainBody.dom.childNodes : [];
\r
195 onAdd : function(){
\r
196 if(this.enableGrouping && !this.ignoreAdd){
\r
197 var ss = this.getScrollState();
\r
199 this.restoreScroll(ss);
\r
200 }else if(!this.enableGrouping){
\r
201 Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
\r
206 onRemove : function(ds, record, index, isUpdate){
\r
207 Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
\r
208 var g = document.getElementById(record._groupId);
\r
209 if(g && g.childNodes[1].childNodes.length < 1){
\r
212 this.applyEmptyText();
\r
216 refreshRow : function(record){
\r
217 if(this.ds.getCount()==1){
\r
220 this.isUpdating = true;
\r
221 Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
\r
222 this.isUpdating = false;
\r
227 beforeMenuShow : function(){
\r
228 var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
\r
229 if((item = items.get('groupBy'))){
\r
230 item.setDisabled(disabled);
\r
232 if((item = items.get('showGroups'))){
\r
233 item.setDisabled(disabled);
\r
234 item.setChecked(!!this.getGroupField(), true);
\r
239 renderUI : function(){
\r
240 Ext.grid.GroupingView.superclass.renderUI.call(this);
\r
241 this.mainBody.on('mousedown', this.interceptMouse, this);
\r
243 if(this.enableGroupingMenu && this.hmenu){
\r
244 this.hmenu.add('-',{
\r
246 text: this.groupByText,
\r
247 handler: this.onGroupByClick,
\r
249 iconCls:'x-group-by-icon'
\r
251 if(this.enableNoGroups){
\r
253 itemId:'showGroups',
\r
254 text: this.showGroupsText,
\r
256 checkHandler: this.onShowGroupsClick,
\r
260 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
\r
265 onGroupByClick : function(){
\r
266 this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
\r
267 this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
\r
271 onShowGroupsClick : function(mi, checked){
\r
273 this.onGroupByClick();
\r
275 this.grid.store.clearGrouping();
\r
279 <div id="method-Ext.grid.GroupingView-toggleGroup"></div>/**
\r
280 * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
\r
281 * @param {String} groupId The groupId assigned to the group (see getGroupId)
\r
282 * @param {Boolean} expanded (optional)
\r
284 toggleGroup : function(group, expanded){
\r
285 this.grid.stopEditing(true);
\r
286 group = Ext.getDom(group);
\r
287 var gel = Ext.fly(group);
\r
288 expanded = expanded !== undefined ?
\r
289 expanded : gel.hasClass('x-grid-group-collapsed');
\r
291 this.state[gel.dom.id] = expanded;
\r
292 gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
\r
295 <div id="method-Ext.grid.GroupingView-toggleAllGroups"></div>/**
\r
296 * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
\r
297 * @param {Boolean} expanded (optional)
\r
299 toggleAllGroups : function(expanded){
\r
300 var groups = this.getGroups();
\r
301 for(var i = 0, len = groups.length; i < len; i++){
\r
302 this.toggleGroup(groups[i], expanded);
\r
306 <div id="method-Ext.grid.GroupingView-expandAllGroups"></div>/**
\r
307 * Expands all grouped rows.
\r
309 expandAllGroups : function(){
\r
310 this.toggleAllGroups(true);
\r
313 <div id="method-Ext.grid.GroupingView-collapseAllGroups"></div>/**
\r
314 * Collapses all grouped rows.
\r
316 collapseAllGroups : function(){
\r
317 this.toggleAllGroups(false);
\r
321 interceptMouse : function(e){
\r
322 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
\r
325 this.toggleGroup(hd.parentNode);
\r
330 getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
\r
331 var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);
\r
332 if(g === '' || g === ' '){
\r
333 g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;
\r
339 getGroupField : function(){
\r
340 return this.grid.store.getGroupState();
\r
344 afterRender : function(){
\r
345 Ext.grid.GroupingView.superclass.afterRender.call(this);
\r
346 if(this.grid.deferRowRender){
\r
347 this.updateGroupWidths();
\r
352 renderRows : function(){
\r
353 var groupField = this.getGroupField();
\r
354 var eg = !!groupField;
\r
355 // if they turned off grouping and the last grouped field is hidden
\r
356 if(this.hideGroupedColumn) {
\r
357 var colIndex = this.cm.findColumnIndex(groupField);
\r
358 if(!eg && this.lastGroupField !== undefined) {
\r
359 this.mainBody.update('');
\r
360 this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
\r
361 delete this.lastGroupField;
\r
362 }else if (eg && this.lastGroupField === undefined) {
\r
363 this.lastGroupField = groupField;
\r
364 this.cm.setHidden(colIndex, true);
\r
365 }else if (eg && this.lastGroupField !== undefined && groupField !== this.lastGroupField) {
\r
366 this.mainBody.update('');
\r
367 var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
\r
368 this.cm.setHidden(oldIndex, false);
\r
369 this.lastGroupField = groupField;
\r
370 this.cm.setHidden(colIndex, true);
\r
373 return Ext.grid.GroupingView.superclass.renderRows.apply(
\r
378 doRender : function(cs, rs, ds, startRow, colCount, stripe){
\r
382 var groupField = this.getGroupField(),
\r
383 colIndex = this.cm.findColumnIndex(groupField),
\r
386 this.enableGrouping = !!groupField;
\r
388 if(!this.enableGrouping || this.isUpdating){
\r
389 return Ext.grid.GroupingView.superclass.doRender.apply(
\r
392 var gstyle = 'width:' + this.getTotalWidth() + ';',
\r
393 gidPrefix = this.grid.getGridEl().id,
\r
394 cfg = this.cm.config[colIndex],
\r
395 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
396 prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
\r
398 curGroup, i, len, gid;
\r
400 for(i = 0, len = rs.length; i < len; i++){
\r
401 var rowIndex = startRow + i,
\r
403 gvalue = r.data[groupField];
\r
405 g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
\r
406 if(!curGroup || curGroup.group != g){
\r
407 gid = this.constructId(gvalue, gidPrefix, groupField, colIndex);
\r
408 // if state is defined use it, however state is in terms of expanded
\r
409 // so negate it, otherwise use the default.
\r
410 var isCollapsed = typeof this.state[gid] !== 'undefined' ? !this.state[gid] : this.startCollapsed;
\r
411 var gcls = isCollapsed ? 'x-grid-group-collapsed' : '';
\r
417 startRow: rowIndex,
\r
422 groups.push(curGroup);
\r
424 curGroup.rs.push(r);
\r
430 for(i = 0, len = groups.length; i < len; i++){
\r
432 this.doGroupStart(buf, g, cs, ds, colCount);
\r
433 buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
\r
434 this, cs, g.rs, ds, g.startRow, colCount, stripe);
\r
436 this.doGroupEnd(buf, g, cs, ds, colCount);
\r
438 return buf.join('');
\r
441 <div id="method-Ext.grid.GroupingView-getGroupId"></div>/**
\r
442 * Dynamically tries to determine the groupId of a specific value
\r
443 * @param {String} value
\r
444 * @return {String} The group id
\r
446 getGroupId : function(value){
\r
447 var field = this.getGroupField();
\r
448 return this.constructId(value, this.grid.getGridEl().id, field, this.cm.findColumnIndex(field));
\r
452 constructId : function(value, prefix, field, idx){
\r
453 var cfg = this.cm.config[idx],
\r
454 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
455 val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
\r
457 return prefix + '-gp-' + field + '-' + Ext.util.Format.htmlEncode(val);
\r
461 doGroupStart : function(buf, g, cs, ds, colCount){
\r
462 buf[buf.length] = this.startGroup.apply(g);
\r
466 doGroupEnd : function(buf, g, cs, ds, colCount){
\r
467 buf[buf.length] = this.endGroup;
\r
471 getRows : function(){
\r
472 if(!this.enableGrouping){
\r
473 return Ext.grid.GroupingView.superclass.getRows.call(this);
\r
476 var g, gs = this.getGroups();
\r
477 for(var i = 0, len = gs.length; i < len; i++){
\r
478 g = gs[i].childNodes[1].childNodes;
\r
479 for(var j = 0, jlen = g.length; j < jlen; j++){
\r
480 r[r.length] = g[j];
\r
487 updateGroupWidths : function(){
\r
488 if(!this.enableGrouping || !this.hasRows()){
\r
491 var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
\r
492 var gs = this.getGroups();
\r
493 for(var i = 0, len = gs.length; i < len; i++){
\r
494 gs[i].firstChild.style.width = tw;
\r
499 onColumnWidthUpdated : function(col, w, tw){
\r
500 Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
\r
501 this.updateGroupWidths();
\r
505 onAllColumnWidthsUpdated : function(ws, tw){
\r
506 Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
\r
507 this.updateGroupWidths();
\r
511 onColumnHiddenUpdated : function(col, hidden, tw){
\r
512 Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
\r
513 this.updateGroupWidths();
\r
517 onLayout : function(){
\r
518 this.updateGroupWidths();
\r
522 onBeforeRowSelect : function(sm, rowIndex){
\r
523 if(!this.enableGrouping){
\r
526 var row = this.getRow(rowIndex);
\r
527 if(row && !row.offsetParent){
\r
528 var g = this.findGroup(row);
\r
529 this.toggleGroup(g, true);
\r
534 Ext.grid.GroupingView.GROUP_ID = 1000;</pre>