3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.grid.GroupingView
\r
9 * @extends Ext.grid.GridView
\r
10 * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}
\r
11 * must be used to enable grouping. Some grouping characteristics may also be configured at the
\r
12 * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>
\r
13 * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</li>
\r
14 * <li><code>{@link Ext.grid.Column#groupable groupable}</li>
\r
15 * <li><code>{@link Ext.grid.Column#groupName groupName}</li>
\r
16 * <li><code>{@link Ext.grid.Column#groupRender groupRender}</li>
\r
18 * <p>Sample usage:</p>
\r
20 var grid = new Ext.grid.GridPanel({
\r
21 // A groupingStore is required for a GroupingView
\r
22 store: new {@link Ext.data.GroupingStore}({
\r
26 sortInfo: {field: 'company', direction: 'ASC'},
\r
27 {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,
\r
28 {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,
\r
29 {@link Ext.data.GroupingStore#groupField groupField}: 'industry'
\r
31 colModel: new {@link Ext.grid.ColumnModel}({
\r
33 {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
\r
34 // {@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
35 {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},
\r
36 {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
\r
37 {header: 'Industry', dataIndex: 'industry'},
\r
38 {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
\r
42 menuDisabled: false,
\r
47 view: new Ext.grid.GroupingView({
\r
48 {@link Ext.grid.GridView#forceFit forceFit}: true,
\r
49 // custom grouping text template to display the number of items per group
\r
50 {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
57 animCollapse: false,
\r
58 title: 'Grouping Example',
\r
59 iconCls: 'icon-grid',
\r
60 renderTo: document.body
\r
64 * @param {Object} config
\r
66 Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {
\r
69 * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column
\r
70 * (defaults to 'Group By This Field').
\r
72 groupByText : 'Group By This Field',
\r
74 * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping
\r
75 * (defaults to 'Show in Groups').
\r
77 showGroupsText : 'Show in Groups',
\r
79 * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)
\r
81 hideGroupedColumn : false,
\r
83 * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value
\r
84 * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>
\r
85 * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the
\r
86 * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).
\r
88 showGroupName : true,
\r
90 * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)
\r
92 startCollapsed : false,
\r
94 * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)
\r
96 enableGrouping : true,
\r
98 * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)
\r
100 enableGroupingMenu : true,
\r
102 * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)
\r
104 enableNoGroups : true,
\r
106 * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).
\r
107 * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.
\r
109 emptyGroupText : '(None)',
\r
111 * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)
\r
115 * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).
\r
116 * This is used to format an object which contains the following properties:
\r
117 * <div class="mdetail-params"><ul>
\r
118 * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.
\r
119 * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>
\r
120 * is specified, it is the result of a call to that function.</p></li>
\r
121 * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>
\r
122 * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>
\r
123 * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>
\r
124 * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the
\r
125 * View Element which contains the group.</p></li>
\r
126 * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>
\r
127 * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data
\r
128 * for the row which caused group change.</p></li>
\r
129 * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>
\r
130 * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>
\r
132 * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>
\r
133 var grid = new Ext.grid.GridPanel({
\r
135 view: new Ext.grid.GroupingView({
\r
136 groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
141 groupTextTpl : '{text}',
\r
144 * @cfg {String} groupMode Indicates how to construct the group identifier. <tt>'value'</tt> constructs the id using
\r
145 * raw value, <tt>'display'</tt> constructs the id using the rendered value. Defaults to <tt>'value'</tt>.
\r
147 groupMode: 'value',
\r
150 * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for
\r
158 initTemplates : function(){
\r
159 Ext.grid.GroupingView.superclass.initTemplates.call(this);
\r
162 var sm = this.grid.getSelectionModel();
\r
163 sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
\r
164 this.onBeforeRowSelect, this);
\r
166 if(!this.startGroup){
\r
167 this.startGroup = new Ext.XTemplate(
\r
168 '<div id="{groupId}" class="x-grid-group {cls}">',
\r
169 '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
\r
170 '<div id="{groupId}-bd" class="x-grid-group-body">'
\r
173 this.startGroup.compile();
\r
174 this.endGroup = '</div></div>';
\r
178 findGroup : function(el){
\r
179 return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
\r
183 getGroups : function(){
\r
184 return this.hasRows() ? this.mainBody.dom.childNodes : [];
\r
188 onAdd : function(){
\r
189 if(this.enableGrouping && !this.ignoreAdd){
\r
190 var ss = this.getScrollState();
\r
192 this.restoreScroll(ss);
\r
193 }else if(!this.enableGrouping){
\r
194 Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
\r
199 onRemove : function(ds, record, index, isUpdate){
\r
200 Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
\r
201 var g = document.getElementById(record._groupId);
\r
202 if(g && g.childNodes[1].childNodes.length < 1){
\r
205 this.applyEmptyText();
\r
209 refreshRow : function(record){
\r
210 if(this.ds.getCount()==1){
\r
213 this.isUpdating = true;
\r
214 Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
\r
215 this.isUpdating = false;
\r
220 beforeMenuShow : function(){
\r
221 var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
\r
222 if((item = items.get('groupBy'))){
\r
223 item.setDisabled(disabled);
\r
225 if((item = items.get('showGroups'))){
\r
226 item.setDisabled(disabled);
\r
227 item.setChecked(!!this.getGroupField(), true);
\r
232 renderUI : function(){
\r
233 Ext.grid.GroupingView.superclass.renderUI.call(this);
\r
234 this.mainBody.on('mousedown', this.interceptMouse, this);
\r
236 if(this.enableGroupingMenu && this.hmenu){
\r
237 this.hmenu.add('-',{
\r
239 text: this.groupByText,
\r
240 handler: this.onGroupByClick,
\r
242 iconCls:'x-group-by-icon'
\r
244 if(this.enableNoGroups){
\r
246 itemId:'showGroups',
\r
247 text: this.showGroupsText,
\r
249 checkHandler: this.onShowGroupsClick,
\r
253 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
\r
258 onGroupByClick : function(){
\r
259 this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
\r
260 this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
\r
264 onShowGroupsClick : function(mi, checked){
\r
266 this.onGroupByClick();
\r
268 this.grid.store.clearGrouping();
\r
273 * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
\r
274 * @param {String} groupId The groupId assigned to the group (see getGroupId)
\r
275 * @param {Boolean} expanded (optional)
\r
277 toggleGroup : function(group, expanded){
\r
278 this.grid.stopEditing(true);
\r
279 group = Ext.getDom(group);
\r
280 var gel = Ext.fly(group);
\r
281 expanded = expanded !== undefined ?
\r
282 expanded : gel.hasClass('x-grid-group-collapsed');
\r
284 this.state[gel.dom.id] = expanded;
\r
285 gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
\r
289 * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
\r
290 * @param {Boolean} expanded (optional)
\r
292 toggleAllGroups : function(expanded){
\r
293 var groups = this.getGroups();
\r
294 for(var i = 0, len = groups.length; i < len; i++){
\r
295 this.toggleGroup(groups[i], expanded);
\r
300 * Expands all grouped rows.
\r
302 expandAllGroups : function(){
\r
303 this.toggleAllGroups(true);
\r
307 * Collapses all grouped rows.
\r
309 collapseAllGroups : function(){
\r
310 this.toggleAllGroups(false);
\r
314 interceptMouse : function(e){
\r
315 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
\r
318 this.toggleGroup(hd.parentNode);
\r
323 getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
\r
324 var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);
\r
325 if(g === '' || g === ' '){
\r
326 g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;
\r
332 getGroupField : function(){
\r
333 return this.grid.store.getGroupState();
\r
337 afterRender : function(){
\r
338 Ext.grid.GroupingView.superclass.afterRender.call(this);
\r
339 if(this.grid.deferRowRender){
\r
340 this.updateGroupWidths();
\r
345 renderRows : function(){
\r
346 var groupField = this.getGroupField();
\r
347 var eg = !!groupField;
\r
348 // if they turned off grouping and the last grouped field is hidden
\r
349 if(this.hideGroupedColumn) {
\r
350 var colIndex = this.cm.findColumnIndex(groupField);
\r
351 if(!eg && this.lastGroupField !== undefined) {
\r
352 this.mainBody.update('');
\r
353 this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
\r
354 delete this.lastGroupField;
\r
355 }else if (eg && this.lastGroupField === undefined) {
\r
356 this.lastGroupField = groupField;
\r
357 this.cm.setHidden(colIndex, true);
\r
358 }else if (eg && this.lastGroupField !== undefined && groupField !== this.lastGroupField) {
\r
359 this.mainBody.update('');
\r
360 var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
\r
361 this.cm.setHidden(oldIndex, false);
\r
362 this.lastGroupField = groupField;
\r
363 this.cm.setHidden(colIndex, true);
\r
366 return Ext.grid.GroupingView.superclass.renderRows.apply(
\r
371 doRender : function(cs, rs, ds, startRow, colCount, stripe){
\r
375 var groupField = this.getGroupField(),
\r
376 colIndex = this.cm.findColumnIndex(groupField),
\r
379 this.enableGrouping = !!groupField;
\r
381 if(!this.enableGrouping || this.isUpdating){
\r
382 return Ext.grid.GroupingView.superclass.doRender.apply(
\r
385 var gstyle = 'width:' + this.getTotalWidth() + ';',
\r
386 gidPrefix = this.grid.getGridEl().id,
\r
387 cfg = this.cm.config[colIndex],
\r
388 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
389 prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
\r
391 curGroup, i, len, gid;
\r
393 for(i = 0, len = rs.length; i < len; i++){
\r
394 var rowIndex = startRow + i,
\r
396 gvalue = r.data[groupField];
\r
398 g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
\r
399 if(!curGroup || curGroup.group != g){
\r
400 gid = this.constructId(gvalue, gidPrefix, groupField, colIndex);
\r
401 // if state is defined use it, however state is in terms of expanded
\r
402 // so negate it, otherwise use the default.
\r
403 var isCollapsed = typeof this.state[gid] !== 'undefined' ? !this.state[gid] : this.startCollapsed;
\r
404 var gcls = isCollapsed ? 'x-grid-group-collapsed' : '';
\r
410 startRow: rowIndex,
\r
415 groups.push(curGroup);
\r
417 curGroup.rs.push(r);
\r
423 for(i = 0, len = groups.length; i < len; i++){
\r
425 this.doGroupStart(buf, g, cs, ds, colCount);
\r
426 buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
\r
427 this, cs, g.rs, ds, g.startRow, colCount, stripe);
\r
429 this.doGroupEnd(buf, g, cs, ds, colCount);
\r
431 return buf.join('');
\r
435 * Dynamically tries to determine the groupId of a specific value
\r
436 * @param {String} value
\r
437 * @return {String} The group id
\r
439 getGroupId : function(value){
\r
440 var field = this.getGroupField();
\r
441 return this.constructId(value, this.grid.getGridEl().id, field, this.cm.findColumnIndex(field));
\r
445 constructId : function(value, prefix, field, idx){
\r
446 var cfg = this.cm.config[idx],
\r
447 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
448 val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
\r
450 return prefix + '-gp-' + field + '-' + Ext.util.Format.htmlEncode(val);
\r
454 doGroupStart : function(buf, g, cs, ds, colCount){
\r
455 buf[buf.length] = this.startGroup.apply(g);
\r
459 doGroupEnd : function(buf, g, cs, ds, colCount){
\r
460 buf[buf.length] = this.endGroup;
\r
464 getRows : function(){
\r
465 if(!this.enableGrouping){
\r
466 return Ext.grid.GroupingView.superclass.getRows.call(this);
\r
469 var g, gs = this.getGroups();
\r
470 for(var i = 0, len = gs.length; i < len; i++){
\r
471 g = gs[i].childNodes[1].childNodes;
\r
472 for(var j = 0, jlen = g.length; j < jlen; j++){
\r
473 r[r.length] = g[j];
\r
480 updateGroupWidths : function(){
\r
481 if(!this.enableGrouping || !this.hasRows()){
\r
484 var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
\r
485 var gs = this.getGroups();
\r
486 for(var i = 0, len = gs.length; i < len; i++){
\r
487 gs[i].firstChild.style.width = tw;
\r
492 onColumnWidthUpdated : function(col, w, tw){
\r
493 Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
\r
494 this.updateGroupWidths();
\r
498 onAllColumnWidthsUpdated : function(ws, tw){
\r
499 Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
\r
500 this.updateGroupWidths();
\r
504 onColumnHiddenUpdated : function(col, hidden, tw){
\r
505 Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
\r
506 this.updateGroupWidths();
\r
510 onLayout : function(){
\r
511 this.updateGroupWidths();
\r
515 onBeforeRowSelect : function(sm, rowIndex){
\r
516 if(!this.enableGrouping){
\r
519 var row = this.getRow(rowIndex);
\r
520 if(row && !row.offsetParent){
\r
521 var g = this.findGroup(row);
\r
522 this.toggleGroup(g, true);
\r
527 Ext.grid.GroupingView.GROUP_ID = 1000;