index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: ['mergeTable'],
  69. props: {
  70. border: {
  71. type: Boolean,
  72. default: false
  73. },
  74. rows: {
  75. type: Array,
  76. default: () => []
  77. },
  78. propDefines: {
  79. type: Object,
  80. default: () => ({
  81. id: {
  82. label: 'id', // label
  83. slot: '', // define slot if need custom body
  84. slotHeader: '', // define slot if need custom header
  85. slotFooter: '' // define slot if need custm footer
  86. // defines any property that el-table-column support.
  87. }
  88. })
  89. }
  90. },
  91. data() {
  92. return {
  93. properties: {}
  94. }
  95. },
  96. methods: {
  97. getRuntimeTable() {
  98. return this.$refs.table
  99. },
  100. mergeRowsColumns(scope) {
  101. if (typeof this['mergeTable'] === 'function') {
  102. return this['mergeTable'](scope)
  103. }
  104. },
  105. scrollToRight() {
  106. setTimeout(() => {
  107. console.log('will set table ', this.$refs.table.bodyWrapper.scrollLeft, this.$refs.table.bodyWidth)
  108. this.$refs.table.bodyWrapper.scrollLeft = Number(this.$refs.table.bodyWidth.replace('px', ''))
  109. }, 500)
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. </style>