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