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.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.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
163 initTemplates : function(){
164 Ext.grid.GroupingView.superclass.initTemplates.call(this);
167 var sm = this.grid.getSelectionModel();
168 sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
169 this.onBeforeRowSelect, this);
171 if(!this.startGroup){
172 this.startGroup = new Ext.XTemplate(
173 '<div id="{groupId}" class="x-grid-group {cls}">',
174 '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
175 '<div id="{groupId}-bd" class="x-grid-group-body">'
178 this.startGroup.compile();
180 if (!this.endGroup) {
181 this.endGroup = '</div></div>';
186 findGroup : function(el){
187 return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
191 getGroups : function(){
192 return this.hasRows() ? this.mainBody.dom.childNodes : [];
196 onAdd : function(ds, records, index) {
197 if (this.canGroup() && !this.ignoreAdd) {
198 var ss = this.getScrollState();
199 this.fireEvent('beforerowsinserted', ds, index, index + (records.length-1));
201 this.restoreScroll(ss);
202 this.fireEvent('rowsinserted', ds, index, index + (records.length-1));
203 } else if (!this.canGroup()) {
204 Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
209 onRemove : function(ds, record, index, isUpdate){
210 Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
211 var g = document.getElementById(record._groupId);
212 if(g && g.childNodes[1].childNodes.length < 1){
215 this.applyEmptyText();
219 refreshRow : function(record){
220 if(this.ds.getCount()==1){
223 this.isUpdating = true;
224 Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
225 this.isUpdating = false;
230 beforeMenuShow : function(){
231 var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
232 if((item = items.get('groupBy'))){
233 item.setDisabled(disabled);
235 if((item = items.get('showGroups'))){
236 item.setDisabled(disabled);
237 item.setChecked(this.enableGrouping, true);
242 renderUI : function(){
243 Ext.grid.GroupingView.superclass.renderUI.call(this);
244 this.mainBody.on('mousedown', this.interceptMouse, this);
246 if(this.enableGroupingMenu && this.hmenu){
249 text: this.groupByText,
250 handler: this.onGroupByClick,
252 iconCls:'x-group-by-icon'
254 if(this.enableNoGroups){
257 text: this.showGroupsText,
259 checkHandler: this.onShowGroupsClick,
263 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
267 processEvent: function(name, e){
268 Ext.grid.GroupingView.superclass.processEvent.call(this, name, e);
269 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
271 // group value is at the end of the string
272 var field = this.getGroupField(),
273 prefix = this.getPrefix(field),
274 groupValue = hd.id.substring(prefix.length);
276 // remove trailing '-hd'
277 groupValue = groupValue.substr(0, groupValue.length - 3);
279 this.grid.fireEvent('group' + name, this.grid, field, groupValue, e);
286 onGroupByClick : function(){
287 this.enableGrouping = true;
288 this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
289 this.grid.fireEvent('groupchange', this, this.grid.store.getGroupState());
290 this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
295 onShowGroupsClick : function(mi, checked){
296 this.enableGrouping = checked;
298 this.onGroupByClick();
300 this.grid.store.clearGrouping();
301 this.grid.fireEvent('groupchange', this, null);
305 <div id="method-Ext.grid.GroupingView-toggleRowIndex"></div>/**
306 * Toggle the group that contains the specific row.
307 * @param {Number} rowIndex The row inside the group
308 * @param {Boolean} expanded (optional)
310 toggleRowIndex : function(rowIndex, expanded){
311 if(!this.canGroup()){
314 var row = this.getRow(rowIndex);
316 this.toggleGroup(this.findGroup(row), expanded);
320 <div id="method-Ext.grid.GroupingView-toggleGroup"></div>/**
321 * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
322 * @param {String} groupId The groupId assigned to the group (see getGroupId)
323 * @param {Boolean} expanded (optional)
325 toggleGroup : function(group, expanded){
326 var gel = Ext.get(group);
327 expanded = Ext.isDefined(expanded) ? expanded : gel.hasClass('x-grid-group-collapsed');
328 if(this.state[gel.id] !== expanded){
329 this.grid.stopEditing(true);
330 this.state[gel.id] = expanded;
331 gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
335 <div id="method-Ext.grid.GroupingView-toggleAllGroups"></div>/**
336 * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
337 * @param {Boolean} expanded (optional)
339 toggleAllGroups : function(expanded){
340 var groups = this.getGroups();
341 for(var i = 0, len = groups.length; i < len; i++){
342 this.toggleGroup(groups[i], expanded);
346 <div id="method-Ext.grid.GroupingView-expandAllGroups"></div>/**
347 * Expands all grouped rows.
349 expandAllGroups : function(){
350 this.toggleAllGroups(true);
353 <div id="method-Ext.grid.GroupingView-collapseAllGroups"></div>/**
354 * Collapses all grouped rows.
356 collapseAllGroups : function(){
357 this.toggleAllGroups(false);
361 interceptMouse : function(e){
362 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
365 this.toggleGroup(hd.parentNode);
370 getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
371 var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);
372 if(g === '' || g === ' '){
373 g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;
379 getGroupField : function(){
380 return this.grid.store.getGroupState();
384 afterRender : function(){
385 if(!this.ds || !this.cm){
388 Ext.grid.GroupingView.superclass.afterRender.call(this);
389 if(this.grid.deferRowRender){
390 this.updateGroupWidths();
395 renderRows : function(){
396 var groupField = this.getGroupField();
397 var eg = !!groupField;
398 // if they turned off grouping and the last grouped field is hidden
399 if(this.hideGroupedColumn) {
400 var colIndex = this.cm.findColumnIndex(groupField),
401 hasLastGroupField = Ext.isDefined(this.lastGroupField);
402 if(!eg && hasLastGroupField){
403 this.mainBody.update('');
404 this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
405 delete this.lastGroupField;
406 }else if (eg && !hasLastGroupField){
407 this.lastGroupField = groupField;
408 this.cm.setHidden(colIndex, true);
409 }else if (eg && hasLastGroupField && groupField !== this.lastGroupField) {
410 this.mainBody.update('');
411 var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
412 this.cm.setHidden(oldIndex, false);
413 this.lastGroupField = groupField;
414 this.cm.setHidden(colIndex, true);
417 return Ext.grid.GroupingView.superclass.renderRows.apply(
422 doRender : function(cs, rs, ds, startRow, colCount, stripe){
427 if(!this.canGroup() || this.isUpdating){
428 return Ext.grid.GroupingView.superclass.doRender.apply(this, arguments);
431 var groupField = this.getGroupField(),
432 colIndex = this.cm.findColumnIndex(groupField),
434 gstyle = 'width:' + this.getTotalWidth() + ';',
435 cfg = this.cm.config[colIndex],
436 groupRenderer = cfg.groupRenderer || cfg.renderer,
437 prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
439 curGroup, i, len, gid;
441 for(i = 0, len = rs.length; i < len; i++){
442 var rowIndex = startRow + i,
444 gvalue = r.data[groupField];
446 g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
447 if(!curGroup || curGroup.group != g){
448 gid = this.constructId(gvalue, groupField, colIndex);
449 // if state is defined use it, however state is in terms of expanded
450 // so negate it, otherwise use the default.
451 this.state[gid] = !(Ext.isDefined(this.state[gid]) ? !this.state[gid] : this.startCollapsed);
459 cls: this.state[gid] ? '' : 'x-grid-group-collapsed',
462 groups.push(curGroup);
470 for(i = 0, len = groups.length; i < len; i++){
472 this.doGroupStart(buf, g, cs, ds, colCount);
473 buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
474 this, cs, g.rs, ds, g.startRow, colCount, stripe);
476 this.doGroupEnd(buf, g, cs, ds, colCount);
481 <div id="method-Ext.grid.GroupingView-getGroupId"></div>/**
482 * Dynamically tries to determine the groupId of a specific value
483 * @param {String} value
484 * @return {String} The group id
486 getGroupId : function(value){
487 var field = this.getGroupField();
488 return this.constructId(value, field, this.cm.findColumnIndex(field));
492 constructId : function(value, field, idx){
493 var cfg = this.cm.config[idx],
494 groupRenderer = cfg.groupRenderer || cfg.renderer,
495 val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
497 return this.getPrefix(field) + Ext.util.Format.htmlEncode(val);
501 canGroup : function(){
502 return this.enableGrouping && !!this.getGroupField();
506 getPrefix: function(field){
507 return this.grid.getGridEl().id + '-gp-' + field + '-';
511 doGroupStart : function(buf, g, cs, ds, colCount){
512 buf[buf.length] = this.startGroup.apply(g);
516 doGroupEnd : function(buf, g, cs, ds, colCount){
517 buf[buf.length] = this.endGroup;
521 getRows : function(){
522 if(!this.canGroup()){
523 return Ext.grid.GroupingView.superclass.getRows.call(this);
526 gs = this.getGroups(),
533 g = gs[i].childNodes[1];
536 for(j = 0, jlen = g.length; j < jlen; ++j){
545 updateGroupWidths : function(){
546 if(!this.canGroup() || !this.hasRows()){
549 var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
550 var gs = this.getGroups();
551 for(var i = 0, len = gs.length; i < len; i++){
552 gs[i].firstChild.style.width = tw;
557 onColumnWidthUpdated : function(col, w, tw){
558 Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
559 this.updateGroupWidths();
563 onAllColumnWidthsUpdated : function(ws, tw){
564 Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
565 this.updateGroupWidths();
569 onColumnHiddenUpdated : function(col, hidden, tw){
570 Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
571 this.updateGroupWidths();
575 onLayout : function(){
576 this.updateGroupWidths();
580 onBeforeRowSelect : function(sm, rowIndex){
581 this.toggleRowIndex(rowIndex, true);
585 Ext.grid.GroupingView.GROUP_ID = 1000;</pre>