RowExpander.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 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 published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. // feature idea to enable Ajax loading and then the content
  10. // cache would actually make sense. Should we dictate that they use
  11. // data or support raw html as well?
  12. /**
  13. * @class Ext.ux.RowExpander
  14. * @extends Ext.AbstractPlugin
  15. * Plugin (ptype = 'rowexpander') that adds the ability to have a Column in a grid which enables
  16. * a second row body which expands/contracts. The expand/contract behavior is configurable to react
  17. * on clicking of the column, double click of the row, and/or hitting enter while a row is selected.
  18. *
  19. * @ptype rowexpander
  20. */
  21. Ext.define('Ext.ux.RowExpander', {
  22. extend: 'Ext.AbstractPlugin',
  23. requires: [
  24. 'Ext.grid.feature.RowBody',
  25. 'Ext.grid.feature.RowWrap'
  26. ],
  27. alias: 'plugin.rowexpander',
  28. rowBodyTpl: null,
  29. /**
  30. * @cfg {Boolean} expandOnEnter
  31. * <tt>true</tt> to toggle selected row(s) between expanded/collapsed when the enter
  32. * key is pressed (defaults to <tt>true</tt>).
  33. */
  34. expandOnEnter: true,
  35. /**
  36. * @cfg {Boolean} expandOnDblClick
  37. * <tt>true</tt> to toggle a row between expanded/collapsed when double clicked
  38. * (defaults to <tt>true</tt>).
  39. */
  40. expandOnDblClick: true,
  41. /**
  42. * @cfg {Boolean} selectRowOnExpand
  43. * <tt>true</tt> to select a row when clicking on the expander icon
  44. * (defaults to <tt>false</tt>).
  45. */
  46. selectRowOnExpand: false,
  47. rowBodyTrSelector: '.x-grid-rowbody-tr',
  48. rowBodyHiddenCls: 'x-grid-row-body-hidden',
  49. rowCollapsedCls: 'x-grid-row-collapsed',
  50. renderer: function(value, metadata, record, rowIdx, colIdx) {
  51. if (colIdx === 0) {
  52. metadata.tdCls = 'x-grid-td-expander';
  53. }
  54. return '<div class="x-grid-row-expander">&#160;</div>';
  55. },
  56. /**
  57. * @event expandbody
  58. * <b<Fired through the grid's View</b>
  59. * @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
  60. * @param {Ext.data.Model} record The record providing the data.
  61. * @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
  62. */
  63. /**
  64. * @event collapsebody
  65. * <b<Fired through the grid's View.</b>
  66. * @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
  67. * @param {Ext.data.Model} record The record providing the data.
  68. * @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
  69. */
  70. constructor: function() {
  71. this.callParent(arguments);
  72. var grid = this.getCmp();
  73. this.recordsExpanded = {};
  74. // <debug>
  75. if (!this.rowBodyTpl) {
  76. Ext.Error.raise("The 'rowBodyTpl' config is required and is not defined.");
  77. }
  78. // </debug>
  79. // TODO: if XTemplate/Template receives a template as an arg, should
  80. // just return it back!
  81. var rowBodyTpl = this.rowBodyTpl.isTemplate ? this.rowBodyTpl : new Ext.XTemplate(this.rowBodyTpl),
  82. features = [{
  83. ftype: 'rowbody',
  84. columnId: this.getHeaderId(),
  85. recordsExpanded: this.recordsExpanded,
  86. rowBodyHiddenCls: this.rowBodyHiddenCls,
  87. rowCollapsedCls: this.rowCollapsedCls,
  88. getAdditionalData: this.getRowBodyFeatureData,
  89. getRowBodyContents: function(data) {
  90. return rowBodyTpl.applyTemplate(data);
  91. }
  92. },{
  93. ftype: 'rowwrap'
  94. }];
  95. rowBodyTpl.owner = grid;
  96. if (grid.features) {
  97. grid.features = features.concat(grid.features);
  98. } else {
  99. grid.features = features;
  100. }
  101. // NOTE: features have to be added before init (before Table.initComponent)
  102. },
  103. init: function(grid) {
  104. this.callParent(arguments);
  105. // Columns have to be added in init (after columns has been used to create the
  106. // headerCt). Otherwise, shared column configs get corrupted, e.g., if put in the
  107. // prototype.
  108. if(grid.headerCt) {
  109. grid.headerCt.insert(0, this.getHeaderConfig());
  110. grid.on('render', this.bindView, this, {single: true});
  111. }
  112. },
  113. getHeaderId: function() {
  114. if (!this.headerId) {
  115. this.headerId = Ext.id();
  116. }
  117. return this.headerId;
  118. },
  119. getRowBodyFeatureData: function(data, idx, record, orig) {
  120. var o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments),
  121. id = this.columnId;
  122. o.rowBodyColspan = o.rowBodyColspan - 1;
  123. o.rowBody = this.getRowBodyContents(data);
  124. o.rowCls = this.recordsExpanded[record.internalId] ? '' : this.rowCollapsedCls;
  125. o.rowBodyCls = this.recordsExpanded[record.internalId] ? '' : this.rowBodyHiddenCls;
  126. o[id + '-tdAttr'] = ' valign="top" rowspan="2" ';
  127. if (orig[id+'-tdAttr']) {
  128. o[id+'-tdAttr'] += orig[id+'-tdAttr'];
  129. }
  130. return o;
  131. },
  132. bindView: function() {
  133. var view = this.getCmp().getView(),
  134. viewEl;
  135. if (!view.rendered) {
  136. view.on('render', this.bindView, this, {single: true});
  137. } else {
  138. viewEl = view.getEl();
  139. if (this.expandOnEnter) {
  140. this.keyNav = Ext.create('Ext.KeyNav', viewEl, {
  141. 'enter' : this.onEnter,
  142. scope: this
  143. });
  144. }
  145. if (this.expandOnDblClick) {
  146. view.on('itemdblclick', this.onDblClick, this);
  147. }
  148. this.view = view;
  149. view.on('expandbody', this.onExpand, this);
  150. view.on('collapsebody', this.onCollapse, this);
  151. view.on('togglerow', this.onToggle, this);
  152. }
  153. // start expand
  154. var me = this;
  155. if(me.startExpand) {
  156. me.getCmp().store.each(function(record){
  157. me.recordsExpanded[record.internalId] = true;
  158. });
  159. }
  160. },
  161. //供重写使用
  162. onExpand: Ext.emptyFn,
  163. onCollapse: Ext.emptyFn,
  164. onToggle: Ext.emptyFn,
  165. onEnter: function(e) {
  166. var view = this.view,
  167. ds = view.store,
  168. sm = view.getSelectionModel(),
  169. sels = sm.getSelection(),
  170. ln = sels.length,
  171. i = 0,
  172. rowIdx;
  173. for (; i < ln; i++) {
  174. rowIdx = ds.indexOf(sels[i]);
  175. this.toggleRow(rowIdx);
  176. }
  177. },
  178. toggleRow: function(rowIdx) {
  179. var rowNode = this.view.getNode(rowIdx),
  180. row = Ext.get(rowNode),
  181. nextBd = Ext.get(row).down(this.rowBodyTrSelector),
  182. record = this.view.getRecord(rowNode),
  183. grid = this.getCmp();
  184. if (row.hasCls(this.rowCollapsedCls)) {
  185. row.removeCls(this.rowCollapsedCls);
  186. nextBd.removeCls(this.rowBodyHiddenCls);
  187. this.recordsExpanded[record.internalId] = true;
  188. this.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
  189. } else {
  190. row.addCls(this.rowCollapsedCls);
  191. nextBd.addCls(this.rowBodyHiddenCls);
  192. this.recordsExpanded[record.internalId] = false;
  193. this.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
  194. }
  195. this.view.fireEvent('togglerow', rowNode, record, nextBd.dom);
  196. // If Grid is auto-heighting itself, then perform a component layhout to accommodate the new height
  197. if (!grid.isFixedHeight()) {
  198. grid.doComponentLayout();
  199. }
  200. this.view.up('gridpanel').invalidateScroller();
  201. },
  202. onDblClick: function(view, cell, rowIdx, cellIndex, e) {
  203. this.toggleRow(rowIdx);
  204. },
  205. getHeaderConfig: function() {
  206. var me = this,
  207. toggleRow = Ext.Function.bind(me.toggleRow, me),
  208. selectRowOnExpand = me.selectRowOnExpand;
  209. return {
  210. id: this.getHeaderId(),
  211. width: 24,
  212. sortable: false,
  213. resizable: false,
  214. draggable: false,
  215. hideable: false,
  216. menuDisabled: true,
  217. cls: Ext.baseCSSPrefix + 'grid-header-special',
  218. renderer: me.renderer || function(value, metadata) {
  219. if(metadata)
  220. metadata.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
  221. return '<div class="' + Ext.baseCSSPrefix + 'grid-row-expander">&#160;</div>';
  222. },
  223. processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
  224. if (type == "mousedown" && e.getTarget('.x-grid-row-expander')) {
  225. var row = e.getTarget('.x-grid-row');
  226. toggleRow(row);
  227. return selectRowOnExpand;
  228. }
  229. }
  230. };
  231. }
  232. });