index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <el-table ref="table" :border="border" :data="rows" :span-method="mergeRowsColumns"
  3. @selection-change="$emit('selection-change', $event)" tooltip-effect="dark">
  4. <template v-for="(prop, key) in propDefines">
  5. <template v-if="prop.hidden"></template>
  6. <el-table-column v-else-if="!prop.type" :key="key" :label="prop.label" :prop="key" :width="prop.width"
  7. :min-width="prop.minWidth" :align="prop.align || 'center'" :fixed="prop.fixed">
  8. <template v-if="prop.slotHeader" #header="scopeHeader">
  9. <slot :name="prop.slotHeader" v-bind="{
  10. ...scopeHeader,
  11. key: key,
  12. label: prop.label,
  13. prop: prop
  14. }">{{ prop.label }}
  15. </slot>
  16. </template>
  17. <template slot-scope="scope">
  18. <slot v-if="prop.slot" :name="prop.slot" v-bind="{
  19. ...scope,
  20. key: key,
  21. label: prop.label,
  22. value: scope.row[key],
  23. prop: prop
  24. }">
  25. <span>{{ scope.row[key] }}</span>
  26. </slot>
  27. <span v-else>{{ scope.row[key] }}</span>
  28. </template>
  29. <!-- TODO: hht 22.4.11 未实现跨组件传递slot, 先支持固定级别 -->
  30. <template v-if="prop.children">
  31. <el-table-column v-for="(childProp, childKey) in prop.children" :key="childKey" :label="childProp.label"
  32. :prop="childKey"
  33. :width="childProp.width" :fixed="childProp.fixed"
  34. :min-width="childProp.minWidth" :align="childProp.align || 'center'">
  35. <template v-if="childProp.slotHeader" #header="scopeHeader">
  36. <slot :name="childProp.slotHeader" v-bind="{
  37. ...scopeHeader,
  38. key: childKey,
  39. label: childProp.label,
  40. prop: childProp
  41. }">{{ childProp.label }}
  42. </slot>
  43. </template>
  44. <template slot-scope="scope">
  45. <slot v-if="childProp.slot" :name="childProp.slot" v-bind="{
  46. ...scope,
  47. key: childKey,
  48. label: childProp.label,
  49. value: scope.row[childKey],
  50. prop: childProp
  51. }">
  52. <span>{{ scope.row[childKey] }}</span>
  53. </slot>
  54. <span v-else>{{ scope.row[childKey] }}</span>
  55. </template>
  56. </el-table-column>
  57. </template>
  58. </el-table-column>
  59. <el-table-column :key="key" v-else :type="prop.type" :label="prop.label" :width="prop.width"
  60. :align="prop.align || 'center'"></el-table-column>
  61. </template>
  62. </el-table>
  63. </template>
  64. <script>
  65. import MxTableColumn from '@/components/MxTable/mx-table-column'
  66. export default {
  67. components: { MxTableColumn },
  68. inject: {
  69. mergeTable: {
  70. default: () => {
  71. // do nothing.
  72. }
  73. }
  74. },
  75. props: {
  76. keepScroll: {
  77. type: Boolean,
  78. default: false
  79. },
  80. border: {
  81. type: Boolean,
  82. default: false
  83. },
  84. rows: {
  85. type: Array,
  86. default: () => []
  87. },
  88. propDefines: {
  89. type: Object,
  90. default: () => ({
  91. id: {
  92. label: '', // label
  93. slot: '', // define slot if need custom body
  94. slotHeader: '', // define slot if need custom header
  95. slotFooter: '' // define slot if need custm footer
  96. // defines any property that el-table-column support.
  97. }
  98. })
  99. }
  100. },
  101. data() {
  102. return {
  103. cacheScrollLeft: 0,
  104. cacheScrollTop: 0
  105. }
  106. },
  107. activated() {
  108. if (this.keepScroll && (this.cacheScrollLeft || this.cacheScrollTop)) {
  109. this.$nextTick(()=>{
  110. this.$refs.table.bodyWrapper.scrollLeft = this.cacheScrollLeft
  111. this.$refs.table.bodyWrapper.scrollTop = this.cacheScrollTop
  112. })
  113. }
  114. },
  115. deactivated() {
  116. if (this.keepScroll) {
  117. this.cacheScrollLeft = this.$refs.table.bodyWrapper.scrollLeft
  118. this.cacheScrollTop = this.$refs.table.bodyWrapper.scrollTop
  119. }
  120. },
  121. methods: {
  122. getRuntimeTable() {
  123. return this.$refs.table
  124. },
  125. mergeRowsColumns(scope) {
  126. if (typeof this['mergeTable'] === 'function') {
  127. return this['mergeTable'](scope)
  128. }
  129. },
  130. scrollToRight() {
  131. // use `setTimeout` instead of `$nextTick`
  132. // - because css style not ready in next-tick, flow maybe work well in `nextTick.nextTick`
  133. setTimeout(() => {
  134. this.$refs.table.bodyWrapper.scrollLeft = Number(this.$refs.table.bodyWidth.replace('px', ''))
  135. }, 500)
  136. }
  137. }
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. </style>