You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
11 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-GroupingStore-method-constructor'><span id='Ext-data-GroupingStore'>/**
  19. </span></span> * @class Ext.data.GroupingStore
  20. * @extends Ext.data.Store
  21. * A specialized store implementation that provides for grouping records by one of the available fields. This
  22. * is usually used in conjunction with an {@link Ext.grid.GroupingView} to provide the data model for
  23. * a grouped GridPanel.
  24. *
  25. * Internally, GroupingStore is simply a normal Store with multi sorting enabled from the start. The grouping field
  26. * and direction are always injected as the first sorter pair. GroupingView picks up on the configured groupField and
  27. * builds grid rows appropriately.
  28. *
  29. * @constructor
  30. * Creates a new GroupingStore.
  31. * @param {Object} config A config object containing the objects needed for the Store to access data,
  32. * and read the data into Records.
  33. * @xtype groupingstore
  34. */
  35. Ext.data.GroupingStore = Ext.extend(Ext.data.Store, {
  36. <span id='Ext-data-GroupingStore-method-constructor'> //inherit docs
  37. </span> constructor: function(config) {
  38. config = config || {};
  39. //We do some preprocessing here to massage the grouping + sorting options into a single
  40. //multi sort array. If grouping and sorting options are both presented to the constructor,
  41. //the sorters array consists of the grouping sorter object followed by the sorting sorter object
  42. //see Ext.data.Store's sorting functions for details about how multi sorting works
  43. this.hasMultiSort = true;
  44. this.multiSortInfo = this.multiSortInfo || {sorters: []};
  45. var sorters = this.multiSortInfo.sorters,
  46. groupField = config.groupField || this.groupField,
  47. sortInfo = config.sortInfo || this.sortInfo,
  48. groupDir = config.groupDir || this.groupDir;
  49. //add the grouping sorter object first
  50. if(groupField){
  51. sorters.push({
  52. field : groupField,
  53. direction: groupDir
  54. });
  55. }
  56. //add the sorting sorter object if it is present
  57. if (sortInfo) {
  58. sorters.push(sortInfo);
  59. }
  60. Ext.data.GroupingStore.superclass.constructor.call(this, config);
  61. this.addEvents(
  62. <span id='Ext-data-GroupingStore-event-groupchange'> /**
  63. </span> * @event groupchange
  64. * Fired whenever a call to store.groupBy successfully changes the grouping on the store
  65. * @param {Ext.data.GroupingStore} store The grouping store
  66. * @param {String} groupField The field that the store is now grouped by
  67. */
  68. 'groupchange'
  69. );
  70. this.applyGroupField();
  71. },
  72. <span id='Ext-data-GroupingStore-cfg-groupField'> /**
  73. </span> * @cfg {String} groupField
  74. * The field name by which to sort the store's data (defaults to '').
  75. */
  76. <span id='Ext-data-GroupingStore-cfg-remoteGroup'> /**
  77. </span> * @cfg {Boolean} remoteGroup
  78. * True if the grouping should apply on the server side, false if it is local only (defaults to false). If the
  79. * grouping is local, it can be applied immediately to the data. If it is remote, then it will simply act as a
  80. * helper, automatically sending the grouping field name as the 'groupBy' param with each XHR call.
  81. */
  82. remoteGroup : false,
  83. <span id='Ext-data-GroupingStore-cfg-groupOnSort'> /**
  84. </span> * @cfg {Boolean} groupOnSort
  85. * True to sort the data on the grouping field when a grouping operation occurs, false to sort based on the
  86. * existing sort info (defaults to false).
  87. */
  88. groupOnSort:false,
  89. <span id='Ext-data-GroupingStore-cfg-groupDir'> /**
  90. </span> * @cfg {String} groupDir
  91. * The direction to sort the groups. Defaults to &lt;tt&gt;'ASC'&lt;/tt&gt;.
  92. */
  93. groupDir : 'ASC',
  94. <span id='Ext-data-GroupingStore-method-clearGrouping'> /**
  95. </span> * Clears any existing grouping and refreshes the data using the default sort.
  96. */
  97. clearGrouping : function(){
  98. this.groupField = false;
  99. if(this.remoteGroup){
  100. if(this.baseParams){
  101. delete this.baseParams.groupBy;
  102. delete this.baseParams.groupDir;
  103. }
  104. var lo = this.lastOptions;
  105. if(lo &amp;&amp; lo.params){
  106. delete lo.params.groupBy;
  107. delete lo.params.groupDir;
  108. }
  109. this.reload();
  110. }else{
  111. this.sort();
  112. this.fireEvent('datachanged', this);
  113. }
  114. },
  115. <span id='Ext-data-GroupingStore-method-groupBy'> /**
  116. </span> * Groups the data by the specified field.
  117. * @param {String} field The field name by which to sort the store's data
  118. * @param {Boolean} forceRegroup (optional) True to force the group to be refreshed even if the field passed
  119. * in is the same as the current grouping field, false to skip grouping on the same field (defaults to false)
  120. */
  121. groupBy : function(field, forceRegroup, direction) {
  122. direction = direction ? (String(direction).toUpperCase() == 'DESC' ? 'DESC' : 'ASC') : this.groupDir;
  123. if (this.groupField == field &amp;&amp; this.groupDir == direction &amp;&amp; !forceRegroup) {
  124. return; // already grouped by this field
  125. }
  126. //check the contents of the first sorter. If the field matches the CURRENT groupField (before it is set to the new one),
  127. //remove the sorter as it is actually the grouper. The new grouper is added back in by this.sort
  128. var sorters = this.multiSortInfo.sorters;
  129. if (sorters.length &gt; 0 &amp;&amp; sorters[0].field == this.groupField) {
  130. sorters.shift();
  131. }
  132. this.groupField = field;
  133. this.groupDir = direction;
  134. this.applyGroupField();
  135. var fireGroupEvent = function() {
  136. this.fireEvent('groupchange', this, this.getGroupState());
  137. };
  138. if (this.groupOnSort) {
  139. this.sort(field, direction);
  140. fireGroupEvent.call(this);
  141. return;
  142. }
  143. if (this.remoteGroup) {
  144. this.on('load', fireGroupEvent, this, {single: true});
  145. this.reload();
  146. } else {
  147. this.sort(sorters);
  148. fireGroupEvent.call(this);
  149. }
  150. },
  151. <span id='Ext-data-GroupingStore-method-sort'> //GroupingStore always uses multisorting so we intercept calls to sort here to make sure that our grouping sorter object
  152. </span> //is always injected first.
  153. sort : function(fieldName, dir) {
  154. if (this.remoteSort) {
  155. return Ext.data.GroupingStore.superclass.sort.call(this, fieldName, dir);
  156. }
  157. var sorters = [];
  158. //cater for any existing valid arguments to this.sort, massage them into an array of sorter objects
  159. if (Ext.isArray(arguments[0])) {
  160. sorters = arguments[0];
  161. } else if (fieldName == undefined) {
  162. //we preserve the existing sortInfo here because this.sort is called after
  163. //clearGrouping and there may be existing sorting
  164. sorters = this.sortInfo ? [this.sortInfo] : [];
  165. } else {
  166. //TODO: this is lifted straight from Ext.data.Store's singleSort function. It should instead be
  167. //refactored into a common method if possible
  168. var field = this.fields.get(fieldName);
  169. if (!field) return false;
  170. var name = field.name,
  171. sortInfo = this.sortInfo || null,
  172. sortToggle = this.sortToggle ? this.sortToggle[name] : null;
  173. if (!dir) {
  174. if (sortInfo &amp;&amp; sortInfo.field == name) { // toggle sort dir
  175. dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC');
  176. } else {
  177. dir = field.sortDir;
  178. }
  179. }
  180. this.sortToggle[name] = dir;
  181. this.sortInfo = {field: name, direction: dir};
  182. sorters = [this.sortInfo];
  183. }
  184. //add the grouping sorter object as the first multisort sorter
  185. if (this.groupField) {
  186. sorters.unshift({direction: this.groupDir, field: this.groupField});
  187. }
  188. return this.multiSort.call(this, sorters, dir);
  189. },
  190. <span id='Ext-data-GroupingStore-method-applyGroupField'> /**
  191. </span> * @private
  192. * Saves the current grouping field and direction to this.baseParams and this.lastOptions.params
  193. * if we're using remote grouping. Does not actually perform any grouping - just stores values
  194. */
  195. applyGroupField: function(){
  196. if (this.remoteGroup) {
  197. if(!this.baseParams){
  198. this.baseParams = {};
  199. }
  200. Ext.apply(this.baseParams, {
  201. groupBy : this.groupField,
  202. groupDir: this.groupDir
  203. });
  204. var lo = this.lastOptions;
  205. if (lo &amp;&amp; lo.params) {
  206. lo.params.groupDir = this.groupDir;
  207. //this is deleted because of a bug reported at http://www.extjs.com/forum/showthread.php?t=82907
  208. delete lo.params.groupBy;
  209. }
  210. }
  211. },
  212. <span id='Ext-data-GroupingStore-method-applyGrouping'> /**
  213. </span> * @private
  214. * TODO: This function is apparently never invoked anywhere in the framework. It has no documentation
  215. * and should be considered for deletion
  216. */
  217. applyGrouping : function(alwaysFireChange){
  218. if(this.groupField !== false){
  219. this.groupBy(this.groupField, true, this.groupDir);
  220. return true;
  221. }else{
  222. if(alwaysFireChange === true){
  223. this.fireEvent('datachanged', this);
  224. }
  225. return false;
  226. }
  227. },
  228. <span id='Ext-data-GroupingStore-method-getGroupState'> /**
  229. </span> * @private
  230. * Returns the grouping field that should be used. If groupOnSort is used this will be sortInfo's field,
  231. * otherwise it will be this.groupField
  232. * @return {String} The group field
  233. */
  234. getGroupState : function(){
  235. return this.groupOnSort &amp;&amp; this.groupField !== false ?
  236. (this.sortInfo ? this.sortInfo.field : undefined) : this.groupField;
  237. }
  238. });
  239. Ext.reg('groupingstore', Ext.data.GroupingStore);
  240. </pre>
  241. </body>
  242. </html>