--- /dev/null
+<html>\r
+<head>\r
+ <title>The source code</title>\r
+ <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
+ <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
+</head>\r
+<body onload="prettyPrint();">\r
+ <pre class="prettyprint lang-js"><div id="cls-Ext.grid.GroupingView"></div>/**\r
+ * @class Ext.grid.GroupingView\r
+ * @extends Ext.grid.GridView\r
+ * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}\r
+ * must be used to enable grouping. Some grouping characteristics may also be configured at the\r
+ * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>\r
+ * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</li>\r
+ * <li><code>{@link Ext.grid.Column#groupable groupable}</li>\r
+ * <li><code>{@link Ext.grid.Column#groupName groupName}</li>\r
+ * <li><code>{@link Ext.grid.Column#groupRender groupRender}</li>\r
+ * </ul></div>\r
+ * <p>Sample usage:</p>\r
+ * <pre><code>\r
+var grid = new Ext.grid.GridPanel({\r
+ // A groupingStore is required for a GroupingView\r
+ store: new {@link Ext.data.GroupingStore}({\r
+ autoDestroy: true,\r
+ reader: reader,\r
+ data: xg.dummyData,\r
+ sortInfo: {field: 'company', direction: 'ASC'},\r
+ {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,\r
+ {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,\r
+ {@link Ext.data.GroupingStore#groupField groupField}: 'industry'\r
+ }),\r
+ colModel: new {@link Ext.grid.ColumnModel}({\r
+ columns:[\r
+ {id:'company',header: 'Company', width: 60, dataIndex: 'company'},\r
+ // {@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
+ {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},\r
+ {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},\r
+ {header: 'Industry', dataIndex: 'industry'},\r
+ {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}\r
+ ],\r
+ defaults: {\r
+ sortable: true,\r
+ menuDisabled: false,\r
+ width: 20\r
+ }\r
+ }),\r
+\r
+ view: new Ext.grid.GroupingView({\r
+ {@link Ext.grid.GridView#forceFit forceFit}: true,\r
+ // custom grouping text template to display the number of items per group\r
+ {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'\r
+ }),\r
+\r
+ frame:true,\r
+ width: 700,\r
+ height: 450,\r
+ collapsible: true,\r
+ animCollapse: false,\r
+ title: 'Grouping Example',\r
+ iconCls: 'icon-grid',\r
+ renderTo: document.body\r
+});\r
+ * </code></pre>\r
+ * @constructor\r
+ * @param {Object} config\r
+ */\r
+Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {\r
+\r
+ <div id="cfg-Ext.grid.GroupingView-groupByText"></div>/**\r
+ * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column\r
+ * (defaults to 'Group By This Field').\r
+ */\r
+ groupByText : 'Group By This Field',\r
+ <div id="cfg-Ext.grid.GroupingView-showGroupsText"></div>/**\r
+ * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping\r
+ * (defaults to 'Show in Groups').\r
+ */\r
+ showGroupsText : 'Show in Groups',\r
+ <div id="cfg-Ext.grid.GroupingView-hideGroupedColumn"></div>/**\r
+ * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)\r
+ */\r
+ hideGroupedColumn : false,\r
+ <div id="cfg-Ext.grid.GroupingView-showGroupName"></div>/**\r
+ * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value\r
+ * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>\r
+ * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the\r
+ * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).\r
+ */\r
+ showGroupName : true,\r
+ <div id="cfg-Ext.grid.GroupingView-startCollapsed"></div>/**\r
+ * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)\r
+ */\r
+ startCollapsed : false,\r
+ <div id="cfg-Ext.grid.GroupingView-enableGrouping"></div>/**\r
+ * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)\r
+ */\r
+ enableGrouping : true,\r
+ <div id="cfg-Ext.grid.GroupingView-enableGroupingMenu"></div>/**\r
+ * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)\r
+ */\r
+ enableGroupingMenu : true,\r
+ <div id="cfg-Ext.grid.GroupingView-enableNoGroups"></div>/**\r
+ * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)\r
+ */\r
+ enableNoGroups : true,\r
+ <div id="cfg-Ext.grid.GroupingView-emptyGroupText"></div>/**\r
+ * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).\r
+ * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.\r
+ */\r
+ emptyGroupText : '(None)',\r
+ <div id="cfg-Ext.grid.GroupingView-ignoreAdd"></div>/**\r
+ * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)\r
+ */\r
+ ignoreAdd : false,\r
+ <div id="cfg-Ext.grid.GroupingView-groupTextTpl"></div>/**\r
+ * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).\r
+ * This is used to format an object which contains the following properties:\r
+ * <div class="mdetail-params"><ul>\r
+ * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.\r
+ * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>\r
+ * is specified, it is the result of a call to that function.</p></li>\r
+ * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>\r
+ * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>\r
+ * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>\r
+ * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the\r
+ * View Element which contains the group.</p></li>\r
+ * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>\r
+ * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data\r
+ * for the row which caused group change.</p></li>\r
+ * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>\r
+ * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>\r
+ * </ul></div></p>\r
+ * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>\r
+var grid = new Ext.grid.GridPanel({\r
+ ...\r
+ view: new Ext.grid.GroupingView({\r
+ groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'\r
+ }),\r
+});\r
+ * </code></pre>\r
+ */\r
+ groupTextTpl : '{text}',\r
+ <div id="cfg-Ext.grid.GroupingView-groupRenderer"></div>/**\r
+ * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for\r
+ * each column.\r
+ */\r
+\r
+ // private\r
+ gidSeed : 1000,\r
+\r
+ // private\r
+ initTemplates : function(){\r
+ Ext.grid.GroupingView.superclass.initTemplates.call(this);\r
+ this.state = {};\r
+\r
+ var sm = this.grid.getSelectionModel();\r
+ sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',\r
+ this.onBeforeRowSelect, this);\r
+\r
+ if(!this.startGroup){\r
+ this.startGroup = new Ext.XTemplate(\r
+ '<div id="{groupId}" class="x-grid-group {cls}">',\r
+ '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',\r
+ '<div id="{groupId}-bd" class="x-grid-group-body">'\r
+ );\r
+ }\r
+ this.startGroup.compile();\r
+ this.endGroup = '</div></div>';\r
+ },\r
+\r
+ // private\r
+ findGroup : function(el){\r
+ return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);\r
+ },\r
+\r
+ // private\r
+ getGroups : function(){\r
+ return this.hasRows() ? this.mainBody.dom.childNodes : [];\r
+ },\r
+\r
+ // private\r
+ onAdd : function(){\r
+ if(this.enableGrouping && !this.ignoreAdd){\r
+ var ss = this.getScrollState();\r
+ this.refresh();\r
+ this.restoreScroll(ss);\r
+ }else if(!this.enableGrouping){\r
+ Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);\r
+ }\r
+ },\r
+\r
+ // private\r
+ onRemove : function(ds, record, index, isUpdate){\r
+ Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);\r
+ var g = document.getElementById(record._groupId);\r
+ if(g && g.childNodes[1].childNodes.length < 1){\r
+ Ext.removeNode(g);\r
+ }\r
+ this.applyEmptyText();\r
+ },\r
+\r
+ // private\r
+ refreshRow : function(record){\r
+ if(this.ds.getCount()==1){\r
+ this.refresh();\r
+ }else{\r
+ this.isUpdating = true;\r
+ Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);\r
+ this.isUpdating = false;\r
+ }\r
+ },\r
+\r
+ // private\r
+ beforeMenuShow : function(){\r
+ var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;\r
+ if((item = items.get('groupBy'))){\r
+ item.setDisabled(disabled);\r
+ }\r
+ if((item = items.get('showGroups'))){\r
+ item.setDisabled(disabled);\r
+ item.setChecked(!!this.getGroupField(), true);\r
+ }\r
+ },\r
+\r
+ // private\r
+ renderUI : function(){\r
+ Ext.grid.GroupingView.superclass.renderUI.call(this);\r
+ this.mainBody.on('mousedown', this.interceptMouse, this);\r
+\r
+ if(this.enableGroupingMenu && this.hmenu){\r
+ this.hmenu.add('-',{\r
+ itemId:'groupBy',\r
+ text: this.groupByText,\r
+ handler: this.onGroupByClick,\r
+ scope: this,\r
+ iconCls:'x-group-by-icon'\r
+ });\r
+ if(this.enableNoGroups){\r
+ this.hmenu.add({\r
+ itemId:'showGroups',\r
+ text: this.showGroupsText,\r
+ checked: true,\r
+ checkHandler: this.onShowGroupsClick,\r
+ scope: this\r
+ });\r
+ }\r
+ this.hmenu.on('beforeshow', this.beforeMenuShow, this);\r
+ }\r
+ },\r
+\r
+ // private\r
+ onGroupByClick : function(){\r
+ this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));\r
+ this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups\r
+ },\r
+\r
+ // private\r
+ onShowGroupsClick : function(mi, checked){\r
+ if(checked){\r
+ this.onGroupByClick();\r
+ }else{\r
+ this.grid.store.clearGrouping();\r
+ }\r
+ },\r
+\r
+ <div id="method-Ext.grid.GroupingView-toggleGroup"></div>/**\r
+ * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.\r
+ * @param {String} groupId The groupId assigned to the group (see getGroupId)\r
+ * @param {Boolean} expanded (optional)\r
+ */\r
+ toggleGroup : function(group, expanded){\r
+ this.grid.stopEditing(true);\r
+ group = Ext.getDom(group);\r
+ var gel = Ext.fly(group);\r
+ expanded = expanded !== undefined ?\r
+ expanded : gel.hasClass('x-grid-group-collapsed');\r
+\r
+ this.state[gel.dom.id] = expanded;\r
+ gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');\r
+ },\r
+\r
+ <div id="method-Ext.grid.GroupingView-toggleAllGroups"></div>/**\r
+ * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.\r
+ * @param {Boolean} expanded (optional)\r
+ */\r
+ toggleAllGroups : function(expanded){\r
+ var groups = this.getGroups();\r
+ for(var i = 0, len = groups.length; i < len; i++){\r
+ this.toggleGroup(groups[i], expanded);\r
+ }\r
+ },\r
+\r
+ <div id="method-Ext.grid.GroupingView-expandAllGroups"></div>/**\r
+ * Expands all grouped rows.\r
+ */\r
+ expandAllGroups : function(){\r
+ this.toggleAllGroups(true);\r
+ },\r
+\r
+ <div id="method-Ext.grid.GroupingView-collapseAllGroups"></div>/**\r
+ * Collapses all grouped rows.\r
+ */\r
+ collapseAllGroups : function(){\r
+ this.toggleAllGroups(false);\r
+ },\r
+\r
+ // private\r
+ interceptMouse : function(e){\r
+ var hd = e.getTarget('.x-grid-group-hd', this.mainBody);\r
+ if(hd){\r
+ e.stopEvent();\r
+ this.toggleGroup(hd.parentNode);\r
+ }\r
+ },\r
+\r
+ // private\r
+ getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){\r
+ var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);\r
+ if(g === ''){\r
+ g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;\r
+ }\r
+ return g;\r
+ },\r
+\r
+ // private\r
+ getGroupField : function(){\r
+ return this.grid.store.getGroupState();\r
+ },\r
+ \r
+ // private\r
+ afterRender : function(){\r
+ Ext.grid.GroupingView.superclass.afterRender.call(this);\r
+ if(this.grid.deferRowRender){\r
+ this.updateGroupWidths();\r
+ }\r
+ },\r
+\r
+ // private\r
+ renderRows : function(){\r
+ var groupField = this.getGroupField();\r
+ var eg = !!groupField;\r
+ // if they turned off grouping and the last grouped field is hidden\r
+ if(this.hideGroupedColumn) {\r
+ var colIndex = this.cm.findColumnIndex(groupField);\r
+ if(!eg && this.lastGroupField !== undefined) {\r
+ this.mainBody.update('');\r
+ this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);\r
+ delete this.lastGroupField;\r
+ }else if (eg && this.lastGroupField === undefined) {\r
+ this.lastGroupField = groupField;\r
+ this.cm.setHidden(colIndex, true);\r
+ }else if (eg && this.lastGroupField !== undefined && groupField !== this.lastGroupField) {\r
+ this.mainBody.update('');\r
+ var oldIndex = this.cm.findColumnIndex(this.lastGroupField);\r
+ this.cm.setHidden(oldIndex, false);\r
+ this.lastGroupField = groupField;\r
+ this.cm.setHidden(colIndex, true);\r
+ }\r
+ }\r
+ return Ext.grid.GroupingView.superclass.renderRows.apply(\r
+ this, arguments);\r
+ },\r
+\r
+ // private\r
+ doRender : function(cs, rs, ds, startRow, colCount, stripe){\r
+ if(rs.length < 1){\r
+ return '';\r
+ }\r
+ var groupField = this.getGroupField(),\r
+ colIndex = this.cm.findColumnIndex(groupField),\r
+ g;\r
+\r
+ this.enableGrouping = !!groupField;\r
+\r
+ if(!this.enableGrouping || this.isUpdating){\r
+ return Ext.grid.GroupingView.superclass.doRender.apply(\r
+ this, arguments);\r
+ }\r
+ var gstyle = 'width:'+this.getTotalWidth()+';';\r
+\r
+ var gidPrefix = this.grid.getGridEl().id;\r
+ var cfg = this.cm.config[colIndex];\r
+ var groupRenderer = cfg.groupRenderer || cfg.renderer;\r
+ var prefix = this.showGroupName ?\r
+ (cfg.groupName || cfg.header)+': ' : '';\r
+\r
+ var groups = [], curGroup, i, len, gid;\r
+ for(i = 0, len = rs.length; i < len; i++){\r
+ var rowIndex = startRow + i,\r
+ r = rs[i],\r
+ gvalue = r.data[groupField];\r
+ \r
+ g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);\r
+ if(!curGroup || curGroup.group != g){\r
+ gid = gidPrefix + '-gp-' + groupField + '-' + Ext.util.Format.htmlEncode(g);\r
+ // if state is defined use it, however state is in terms of expanded\r
+ // so negate it, otherwise use the default.\r
+ var isCollapsed = typeof this.state[gid] !== 'undefined' ? !this.state[gid] : this.startCollapsed;\r
+ var gcls = isCollapsed ? 'x-grid-group-collapsed' : ''; \r
+ curGroup = {\r
+ group: g,\r
+ gvalue: gvalue,\r
+ text: prefix + g,\r
+ groupId: gid,\r
+ startRow: rowIndex,\r
+ rs: [r],\r
+ cls: gcls,\r
+ style: gstyle\r
+ };\r
+ groups.push(curGroup);\r
+ }else{\r
+ curGroup.rs.push(r);\r
+ }\r
+ r._groupId = gid;\r
+ }\r
+\r
+ var buf = [];\r
+ for(i = 0, len = groups.length; i < len; i++){\r
+ g = groups[i];\r
+ this.doGroupStart(buf, g, cs, ds, colCount);\r
+ buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(\r
+ this, cs, g.rs, ds, g.startRow, colCount, stripe);\r
+\r
+ this.doGroupEnd(buf, g, cs, ds, colCount);\r
+ }\r
+ return buf.join('');\r
+ },\r
+\r
+ <div id="method-Ext.grid.GroupingView-getGroupId"></div>/**\r
+ * Dynamically tries to determine the groupId of a specific value\r
+ * @param {String} value\r
+ * @return {String} The group id\r
+ */\r
+ getGroupId : function(value){\r
+ var gidPrefix = this.grid.getGridEl().id;\r
+ var groupField = this.getGroupField();\r
+ var colIndex = this.cm.findColumnIndex(groupField);\r
+ var cfg = this.cm.config[colIndex];\r
+ var groupRenderer = cfg.groupRenderer || cfg.renderer;\r
+ var gtext = this.getGroup(value, {data:{}}, groupRenderer, 0, colIndex, this.ds);\r
+ return gidPrefix + '-gp-' + groupField + '-' + Ext.util.Format.htmlEncode(value);\r
+ },\r
+\r
+ // private\r
+ doGroupStart : function(buf, g, cs, ds, colCount){\r
+ buf[buf.length] = this.startGroup.apply(g);\r
+ },\r
+\r
+ // private\r
+ doGroupEnd : function(buf, g, cs, ds, colCount){\r
+ buf[buf.length] = this.endGroup;\r
+ },\r
+\r
+ // private\r
+ getRows : function(){\r
+ if(!this.enableGrouping){\r
+ return Ext.grid.GroupingView.superclass.getRows.call(this);\r
+ }\r
+ var r = [];\r
+ var g, gs = this.getGroups();\r
+ for(var i = 0, len = gs.length; i < len; i++){\r
+ g = gs[i].childNodes[1].childNodes;\r
+ for(var j = 0, jlen = g.length; j < jlen; j++){\r
+ r[r.length] = g[j];\r
+ }\r
+ }\r
+ return r;\r
+ },\r
+\r
+ // private\r
+ updateGroupWidths : function(){\r
+ if(!this.enableGrouping || !this.hasRows()){\r
+ return;\r
+ }\r
+ var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.scrollOffset) +'px';\r
+ var gs = this.getGroups();\r
+ for(var i = 0, len = gs.length; i < len; i++){\r
+ gs[i].firstChild.style.width = tw;\r
+ }\r
+ },\r
+\r
+ // private\r
+ onColumnWidthUpdated : function(col, w, tw){\r
+ Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);\r
+ this.updateGroupWidths();\r
+ },\r
+\r
+ // private\r
+ onAllColumnWidthsUpdated : function(ws, tw){\r
+ Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);\r
+ this.updateGroupWidths();\r
+ },\r
+\r
+ // private\r
+ onColumnHiddenUpdated : function(col, hidden, tw){\r
+ Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);\r
+ this.updateGroupWidths();\r
+ },\r
+\r
+ // private\r
+ onLayout : function(){\r
+ this.updateGroupWidths();\r
+ },\r
+\r
+ // private\r
+ onBeforeRowSelect : function(sm, rowIndex){\r
+ if(!this.enableGrouping){\r
+ return;\r
+ }\r
+ var row = this.getRow(rowIndex);\r
+ if(row && !row.offsetParent){\r
+ var g = this.findGroup(row);\r
+ this.toggleGroup(g, true);\r
+ }\r
+ }\r
+});\r
+// private\r
+Ext.grid.GroupingView.GROUP_ID = 1000;</pre> \r
+</body>\r
+</html>
\ No newline at end of file