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