RowExpander.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.grid.RowExpander = function(config) {
  7. Ext.apply(this, config);
  8. this.addEvents({
  9. beforeexpand : true,
  10. expand : true,
  11. beforecollapse : true,
  12. collapse : true
  13. });
  14. Ext.grid.RowExpander.superclass.constructor.call(this);
  15. if (this.tpl) {
  16. if (typeof this.tpl == 'string') {
  17. this.tpl = new Ext.Template(this.tpl);
  18. }
  19. this.tpl.compile();
  20. }
  21. this.state = {};
  22. this.bodyContent = {};
  23. };
  24. Ext.extend(Ext.grid.RowExpander, Ext.util.Observable, {
  25. header : "",
  26. width : 20,
  27. sortable : false,
  28. fixed : true,
  29. dataIndex : '',
  30. id : 'expander',
  31. lazyRender : true,
  32. enableCaching : true,
  33. getRowClass : function(record, rowIndex, p, ds) {
  34. p.cols = p.cols - 1;
  35. var content = this.bodyContent[record.id];
  36. if (!content && !this.lazyRender) {
  37. content = this.getBodyContent(record, rowIndex);
  38. }
  39. if (content) {
  40. p.body = content;
  41. }
  42. return this.state[record.id]
  43. ? 'x-grid3-row-expanded'
  44. : 'x-grid3-row-collapsed';
  45. },
  46. init : function(grid) {
  47. this.grid = grid;
  48. var view = grid.getView();
  49. view.getRowClass = this.getRowClass.createDelegate(this);
  50. view.enableRowBody = true;
  51. grid.on('render', function() {
  52. view.mainBody.on('mousedown', this.onMouseDown, this);
  53. }, this);
  54. },
  55. getBodyContent : function(record, index) {
  56. if (!this.enableCaching) {
  57. return this.tpl.apply(record.data);
  58. }
  59. var content = this.bodyContent[record.id];
  60. if (!content) {
  61. content = this.tpl.apply(record.data);
  62. this.bodyContent[record.id] = content;
  63. }
  64. return content;
  65. },
  66. onMouseDown : function(e, t) {
  67. if (t.className == 'x-grid3-row-expander') {
  68. e.stopEvent();
  69. var row = e.getTarget('.x-grid3-row');
  70. this.toggleRow(row);
  71. }
  72. },
  73. renderer : function(v, p, record) {
  74. p.cellAttr = 'rowspan="2"';
  75. return '<div class="x-grid3-row-expander">&#160;</div>';
  76. },
  77. beforeExpand : function(record, body, rowIndex) {
  78. if (this.fireEvent('beforeexpand', this, record, body, rowIndex) !== false) {
  79. if (this.tpl && this.lazyRender) {
  80. body.innerHTML = this.getBodyContent(record, rowIndex);
  81. }
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. },
  87. toggleRow : function(row) {
  88. if (typeof row == 'number') {
  89. row = this.grid.view.getRow(row);
  90. }
  91. this[Ext.fly(row).hasClass('x-grid3-row-collapsed')
  92. ? 'expandRow'
  93. : 'collapseRow'](row);
  94. },
  95. expandRow : function(row) {
  96. if (typeof row == 'number') {
  97. row = this.grid.view.getRow(row);
  98. }
  99. var record = this.grid.store.getAt(row.rowIndex);
  100. var body = Ext.DomQuery.selectNode('tr:nth(2) div.x-grid3-row-body',
  101. row);
  102. if (this.beforeExpand(record, body, row.rowIndex)) {
  103. this.state[record.id] = true;
  104. Ext.fly(row).replaceClass('x-grid3-row-collapsed',
  105. 'x-grid3-row-expanded');
  106. this.fireEvent('expand', this, record, body, row.rowIndex);
  107. }
  108. },
  109. collapseRow : function(row) {
  110. if (typeof row == 'number') {
  111. row = this.grid.view.getRow(row);
  112. }
  113. var record = this.grid.store.getAt(row.rowIndex);
  114. var body = Ext.fly(row).child('tr:nth(1) div.x-grid3-row-body', true);
  115. if (this.fireEvent('beforcollapse', this, record, body, row.rowIndex) !== false) {
  116. this.state[record.id] = false;
  117. Ext.fly(row).replaceClass('x-grid3-row-expanded',
  118. 'x-grid3-row-collapsed');
  119. this.fireEvent('collapse', this, record, body, row.rowIndex);
  120. }
  121. }
  122. });