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.

413 lines
13 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.ns('Ext.ux.grid');
  16. /**
  17. * @class Ext.ux.grid.GroupSummary
  18. * @extends Ext.util.Observable
  19. * A GridPanel plugin that enables dynamic column calculations and a dynamically
  20. * updated grouped summary row.
  21. */
  22. Ext.ux.grid.GroupSummary = Ext.extend(Ext.util.Observable, {
  23. /**
  24. * @cfg {Function} summaryRenderer Renderer example:<pre><code>
  25. summaryRenderer: function(v, params, data){
  26. return ((v === 0 || v > 1) ? '(' + v +' Tasks)' : '(1 Task)');
  27. },
  28. * </code></pre>
  29. */
  30. /**
  31. * @cfg {String} summaryType (Optional) The type of
  32. * calculation to be used for the column. For options available see
  33. * {@link #Calculations}.
  34. */
  35. constructor : function(config){
  36. Ext.apply(this, config);
  37. Ext.ux.grid.GroupSummary.superclass.constructor.call(this);
  38. },
  39. init : function(grid){
  40. this.grid = grid;
  41. var v = this.view = grid.getView();
  42. v.doGroupEnd = this.doGroupEnd.createDelegate(this);
  43. v.afterMethod('onColumnWidthUpdated', this.doWidth, this);
  44. v.afterMethod('onAllColumnWidthsUpdated', this.doAllWidths, this);
  45. v.afterMethod('onColumnHiddenUpdated', this.doHidden, this);
  46. v.afterMethod('onUpdate', this.doUpdate, this);
  47. v.afterMethod('onRemove', this.doRemove, this);
  48. if(!this.rowTpl){
  49. this.rowTpl = new Ext.Template(
  50. '<div class="x-grid3-summary-row" style="{tstyle}">',
  51. '<table class="x-grid3-summary-table" border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
  52. '<tbody><tr>{cells}</tr></tbody>',
  53. '</table></div>'
  54. );
  55. this.rowTpl.disableFormats = true;
  56. }
  57. this.rowTpl.compile();
  58. if(!this.cellTpl){
  59. this.cellTpl = new Ext.Template(
  60. '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}">',
  61. '<div class="x-grid3-cell-inner x-grid3-col-{id}" unselectable="on">{value}</div>',
  62. "</td>"
  63. );
  64. this.cellTpl.disableFormats = true;
  65. }
  66. this.cellTpl.compile();
  67. },
  68. /**
  69. * Toggle the display of the summary row on/off
  70. * @param {Boolean} visible <tt>true</tt> to show the summary, <tt>false</tt> to hide the summary.
  71. */
  72. toggleSummaries : function(visible){
  73. var el = this.grid.getGridEl();
  74. if(el){
  75. if(visible === undefined){
  76. visible = el.hasClass('x-grid-hide-summary');
  77. }
  78. el[visible ? 'removeClass' : 'addClass']('x-grid-hide-summary');
  79. }
  80. },
  81. renderSummary : function(o, cs){
  82. cs = cs || this.view.getColumnData();
  83. var cfg = this.grid.getColumnModel().config,
  84. buf = [], c, p = {}, cf, last = cs.length-1;
  85. for(var i = 0, len = cs.length; i < len; i++){
  86. c = cs[i];
  87. cf = cfg[i];
  88. p.id = c.id;
  89. p.style = c.style;
  90. p.css = i == 0 ? 'x-grid3-cell-first ' : (i == last ? 'x-grid3-cell-last ' : '');
  91. if(cf.summaryType || cf.summaryRenderer){
  92. p.value = (cf.summaryRenderer || c.renderer)(o.data[c.name], p, o);
  93. }else{
  94. p.value = '';
  95. }
  96. if(p.value == undefined || p.value === "") p.value = "&#160;";
  97. buf[buf.length] = this.cellTpl.apply(p);
  98. }
  99. return this.rowTpl.apply({
  100. tstyle: 'width:'+this.view.getTotalWidth()+';',
  101. cells: buf.join('')
  102. });
  103. },
  104. /**
  105. * @private
  106. * @param {Object} rs
  107. * @param {Object} cs
  108. */
  109. calculate : function(rs, cs){
  110. var data = {}, r, c, cfg = this.grid.getColumnModel().config, cf;
  111. for(var j = 0, jlen = rs.length; j < jlen; j++){
  112. r = rs[j];
  113. for(var i = 0, len = cs.length; i < len; i++){
  114. c = cs[i];
  115. cf = cfg[i];
  116. if(cf.summaryType){
  117. data[c.name] = Ext.ux.grid.GroupSummary.Calculations[cf.summaryType](data[c.name] || 0, r, c.name, data);
  118. }
  119. }
  120. }
  121. return data;
  122. },
  123. doGroupEnd : function(buf, g, cs, ds, colCount){
  124. var data = this.calculate(g.rs, cs);
  125. buf.push('</div>', this.renderSummary({data: data}, cs), '</div>');
  126. },
  127. doWidth : function(col, w, tw){
  128. if(!this.isGrouped()){
  129. return;
  130. }
  131. var gs = this.view.getGroups(),
  132. len = gs.length,
  133. i = 0,
  134. s;
  135. for(; i < len; ++i){
  136. s = gs[i].childNodes[2];
  137. s.style.width = tw;
  138. s.firstChild.style.width = tw;
  139. s.firstChild.rows[0].childNodes[col].style.width = w;
  140. }
  141. },
  142. doAllWidths : function(ws, tw){
  143. if(!this.isGrouped()){
  144. return;
  145. }
  146. var gs = this.view.getGroups(),
  147. len = gs.length,
  148. i = 0,
  149. j,
  150. s,
  151. cells,
  152. wlen = ws.length;
  153. for(; i < len; i++){
  154. s = gs[i].childNodes[2];
  155. s.style.width = tw;
  156. s.firstChild.style.width = tw;
  157. cells = s.firstChild.rows[0].childNodes;
  158. for(j = 0; j < wlen; j++){
  159. cells[j].style.width = ws[j];
  160. }
  161. }
  162. },
  163. doHidden : function(col, hidden, tw){
  164. if(!this.isGrouped()){
  165. return;
  166. }
  167. var gs = this.view.getGroups(),
  168. len = gs.length,
  169. i = 0,
  170. s,
  171. display = hidden ? 'none' : '';
  172. for(; i < len; i++){
  173. s = gs[i].childNodes[2];
  174. s.style.width = tw;
  175. s.firstChild.style.width = tw;
  176. s.firstChild.rows[0].childNodes[col].style.display = display;
  177. }
  178. },
  179. isGrouped : function(){
  180. return !Ext.isEmpty(this.grid.getStore().groupField);
  181. },
  182. // Note: requires that all (or the first) record in the
  183. // group share the same group value. Returns false if the group
  184. // could not be found.
  185. refreshSummary : function(groupValue){
  186. return this.refreshSummaryById(this.view.getGroupId(groupValue));
  187. },
  188. getSummaryNode : function(gid){
  189. var g = Ext.fly(gid, '_gsummary');
  190. if(g){
  191. return g.down('.x-grid3-summary-row', true);
  192. }
  193. return null;
  194. },
  195. refreshSummaryById : function(gid){
  196. var g = Ext.getDom(gid);
  197. if(!g){
  198. return false;
  199. }
  200. var rs = [];
  201. this.grid.getStore().each(function(r){
  202. if(r._groupId == gid){
  203. rs[rs.length] = r;
  204. }
  205. });
  206. var cs = this.view.getColumnData(),
  207. data = this.calculate(rs, cs),
  208. markup = this.renderSummary({data: data}, cs),
  209. existing = this.getSummaryNode(gid);
  210. if(existing){
  211. g.removeChild(existing);
  212. }
  213. Ext.DomHelper.append(g, markup);
  214. return true;
  215. },
  216. doUpdate : function(ds, record){
  217. this.refreshSummaryById(record._groupId);
  218. },
  219. doRemove : function(ds, record, index, isUpdate){
  220. if(!isUpdate){
  221. this.refreshSummaryById(record._groupId);
  222. }
  223. },
  224. /**
  225. * Show a message in the summary row.
  226. * <pre><code>
  227. grid.on('afteredit', function(){
  228. var groupValue = 'Ext Forms: Field Anchoring';
  229. summary.showSummaryMsg(groupValue, 'Updating Summary...');
  230. });
  231. * </code></pre>
  232. * @param {String} groupValue
  233. * @param {String} msg Text to use as innerHTML for the summary row.
  234. */
  235. showSummaryMsg : function(groupValue, msg){
  236. var gid = this.view.getGroupId(groupValue),
  237. node = this.getSummaryNode(gid);
  238. if(node){
  239. node.innerHTML = '<div class="x-grid3-summary-msg">' + msg + '</div>';
  240. }
  241. }
  242. });
  243. //backwards compat
  244. Ext.grid.GroupSummary = Ext.ux.grid.GroupSummary;
  245. /**
  246. * Calculation types for summary row:</p><div class="mdetail-params"><ul>
  247. * <li><b><tt>sum</tt></b> : <div class="sub-desc"></div></li>
  248. * <li><b><tt>count</tt></b> : <div class="sub-desc"></div></li>
  249. * <li><b><tt>max</tt></b> : <div class="sub-desc"></div></li>
  250. * <li><b><tt>min</tt></b> : <div class="sub-desc"></div></li>
  251. * <li><b><tt>average</tt></b> : <div class="sub-desc"></div></li>
  252. * </ul></div>
  253. * <p>Custom calculations may be implemented. An example of
  254. * custom <code>summaryType=totalCost</code>:</p><pre><code>
  255. // define a custom summary function
  256. Ext.ux.grid.GroupSummary.Calculations['totalCost'] = function(v, record, field){
  257. return v + (record.data.estimate * record.data.rate);
  258. };
  259. * </code></pre>
  260. * @property Calculations
  261. */
  262. Ext.ux.grid.GroupSummary.Calculations = {
  263. 'sum' : function(v, record, field){
  264. return v + (record.data[field]||0);
  265. },
  266. 'count' : function(v, record, field, data){
  267. return data[field+'count'] ? ++data[field+'count'] : (data[field+'count'] = 1);
  268. },
  269. 'max' : function(v, record, field, data){
  270. var v = record.data[field];
  271. var max = data[field+'max'] === undefined ? (data[field+'max'] = v) : data[field+'max'];
  272. return v > max ? (data[field+'max'] = v) : max;
  273. },
  274. 'min' : function(v, record, field, data){
  275. var v = record.data[field];
  276. var min = data[field+'min'] === undefined ? (data[field+'min'] = v) : data[field+'min'];
  277. return v < min ? (data[field+'min'] = v) : min;
  278. },
  279. 'average' : function(v, record, field, data){
  280. var c = data[field+'count'] ? ++data[field+'count'] : (data[field+'count'] = 1);
  281. var t = (data[field+'total'] = ((data[field+'total']||0) + (record.data[field]||0)));
  282. return t === 0 ? 0 : t / c;
  283. }
  284. };
  285. Ext.grid.GroupSummary.Calculations = Ext.ux.grid.GroupSummary.Calculations;
  286. /**
  287. * @class Ext.ux.grid.HybridSummary
  288. * @extends Ext.ux.grid.GroupSummary
  289. * Adds capability to specify the summary data for the group via json as illustrated here:
  290. * <pre><code>
  291. {
  292. data: [
  293. {
  294. projectId: 100, project: 'House',
  295. taskId: 112, description: 'Paint',
  296. estimate: 6, rate: 150,
  297. due:'06/24/2007'
  298. },
  299. ...
  300. ],
  301. summaryData: {
  302. 'House': {
  303. description: 14, estimate: 9,
  304. rate: 99, due: new Date(2009, 6, 29),
  305. cost: 999
  306. }
  307. }
  308. }
  309. * </code></pre>
  310. *
  311. */
  312. Ext.ux.grid.HybridSummary = Ext.extend(Ext.ux.grid.GroupSummary, {
  313. /**
  314. * @private
  315. * @param {Object} rs
  316. * @param {Object} cs
  317. */
  318. calculate : function(rs, cs){
  319. var gcol = this.view.getGroupField(),
  320. gvalue = rs[0].data[gcol],
  321. gdata = this.getSummaryData(gvalue);
  322. return gdata || Ext.ux.grid.HybridSummary.superclass.calculate.call(this, rs, cs);
  323. },
  324. /**
  325. * <pre><code>
  326. grid.on('afteredit', function(){
  327. var groupValue = 'Ext Forms: Field Anchoring';
  328. summary.showSummaryMsg(groupValue, 'Updating Summary...');
  329. setTimeout(function(){ // simulate server call
  330. // HybridSummary class implements updateSummaryData
  331. summary.updateSummaryData(groupValue,
  332. // create data object based on configured dataIndex
  333. {description: 22, estimate: 888, rate: 888, due: new Date(), cost: 8});
  334. }, 2000);
  335. });
  336. * </code></pre>
  337. * @param {String} groupValue
  338. * @param {Object} data data object
  339. * @param {Boolean} skipRefresh (Optional) Defaults to false
  340. */
  341. updateSummaryData : function(groupValue, data, skipRefresh){
  342. var json = this.grid.getStore().reader.jsonData;
  343. if(!json.summaryData){
  344. json.summaryData = {};
  345. }
  346. json.summaryData[groupValue] = data;
  347. if(!skipRefresh){
  348. this.refreshSummary(groupValue);
  349. }
  350. },
  351. /**
  352. * Returns the summaryData for the specified groupValue or null.
  353. * @param {String} groupValue
  354. * @return {Object} summaryData
  355. */
  356. getSummaryData : function(groupValue){
  357. var reader = this.grid.getStore().reader,
  358. json = reader.jsonData,
  359. fields = reader.recordType.prototype.fields,
  360. v;
  361. if(json && json.summaryData){
  362. v = json.summaryData[groupValue];
  363. if(v){
  364. return reader.extractValues(v, fields.items, fields.length);
  365. }
  366. }
  367. return null;
  368. }
  369. });
  370. //backwards compat
  371. Ext.grid.HybridSummary = Ext.ux.grid.HybridSummary;