3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.grid.GroupingView"></div>/**
16 * @class Ext.grid.GroupingView
17 * @extends Ext.grid.GridView
18 * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}
19 * must be used to enable grouping. Some grouping characteristics may also be configured at the
20 * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>
21 * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</code></li>
22 * <li><code>{@link Ext.grid.Column#groupable groupable}</code></li>
23 * <li><code>{@link Ext.grid.Column#groupName groupName}</code></li>
24 * <li><code>{@link Ext.grid.Column#groupRender groupRender}</code></li>
26 * <p>Sample usage:</p>
28 var grid = new Ext.grid.GridPanel({
29 // A groupingStore is required for a GroupingView
30 store: new {@link Ext.data.GroupingStore}({
34 sortInfo: {field: 'company', direction: 'ASC'},
35 {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,
36 {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,
37 {@link Ext.data.GroupingStore#groupField groupField}: 'industry'
39 colModel: new {@link Ext.grid.ColumnModel}({
41 {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
42 // {@link Ext.grid.Column#groupable groupable}, {@link Ext.grid.Column#groupName groupName}, {@link Ext.grid.Column#groupRender groupRender} are also configurable at column level
43 {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},
44 {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
45 {header: 'Industry', dataIndex: 'industry'},
46 {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
55 view: new Ext.grid.GroupingView({
56 {@link Ext.grid.GridView#forceFit forceFit}: true,
57 // custom grouping text template to display the number of items per group
58 {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
66 title: 'Grouping Example',
68 renderTo: document.body
72 * @param {Object} config
74 Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {
76 <div id="cfg-Ext.grid.GroupingView-groupByText"></div>/**
77 * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column
78 * (defaults to 'Group By This Field').
80 groupByText : 'Group By This Field',
81 <div id="cfg-Ext.grid.GroupingView-showGroupsText"></div>/**
82 * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping
83 * (defaults to 'Show in Groups').
85 showGroupsText : 'Show in Groups',
86 <div id="cfg-Ext.grid.GroupingView-hideGroupedColumn"></div>/**
87 * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)
89 hideGroupedColumn : false,
90 <div id="cfg-Ext.grid.GroupingView-showGroupName"></div>/**
91 * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value
92 * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>
93 * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the
94 * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).
97 <div id="cfg-Ext.grid.GroupingView-startCollapsed"></div>/**
98 * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)
100 startCollapsed : false,
101 <div id="cfg-Ext.grid.GroupingView-enableGrouping"></div>/**
102 * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)
104 enableGrouping : true,
105 <div id="cfg-Ext.grid.GroupingView-enableGroupingMenu"></div>/**
106 * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)
108 enableGroupingMenu : true,
109 <div id="cfg-Ext.grid.GroupingView-enableNoGroups"></div>/**
110 * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)
112 enableNoGroups : true,
113 <div id="cfg-Ext.grid.GroupingView-emptyGroupText"></div>/**
114 * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).
115 * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.
117 emptyGroupText : '(None)',
118 <div id="cfg-Ext.grid.GroupingView-ignoreAdd"></div>/**
119 * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)
122 <div id="cfg-Ext.grid.GroupingView-groupTextTpl"></div>/**
123 * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).
124 * This is used to format an object which contains the following properties:
125 * <div class="mdetail-params"><ul>
126 * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.
127 * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>
128 * is specified, it is the result of a call to that function.</p></li>
129 * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>
130 * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>
131 * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>
132 * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the
133 * View Element which contains the group.</p></li>
134 * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>
135 * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data
136 * for the row which caused group change.</p></li>
137 * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>
138 * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>
140 * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>
141 var grid = new Ext.grid.GridPanel({
143 view: new Ext.grid.GroupingView({
144 groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
149 groupTextTpl : '{text}',
151 <div id="cfg-Ext.grid.GroupingView-groupMode"></div>/**
152 * @cfg {String} groupMode Indicates how to construct the group identifier. <tt>'value'</tt> constructs the id using
153 * raw value, <tt>'display'</tt> constructs the id using the rendered value. Defaults to <tt>'value'</tt>.
157 <div id="cfg-Ext.grid.GroupingView-groupRenderer"></div>/**
158 * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for
162 <div id="cfg-Ext.grid.GroupingView-cancelEditOnToggle"></div>/**
163 * @cfg {Boolean} cancelEditOnToggle True to cancel any editing when the group header is toggled. Defaults to <tt>true</tt>.
165 cancelEditOnToggle: true,
168 initTemplates : function(){
169 Ext.grid.GroupingView.superclass.initTemplates.call(this);
172 var sm = this.grid.getSelectionModel();
173 sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
174 this.onBeforeRowSelect, this);
176 if(!this.startGroup){
177 this.startGroup = new Ext.XTemplate(
178 '<div id="{groupId}" class="x-grid-group {cls}">',
179 '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
180 '<div id="{groupId}-bd" class="x-grid-group-body">'
183 this.startGroup.compile();
185 if (!this.endGroup) {
186 this.endGroup = '</div></div>';
191 findGroup : function(el){
192 return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
196 getGroups : function(){
197 return this.hasRows() ? this.mainBody.dom.childNodes : [];
201 onAdd : function(ds, records, index) {
202 if (this.canGroup() && !this.ignoreAdd) {
203 var ss = this.getScrollState();
204 this.fireEvent('beforerowsinserted', ds, index, index + (records.length-1));
206 this.restoreScroll(ss);
207 this.fireEvent('rowsinserted', ds, index, index + (records.length-1));
208 } else if (!this.canGroup()) {
209 Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
214 onRemove : function(ds, record, index, isUpdate){
215 Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
216 var g = document.getElementById(record._groupId);
217 if(g && g.childNodes[1].childNodes.length < 1){
220 this.applyEmptyText();
224 refreshRow : function(record){
225 if(this.ds.getCount()==1){
228 this.isUpdating = true;
229 Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
230 this.isUpdating = false;
235 beforeMenuShow : function(){
236 var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
237 if((item = items.get('groupBy'))){
238 item.setDisabled(disabled);
240 if((item = items.get('showGroups'))){
241 item.setDisabled(disabled);
242 item.setChecked(this.canGroup(), true);
247 renderUI : function(){
248 var markup = Ext.grid.GroupingView.superclass.renderUI.call(this);
250 if(this.enableGroupingMenu && this.hmenu){
253 text: this.groupByText,
254 handler: this.onGroupByClick,
256 iconCls:'x-group-by-icon'
258 if(this.enableNoGroups){
261 text: this.showGroupsText,
263 checkHandler: this.onShowGroupsClick,
267 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
272 processEvent: function(name, e){
273 Ext.grid.GroupingView.superclass.processEvent.call(this, name, e);
274 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
276 // group value is at the end of the string
277 var field = this.getGroupField(),
278 prefix = this.getPrefix(field),
279 groupValue = hd.id.substring(prefix.length),
280 emptyRe = new RegExp('gp-' + Ext.escapeRe(field) + '--hd');
282 // remove trailing '-hd'
283 groupValue = groupValue.substr(0, groupValue.length - 3);
285 // also need to check for empty groups
286 if(groupValue || emptyRe.test(hd.id)){
287 this.grid.fireEvent('group' + name, this.grid, field, groupValue, e);
289 if(name == 'mousedown' && e.button == 0){
290 this.toggleGroup(hd.parentNode);
297 onGroupByClick : function(){
298 var grid = this.grid;
299 this.enableGrouping = true;
300 grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
301 grid.fireEvent('groupchange', grid, grid.store.getGroupState());
302 this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
307 onShowGroupsClick : function(mi, checked){
308 this.enableGrouping = checked;
310 this.onGroupByClick();
312 this.grid.store.clearGrouping();
313 this.grid.fireEvent('groupchange', this, null);
317 <div id="method-Ext.grid.GroupingView-toggleRowIndex"></div>/**
318 * Toggle the group that contains the specific row.
319 * @param {Number} rowIndex The row inside the group
320 * @param {Boolean} expanded (optional)
322 toggleRowIndex : function(rowIndex, expanded){
323 if(!this.canGroup()){
326 var row = this.getRow(rowIndex);
328 this.toggleGroup(this.findGroup(row), expanded);
332 <div id="method-Ext.grid.GroupingView-toggleGroup"></div>/**
333 * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
334 * @param {String} groupId The groupId assigned to the group (see getGroupId)
335 * @param {Boolean} expanded (optional)
337 toggleGroup : function(group, expanded){
338 var gel = Ext.get(group);
339 expanded = Ext.isDefined(expanded) ? expanded : gel.hasClass('x-grid-group-collapsed');
340 if(this.state[gel.id] !== expanded){
341 if (this.cancelEditOnToggle !== false) {
342 this.grid.stopEditing(true);
344 this.state[gel.id] = expanded;
345 gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
349 <div id="method-Ext.grid.GroupingView-toggleAllGroups"></div>/**
350 * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
351 * @param {Boolean} expanded (optional)
353 toggleAllGroups : function(expanded){
354 var groups = this.getGroups();
355 for(var i = 0, len = groups.length; i < len; i++){
356 this.toggleGroup(groups[i], expanded);
360 <div id="method-Ext.grid.GroupingView-expandAllGroups"></div>/**
361 * Expands all grouped rows.
363 expandAllGroups : function(){
364 this.toggleAllGroups(true);
367 <div id="method-Ext.grid.GroupingView-collapseAllGroups"></div>/**
368 * Collapses all grouped rows.
370 collapseAllGroups : function(){
371 this.toggleAllGroups(false);
375 getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
376 var column = this.cm.config[colIndex],
377 g = groupRenderer ? groupRenderer.call(column.scope, v, {}, r, rowIndex, colIndex, ds) : String(v);
378 if(g === '' || g === ' '){
379 g = column.emptyGroupText || this.emptyGroupText;
385 getGroupField : function(){
386 return this.grid.store.getGroupState();
390 afterRender : function(){
391 if(!this.ds || !this.cm){
394 Ext.grid.GroupingView.superclass.afterRender.call(this);
395 if(this.grid.deferRowRender){
396 this.updateGroupWidths();
400 afterRenderUI: function () {
401 Ext.grid.GroupingView.superclass.afterRenderUI.call(this);
403 if (this.enableGroupingMenu && this.hmenu) {
406 text: this.groupByText,
407 handler: this.onGroupByClick,
409 iconCls:'x-group-by-icon'
412 if (this.enableNoGroups) {
415 text: this.showGroupsText,
417 checkHandler: this.onShowGroupsClick,
422 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
427 renderRows : function(){
428 var groupField = this.getGroupField();
429 var eg = !!groupField;
430 // if they turned off grouping and the last grouped field is hidden
431 if(this.hideGroupedColumn) {
432 var colIndex = this.cm.findColumnIndex(groupField),
433 hasLastGroupField = Ext.isDefined(this.lastGroupField);
434 if(!eg && hasLastGroupField){
435 this.mainBody.update('');
436 this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
437 delete this.lastGroupField;
438 }else if (eg && !hasLastGroupField){
439 this.lastGroupField = groupField;
440 this.cm.setHidden(colIndex, true);
441 }else if (eg && hasLastGroupField && groupField !== this.lastGroupField) {
442 this.mainBody.update('');
443 var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
444 this.cm.setHidden(oldIndex, false);
445 this.lastGroupField = groupField;
446 this.cm.setHidden(colIndex, true);
449 return Ext.grid.GroupingView.superclass.renderRows.apply(
454 doRender : function(cs, rs, ds, startRow, colCount, stripe){
459 if(!this.canGroup() || this.isUpdating){
460 return Ext.grid.GroupingView.superclass.doRender.apply(this, arguments);
463 var groupField = this.getGroupField(),
464 colIndex = this.cm.findColumnIndex(groupField),
466 gstyle = 'width:' + this.getTotalWidth() + ';',
467 cfg = this.cm.config[colIndex],
468 groupRenderer = cfg.groupRenderer || cfg.renderer,
469 prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
471 curGroup, i, len, gid;
473 for(i = 0, len = rs.length; i < len; i++){
474 var rowIndex = startRow + i,
476 gvalue = r.data[groupField];
478 g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
479 if(!curGroup || curGroup.group != g){
480 gid = this.constructId(gvalue, groupField, colIndex);
481 // if state is defined use it, however state is in terms of expanded
482 // so negate it, otherwise use the default.
483 this.state[gid] = !(Ext.isDefined(this.state[gid]) ? !this.state[gid] : this.startCollapsed);
491 cls: this.state[gid] ? '' : 'x-grid-group-collapsed',
494 groups.push(curGroup);
502 for(i = 0, len = groups.length; i < len; i++){
504 this.doGroupStart(buf, g, cs, ds, colCount);
505 buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
506 this, cs, g.rs, ds, g.startRow, colCount, stripe);
508 this.doGroupEnd(buf, g, cs, ds, colCount);
513 <div id="method-Ext.grid.GroupingView-getGroupId"></div>/**
514 * Dynamically tries to determine the groupId of a specific value
515 * @param {String} value
516 * @return {String} The group id
518 getGroupId : function(value){
519 var field = this.getGroupField();
520 return this.constructId(value, field, this.cm.findColumnIndex(field));
524 constructId : function(value, field, idx){
525 var cfg = this.cm.config[idx],
526 groupRenderer = cfg.groupRenderer || cfg.renderer,
527 val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
529 return this.getPrefix(field) + Ext.util.Format.htmlEncode(val);
533 canGroup : function(){
534 return this.enableGrouping && !!this.getGroupField();
538 getPrefix: function(field){
539 return this.grid.getGridEl().id + '-gp-' + field + '-';
543 doGroupStart : function(buf, g, cs, ds, colCount){
544 buf[buf.length] = this.startGroup.apply(g);
548 doGroupEnd : function(buf, g, cs, ds, colCount){
549 buf[buf.length] = this.endGroup;
553 getRows : function(){
554 if(!this.canGroup()){
555 return Ext.grid.GroupingView.superclass.getRows.call(this);
558 gs = this.getGroups(),
565 g = gs[i].childNodes[1];
568 for(j = 0, jlen = g.length; j < jlen; ++j){
577 updateGroupWidths : function(){
578 if(!this.canGroup() || !this.hasRows()){
581 var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
582 var gs = this.getGroups();
583 for(var i = 0, len = gs.length; i < len; i++){
584 gs[i].firstChild.style.width = tw;
589 onColumnWidthUpdated : function(col, w, tw){
590 Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
591 this.updateGroupWidths();
595 onAllColumnWidthsUpdated : function(ws, tw){
596 Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
597 this.updateGroupWidths();
601 onColumnHiddenUpdated : function(col, hidden, tw){
602 Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
603 this.updateGroupWidths();
607 onLayout : function(){
608 this.updateGroupWidths();
612 onBeforeRowSelect : function(sm, rowIndex){
613 this.toggleRowIndex(rowIndex, true);
617 Ext.grid.GroupingView.GROUP_ID = 1000;</pre>