3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.grid.GroupingView"></div>/**
\r
10 * @class Ext.grid.GroupingView
\r
11 * @extends Ext.grid.GridView
\r
12 * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}
\r
13 * must be used to enable grouping. Some grouping characteristics may also be configured at the
\r
14 * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>
\r
15 * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</li>
\r
16 * <li><code>{@link Ext.grid.Column#groupable groupable}</li>
\r
17 * <li><code>{@link Ext.grid.Column#groupName groupName}</li>
\r
18 * <li><code>{@link Ext.grid.Column#groupRender groupRender}</li>
\r
20 * <p>Sample usage:</p>
\r
22 var grid = new Ext.grid.GridPanel({
\r
23 // A groupingStore is required for a GroupingView
\r
24 store: new {@link Ext.data.GroupingStore}({
\r
28 sortInfo: {field: 'company', direction: 'ASC'},
\r
29 {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,
\r
30 {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,
\r
31 {@link Ext.data.GroupingStore#groupField groupField}: 'industry'
\r
33 colModel: new {@link Ext.grid.ColumnModel}({
\r
35 {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
\r
36 // {@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
37 {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},
\r
38 {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
\r
39 {header: 'Industry', dataIndex: 'industry'},
\r
40 {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
\r
44 menuDisabled: false,
\r
49 view: new Ext.grid.GroupingView({
\r
50 {@link Ext.grid.GridView#forceFit forceFit}: true,
\r
51 // custom grouping text template to display the number of items per group
\r
52 {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
59 animCollapse: false,
\r
60 title: 'Grouping Example',
\r
61 iconCls: 'icon-grid',
\r
62 renderTo: document.body
\r
66 * @param {Object} config
\r
68 Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {
\r
70 <div id="cfg-Ext.grid.GroupingView-groupByText"></div>/**
\r
71 * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column
\r
72 * (defaults to 'Group By This Field').
\r
74 groupByText : 'Group By This Field',
\r
75 <div id="cfg-Ext.grid.GroupingView-showGroupsText"></div>/**
\r
76 * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping
\r
77 * (defaults to 'Show in Groups').
\r
79 showGroupsText : 'Show in Groups',
\r
80 <div id="cfg-Ext.grid.GroupingView-hideGroupedColumn"></div>/**
\r
81 * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)
\r
83 hideGroupedColumn : false,
\r
84 <div id="cfg-Ext.grid.GroupingView-showGroupName"></div>/**
\r
85 * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value
\r
86 * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>
\r
87 * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the
\r
88 * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).
\r
90 showGroupName : true,
\r
91 <div id="cfg-Ext.grid.GroupingView-startCollapsed"></div>/**
\r
92 * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)
\r
94 startCollapsed : false,
\r
95 <div id="cfg-Ext.grid.GroupingView-enableGrouping"></div>/**
\r
96 * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)
\r
98 enableGrouping : true,
\r
99 <div id="cfg-Ext.grid.GroupingView-enableGroupingMenu"></div>/**
\r
100 * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)
\r
102 enableGroupingMenu : true,
\r
103 <div id="cfg-Ext.grid.GroupingView-enableNoGroups"></div>/**
\r
104 * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)
\r
106 enableNoGroups : true,
\r
107 <div id="cfg-Ext.grid.GroupingView-emptyGroupText"></div>/**
\r
108 * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).
\r
109 * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.
\r
111 emptyGroupText : '(None)',
\r
112 <div id="cfg-Ext.grid.GroupingView-ignoreAdd"></div>/**
\r
113 * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)
\r
116 <div id="cfg-Ext.grid.GroupingView-groupTextTpl"></div>/**
\r
117 * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).
\r
118 * This is used to format an object which contains the following properties:
\r
119 * <div class="mdetail-params"><ul>
\r
120 * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.
\r
121 * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>
\r
122 * is specified, it is the result of a call to that function.</p></li>
\r
123 * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>
\r
124 * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>
\r
125 * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>
\r
126 * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the
\r
127 * View Element which contains the group.</p></li>
\r
128 * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>
\r
129 * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data
\r
130 * for the row which caused group change.</p></li>
\r
131 * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>
\r
132 * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>
\r
134 * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>
\r
135 var grid = new Ext.grid.GridPanel({
\r
137 view: new Ext.grid.GroupingView({
\r
138 groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
\r
143 groupTextTpl : '{text}',
\r
145 <div id="cfg-Ext.grid.GroupingView-groupMode"></div>/**
\r
146 * @cfg {String} groupMode Indicates how to construct the group identifier. <tt>'value'</tt> constructs the id using
\r
147 * raw value, <tt>'display'</tt> constructs the id using the rendered value. Defaults to <tt>'value'</tt>.
\r
149 groupMode: 'value',
\r
151 <div id="cfg-Ext.grid.GroupingView-groupRenderer"></div>/**
\r
152 * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for
\r
160 initTemplates : function(){
\r
161 Ext.grid.GroupingView.superclass.initTemplates.call(this);
\r
164 var sm = this.grid.getSelectionModel();
\r
165 sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
\r
166 this.onBeforeRowSelect, this);
\r
168 if(!this.startGroup){
\r
169 this.startGroup = new Ext.XTemplate(
\r
170 '<div id="{groupId}" class="x-grid-group {cls}">',
\r
171 '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
\r
172 '<div id="{groupId}-bd" class="x-grid-group-body">'
\r
175 this.startGroup.compile();
\r
176 if(!this.endGroup){
\r
177 this.endGroup = '</div></div>';
\r
180 this.endGroup = '</div></div>';
\r
184 findGroup : function(el){
\r
185 return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
\r
189 getGroups : function(){
\r
190 return this.hasRows() ? this.mainBody.dom.childNodes : [];
\r
194 onAdd : function(){
\r
195 if(this.enableGrouping && !this.ignoreAdd){
\r
196 var ss = this.getScrollState();
\r
198 this.restoreScroll(ss);
\r
199 }else if(!this.enableGrouping){
\r
200 Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
\r
205 onRemove : function(ds, record, index, isUpdate){
\r
206 Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
\r
207 var g = document.getElementById(record._groupId);
\r
208 if(g && g.childNodes[1].childNodes.length < 1){
\r
211 this.applyEmptyText();
\r
215 refreshRow : function(record){
\r
216 if(this.ds.getCount()==1){
\r
219 this.isUpdating = true;
\r
220 Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
\r
221 this.isUpdating = false;
\r
226 beforeMenuShow : function(){
\r
227 var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
\r
228 if((item = items.get('groupBy'))){
\r
229 item.setDisabled(disabled);
\r
231 if((item = items.get('showGroups'))){
\r
232 item.setDisabled(disabled);
\r
233 item.setChecked(this.enableGrouping, true);
\r
238 renderUI : function(){
\r
239 Ext.grid.GroupingView.superclass.renderUI.call(this);
\r
240 this.mainBody.on('mousedown', this.interceptMouse, this);
\r
242 if(this.enableGroupingMenu && this.hmenu){
\r
243 this.hmenu.add('-',{
\r
245 text: this.groupByText,
\r
246 handler: this.onGroupByClick,
\r
248 iconCls:'x-group-by-icon'
\r
250 if(this.enableNoGroups){
\r
252 itemId:'showGroups',
\r
253 text: this.showGroupsText,
\r
255 checkHandler: this.onShowGroupsClick,
\r
259 this.hmenu.on('beforeshow', this.beforeMenuShow, this);
\r
263 processEvent: function(name, e){
\r
264 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
\r
266 // group value is at the end of the string
\r
267 var field = this.getGroupField(),
\r
268 prefix = this.getPrefix(field),
\r
269 groupValue = hd.id.substring(prefix.length);
\r
271 // remove trailing '-hd'
\r
272 groupValue = groupValue.substr(0, groupValue.length - 3);
\r
274 this.grid.fireEvent('group' + name, this.grid, field, groupValue, e);
\r
281 onGroupByClick : function(){
\r
282 this.enableGrouping = true;
\r
283 this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
\r
284 this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
\r
289 onShowGroupsClick : function(mi, checked){
\r
290 this.enableGrouping = checked;
\r
292 this.onGroupByClick();
\r
294 this.grid.store.clearGrouping();
\r
298 <div id="method-Ext.grid.GroupingView-toggleRowIndex"></div>/**
\r
299 * Toggle the group that contains the specific row.
\r
300 * @param {Number} rowIndex The row inside the group
\r
301 * @param {Boolean} expanded (optional)
\r
303 toggleRowIndex : function(rowIndex, expanded){
\r
304 if(!this.enableGrouping){
\r
307 var row = this.getRow(rowIndex);
\r
309 this.toggleGroup(this.findGroup(row), expanded);
\r
313 <div id="method-Ext.grid.GroupingView-toggleGroup"></div>/**
\r
314 * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
\r
315 * @param {String} groupId The groupId assigned to the group (see getGroupId)
\r
316 * @param {Boolean} expanded (optional)
\r
318 toggleGroup : function(group, expanded){
\r
319 var gel = Ext.get(group);
\r
320 expanded = Ext.isDefined(expanded) ? expanded : gel.hasClass('x-grid-group-collapsed');
\r
321 if(this.state[gel.id] !== expanded){
\r
322 this.grid.stopEditing(true);
\r
323 this.state[gel.id] = expanded;
\r
324 gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
\r
328 <div id="method-Ext.grid.GroupingView-toggleAllGroups"></div>/**
\r
329 * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
\r
330 * @param {Boolean} expanded (optional)
\r
332 toggleAllGroups : function(expanded){
\r
333 var groups = this.getGroups();
\r
334 for(var i = 0, len = groups.length; i < len; i++){
\r
335 this.toggleGroup(groups[i], expanded);
\r
339 <div id="method-Ext.grid.GroupingView-expandAllGroups"></div>/**
\r
340 * Expands all grouped rows.
\r
342 expandAllGroups : function(){
\r
343 this.toggleAllGroups(true);
\r
346 <div id="method-Ext.grid.GroupingView-collapseAllGroups"></div>/**
\r
347 * Collapses all grouped rows.
\r
349 collapseAllGroups : function(){
\r
350 this.toggleAllGroups(false);
\r
354 interceptMouse : function(e){
\r
355 var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
\r
358 this.toggleGroup(hd.parentNode);
\r
363 getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
\r
364 var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);
\r
365 if(g === '' || g === ' '){
\r
366 g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;
\r
372 getGroupField : function(){
\r
373 return this.grid.store.getGroupState();
\r
377 afterRender : function(){
\r
378 Ext.grid.GroupingView.superclass.afterRender.call(this);
\r
379 if(this.grid.deferRowRender){
\r
380 this.updateGroupWidths();
\r
385 renderRows : function(){
\r
386 var groupField = this.getGroupField();
\r
387 var eg = !!groupField;
\r
388 // if they turned off grouping and the last grouped field is hidden
\r
389 if(this.hideGroupedColumn) {
\r
390 var colIndex = this.cm.findColumnIndex(groupField),
\r
391 hasLastGroupField = Ext.isDefined(this.lastGroupField);
\r
392 if(!eg && hasLastGroupField){
\r
393 this.mainBody.update('');
\r
394 this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
\r
395 delete this.lastGroupField;
\r
396 }else if (eg && !hasLastGroupField){
\r
397 this.lastGroupField = groupField;
\r
398 this.cm.setHidden(colIndex, true);
\r
399 }else if (eg && hasLastGroupField && groupField !== this.lastGroupField) {
\r
400 this.mainBody.update('');
\r
401 var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
\r
402 this.cm.setHidden(oldIndex, false);
\r
403 this.lastGroupField = groupField;
\r
404 this.cm.setHidden(colIndex, true);
\r
407 return Ext.grid.GroupingView.superclass.renderRows.apply(
\r
412 doRender : function(cs, rs, ds, startRow, colCount, stripe){
\r
416 var groupField = this.getGroupField(),
\r
417 colIndex = this.cm.findColumnIndex(groupField),
\r
420 this.enableGrouping = (this.enableGrouping === false) ? false : !!groupField;
\r
422 if(!this.enableGrouping || this.isUpdating){
\r
423 return Ext.grid.GroupingView.superclass.doRender.apply(
\r
426 var gstyle = 'width:' + this.getTotalWidth() + ';',
\r
427 cfg = this.cm.config[colIndex],
\r
428 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
429 prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
\r
431 curGroup, i, len, gid;
\r
433 for(i = 0, len = rs.length; i < len; i++){
\r
434 var rowIndex = startRow + i,
\r
436 gvalue = r.data[groupField];
\r
438 g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
\r
439 if(!curGroup || curGroup.group != g){
\r
440 gid = this.constructId(gvalue, groupField, colIndex);
\r
441 // if state is defined use it, however state is in terms of expanded
\r
442 // so negate it, otherwise use the default.
\r
443 this.state[gid] = !(Ext.isDefined(this.state[gid]) ? !this.state[gid] : this.startCollapsed);
\r
449 startRow: rowIndex,
\r
451 cls: this.state[gid] ? '' : 'x-grid-group-collapsed',
\r
454 groups.push(curGroup);
\r
456 curGroup.rs.push(r);
\r
462 for(i = 0, len = groups.length; i < len; i++){
\r
464 this.doGroupStart(buf, g, cs, ds, colCount);
\r
465 buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
\r
466 this, cs, g.rs, ds, g.startRow, colCount, stripe);
\r
468 this.doGroupEnd(buf, g, cs, ds, colCount);
\r
470 return buf.join('');
\r
473 <div id="method-Ext.grid.GroupingView-getGroupId"></div>/**
\r
474 * Dynamically tries to determine the groupId of a specific value
\r
475 * @param {String} value
\r
476 * @return {String} The group id
\r
478 getGroupId : function(value){
\r
479 var field = this.getGroupField();
\r
480 return this.constructId(value, field, this.cm.findColumnIndex(field));
\r
484 constructId : function(value, field, idx){
\r
485 var cfg = this.cm.config[idx],
\r
486 groupRenderer = cfg.groupRenderer || cfg.renderer,
\r
487 val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
\r
489 return this.getPrefix(field) + Ext.util.Format.htmlEncode(val);
\r
493 getPrefix: function(field){
\r
494 return this.grid.getGridEl().id + '-gp-' + field + '-';
\r
498 doGroupStart : function(buf, g, cs, ds, colCount){
\r
499 buf[buf.length] = this.startGroup.apply(g);
\r
503 doGroupEnd : function(buf, g, cs, ds, colCount){
\r
504 buf[buf.length] = this.endGroup;
\r
508 getRows : function(){
\r
509 if(!this.enableGrouping){
\r
510 return Ext.grid.GroupingView.superclass.getRows.call(this);
\r
513 var g, gs = this.getGroups();
\r
514 for(var i = 0, len = gs.length; i < len; i++){
\r
515 g = gs[i].childNodes[1].childNodes;
\r
516 for(var j = 0, jlen = g.length; j < jlen; j++){
\r
517 r[r.length] = g[j];
\r
524 updateGroupWidths : function(){
\r
525 if(!this.enableGrouping || !this.hasRows()){
\r
528 var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
\r
529 var gs = this.getGroups();
\r
530 for(var i = 0, len = gs.length; i < len; i++){
\r
531 gs[i].firstChild.style.width = tw;
\r
536 onColumnWidthUpdated : function(col, w, tw){
\r
537 Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
\r
538 this.updateGroupWidths();
\r
542 onAllColumnWidthsUpdated : function(ws, tw){
\r
543 Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
\r
544 this.updateGroupWidths();
\r
548 onColumnHiddenUpdated : function(col, hidden, tw){
\r
549 Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
\r
550 this.updateGroupWidths();
\r
554 onLayout : function(){
\r
555 this.updateGroupWidths();
\r
559 onBeforeRowSelect : function(sm, rowIndex){
\r
560 this.toggleRowIndex(rowIndex, true);
\r
564 Ext.grid.GroupingView.GROUP_ID = 1000;</pre>
\r