Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / GroupingStore.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 <div id="cls-Ext.data.GroupingStore"></div>/**\r
15  * @class Ext.data.GroupingStore\r
16  * @extends Ext.data.Store\r
17  * A specialized store implementation that provides for grouping records by one of the available fields. This\r
18  * is usually used in conjunction with an {@link Ext.grid.GroupingView} to proved the data model for\r
19  * a grouped GridPanel.\r
20  * @constructor\r
21  * Creates a new GroupingStore.\r
22  * @param {Object} config A config object containing the objects needed for the Store to access data,\r
23  * and read the data into Records.\r
24  * @xtype groupingstore\r
25  */\r
26 Ext.data.GroupingStore = Ext.extend(Ext.data.Store, {\r
27     \r
28     //inherit docs\r
29     constructor: function(config){\r
30         Ext.data.GroupingStore.superclass.constructor.call(this, config);\r
31         this.applyGroupField();\r
32     },\r
33     \r
34     <div id="cfg-Ext.data.GroupingStore-groupField"></div>/**\r
35      * @cfg {String} groupField\r
36      * The field name by which to sort the store's data (defaults to '').\r
37      */\r
38     <div id="cfg-Ext.data.GroupingStore-remoteGroup"></div>/**\r
39      * @cfg {Boolean} remoteGroup\r
40      * True if the grouping should apply on the server side, false if it is local only (defaults to false).  If the\r
41      * grouping is local, it can be applied immediately to the data.  If it is remote, then it will simply act as a\r
42      * helper, automatically sending the grouping field name as the 'groupBy' param with each XHR call.\r
43      */\r
44     remoteGroup : false,\r
45     <div id="cfg-Ext.data.GroupingStore-groupOnSort"></div>/**\r
46      * @cfg {Boolean} groupOnSort\r
47      * True to sort the data on the grouping field when a grouping operation occurs, false to sort based on the\r
48      * existing sort info (defaults to false).\r
49      */\r
50     groupOnSort:false,\r
51 \r
52         groupDir : 'ASC',\r
53         \r
54     <div id="method-Ext.data.GroupingStore-clearGrouping"></div>/**\r
55      * Clears any existing grouping and refreshes the data using the default sort.\r
56      */\r
57     clearGrouping : function(){\r
58         this.groupField = false;\r
59         if(this.remoteGroup){\r
60             if(this.baseParams){\r
61                 delete this.baseParams.groupBy;\r
62             }\r
63             var lo = this.lastOptions;\r
64             if(lo && lo.params){\r
65                 delete lo.params.groupBy;\r
66             }\r
67             this.reload();\r
68         }else{\r
69             this.applySort();\r
70             this.fireEvent('datachanged', this);\r
71         }\r
72     },\r
73 \r
74     <div id="method-Ext.data.GroupingStore-groupBy"></div>/**\r
75      * Groups the data by the specified field.\r
76      * @param {String} field The field name by which to sort the store's data\r
77      * @param {Boolean} forceRegroup (optional) True to force the group to be refreshed even if the field passed\r
78      * in is the same as the current grouping field, false to skip grouping on the same field (defaults to false)\r
79      */\r
80     groupBy : function(field, forceRegroup, direction){\r
81                 direction = direction ? (String(direction).toUpperCase() == 'DESC' ? 'DESC' : 'ASC') : this.groupDir;\r
82         if(this.groupField == field && this.groupDir == direction && !forceRegroup){\r
83             return; // already grouped by this field\r
84         }\r
85         this.groupField = field;\r
86                 this.groupDir = direction;\r
87         this.applyGroupField();\r
88         if(this.groupOnSort){\r
89             this.sort(field, direction);\r
90             return;\r
91         }\r
92         if(this.remoteGroup){\r
93             this.reload();\r
94         }else{\r
95             var si = this.sortInfo || {};\r
96             if(si.field != field || si.direction != direction){\r
97                 this.applySort();\r
98             }else{\r
99                 this.sortData(field, direction);\r
100             }\r
101             this.fireEvent('datachanged', this);\r
102         }\r
103     },\r
104     \r
105     // private\r
106     applyGroupField: function(){\r
107         if(this.remoteGroup){\r
108             if(!this.baseParams){\r
109                 this.baseParams = {};\r
110             }\r
111             this.baseParams.groupBy = this.groupField;\r
112             this.baseParams.groupDir = this.groupDir;\r
113         }\r
114     },\r
115 \r
116     // private\r
117     applySort : function(){\r
118         Ext.data.GroupingStore.superclass.applySort.call(this);\r
119         if(!this.groupOnSort && !this.remoteGroup){\r
120             var gs = this.getGroupState();\r
121             if(gs && (gs != this.sortInfo.field || this.groupDir != this.sortInfo.direction)){\r
122                 this.sortData(this.groupField, this.groupDir);\r
123             }\r
124         }\r
125     },\r
126 \r
127     // private\r
128     applyGrouping : function(alwaysFireChange){\r
129         if(this.groupField !== false){\r
130             this.groupBy(this.groupField, true, this.groupDir);\r
131             return true;\r
132         }else{\r
133             if(alwaysFireChange === true){\r
134                 this.fireEvent('datachanged', this);\r
135             }\r
136             return false;\r
137         }\r
138     },\r
139 \r
140     // private\r
141     getGroupState : function(){\r
142         return this.groupOnSort && this.groupField !== false ?\r
143                (this.sortInfo ? this.sortInfo.field : undefined) : this.groupField;\r
144     }\r
145 });\r
146 Ext.reg('groupingstore', Ext.data.GroupingStore);</pre>
147 </body>
148 </html>