table-body.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const getColumnById = function(grid, columnId) {
  2. let column = null;
  3. grid.columns.forEach(function(item) {
  4. if (item.id === columnId) {
  5. column = item;
  6. }
  7. });
  8. return column;
  9. };
  10. const getColumnByCell = function(grid, cell) {
  11. const matches = (cell.className || '').match(/grid_[^\s]+/gm);
  12. if (matches) {
  13. return getColumnById(grid, matches[0]);
  14. }
  15. return null;
  16. };
  17. import { getValueByPath, getCell, orderBy, getChild } from './util';
  18. export default {
  19. props: {
  20. columns: {},
  21. data: {},
  22. fixed: {},
  23. selection: {
  24. default() {
  25. return [];
  26. }
  27. }
  28. },
  29. render(h) {
  30. return (
  31. <table
  32. class="el-table__body"
  33. cellspacing="0"
  34. cellpadding="0"
  35. border="0">
  36. <tbody>
  37. {
  38. this._l(this.data, (row, $index) =>
  39. <tr
  40. on-click={ ($event) => this.handleClick($event, row) }
  41. on-mouseenter={ _ => this.handleMouseEnter($index) }
  42. style={ this.getCustomStyle(row) }
  43. class={{
  44. 'current-row': row === this.$parent.selected,
  45. 'hover': this.$parent.$parent.hoverRowIndex === $index
  46. }}>
  47. {
  48. this._l(this.columns, (column) =>
  49. <td
  50. style={ this.getColumnWhiteSpaceStyle(column) }
  51. class={ column.id }
  52. on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
  53. on-mouseleave={ this.handleCellMouseLeave }>
  54. {
  55. column.template
  56. ? column.template.call(this._renderProxy, h, { row, column, $index, _self: this.$parent.$vnode.context })
  57. : <div class="cell">{ this.$getPropertyText(row, column.property, column.id) }</div>
  58. }
  59. </td>
  60. ).concat(this.fixed ? <td class="gutter" /> : '')
  61. }
  62. </tr>
  63. )
  64. }
  65. </tbody>
  66. </table>
  67. );
  68. },
  69. data() {
  70. return {
  71. criteria: this.$parent.customCriteria,
  72. colors: this.$parent.customBackgroundColors,
  73. tooltipDisabled: true
  74. };
  75. },
  76. filters: {
  77. orderBy
  78. },
  79. methods: {
  80. getColumnWhiteSpaceStyle(column) {
  81. return column.showTooltipWhenOverflow ? { 'white-space': 'nowrap' } : {};
  82. },
  83. checkProperty(row) {
  84. if (this.criteria && this.criteria.length > 0) {
  85. for (let i = 0, len = this.criteria.length; i < len; i++) {
  86. if (row[this.criteria[i]] === true) {
  87. return i;
  88. }
  89. }
  90. }
  91. return -1;
  92. },
  93. getCustomStyle(row) {
  94. if (!this.criteria || !this.colors || this.criteria.length !== this.colors.length) {
  95. return {};
  96. }
  97. let criterionIndex = this.checkProperty(row);
  98. return criterionIndex > -1 ? { 'background-color': this.colors[criterionIndex] } : {};
  99. },
  100. handleCellMouseEnter(event, row) {
  101. let grid = this.$parent;
  102. const cell = getCell(event);
  103. if (cell) {
  104. const column = getColumnByCell(grid, cell);
  105. const hoverState = grid.hoverState = { cell: cell, column: column, row: row };
  106. grid.$emit('cellmouseenter', hoverState.row, hoverState.column, hoverState.cell, event);
  107. }
  108. // 判断是否text-overflow, 如果是就显示tooltip
  109. const cellChild = getChild(event);
  110. this.tooltipDisabled = cellChild.scrollWidth <= cellChild.offsetWidth;
  111. },
  112. handleCellMouseLeave(event) {
  113. let grid = this.$parent;
  114. const cell = getCell(event);
  115. if (cell) {
  116. const oldHoverState = grid.hoverState;
  117. grid.$emit('cellmouseleave', oldHoverState.row, oldHoverState.column, oldHoverState.cell, event);
  118. }
  119. },
  120. handleMouseEnter(index) {
  121. this.$parent.hoverRowIndex = index;
  122. },
  123. handleClick(event, row) {
  124. let grid = this.$parent;
  125. const cell = getCell(event);
  126. if (cell) {
  127. const column = getColumnByCell(grid, cell);
  128. if (column) {
  129. grid.$emit('cellclick', row, column, cell, event);
  130. }
  131. }
  132. if (grid.selectionMode === 'single') {
  133. grid.selected = row;
  134. }
  135. grid.$emit('rowclick', row, event);
  136. },
  137. handleCreate(vm) {
  138. document.body.appendChild(vm.$refs.popper);
  139. vm.updatePopper();
  140. },
  141. $getPropertyText(row, property, columnId) {
  142. let grid = this.$parent;
  143. const column = getColumnById(grid, columnId);
  144. if (column && column.formatter) {
  145. return column.formatter(row, column);
  146. }
  147. return getValueByPath(row, property);
  148. }
  149. }
  150. };