index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="app-table">
  3. <div class="app-table__header" v-if="showHeader">
  4. <div>
  5. <slot name="header"></slot>
  6. </div>
  7. <div>
  8. <el-button-group>
  9. <el-button icon="Refresh" @click="refresh" />
  10. <el-button icon="Setting" @click="showTableSetting = true" />
  11. </el-button-group>
  12. </div>
  13. </div>
  14. <div class="app-table__container">
  15. <el-table ref="tableRef" :header-cell-class-name="selectionType" v-bind="$attrs" v-loading="loading"
  16. highlight-current-row scrollbar-always-on @row-click="onRowClick" @select="onSelect">
  17. <!-- 展开行 -->
  18. <el-table-column type="expand" v-if="$slots.expand">
  19. <template #default="{ row, $index }">
  20. <slot name="expand" :row="row" :index="$index"></slot>
  21. </template>
  22. </el-table-column>
  23. <!-- 选择列 -->
  24. <el-table-column type="selection" width="55" align="center" fixed v-if="selectionType" />
  25. <!-- 数据列 -->
  26. <template v-for="(item, index) in columns" :key="index">
  27. <el-table-column :class-name="item.className" :align="item.align ?? 'center'" :min-width="120"
  28. :width="item.width" :label="item.label" :prop="item.prop" :fixed="item.fixed || false"
  29. :sortable="item.sortable" show-overflow-tooltip v-if="item.show">
  30. <template #default="{ row, $index }">
  31. <slot :name="item.prop" :row="row" :value="row[item.prop]" :index="$index">
  32. {{ handleNoneValue(row[item.prop]) }}
  33. </slot>
  34. </template>
  35. </el-table-column>
  36. </template>
  37. </el-table>
  38. </div>
  39. <div class="app-table__footer">
  40. <slot name="footer"></slot>
  41. </div>
  42. <app-table-setting :columns="columns" @update="updateColumn" @closed="showTableSetting = false"
  43. v-if="showTableSetting" />
  44. </div>
  45. </template>
  46. <script lang="ts">
  47. import { defineComponent, shallowRef, PropType } from 'vue'
  48. import { handleNoneValue } from '@/filters'
  49. import AppTableSetting from '../table-setting/index.vue'
  50. export default defineComponent({
  51. inheritAttrs: false,
  52. components: {
  53. AppTableSetting
  54. },
  55. emits: ['refresh', 'update:columns', 'rowClick', 'select'],
  56. props: {
  57. columns: {
  58. type: Array as PropType<Ermcp.TableColumn[]>,
  59. default: () => ([])
  60. },
  61. showHeader: {
  62. type: Boolean,
  63. default: true
  64. },
  65. loading: {
  66. type: Boolean,
  67. default: false
  68. },
  69. // 选择列类型
  70. selectionType: {
  71. type: String as PropType<'single' | 'multiple'>,
  72. }
  73. },
  74. setup(props, { emit, expose }) {
  75. const tableRef = shallowRef()
  76. const tableRadio = shallowRef()
  77. const showTableSetting = shallowRef(false)
  78. const refresh = () => emit('refresh')
  79. const updateColumn = (value: Ermcp.TableColumn[]) => emit('update:columns', value)
  80. // 当某一行被勾选时触发的事件
  81. const onSelect = (selection: unknown[], currentRow: unknown) => {
  82. const el = tableRef.value
  83. const rows = selection.filter((e) => {
  84. if (props.selectionType === 'single') {
  85. if (e === currentRow) {
  86. el.setCurrentRow(currentRow) // 高亮行
  87. return true
  88. } else {
  89. el.toggleRowSelection(e, false) // 单选取消其他选中的行
  90. return false
  91. }
  92. }
  93. return true
  94. })
  95. emit('select', rows, currentRow)
  96. }
  97. // 当某一行被点击时选中该行
  98. const onRowClick = (row: unknown) => {
  99. // if (props.selectionType) {
  100. // const el = tableRef.value
  101. // const selection = el.getSelectionRows()
  102. // const selected = selection.find((e: unknown) => e === row)
  103. // el.toggleRowSelection(row, selected ? false : true)
  104. // onSelect(el.getSelectionRows(), row)
  105. // }
  106. emit('rowClick', row)
  107. }
  108. // 暴露组件属性
  109. expose({
  110. elTable: tableRef
  111. })
  112. return {
  113. tableRef,
  114. tableRadio,
  115. showTableSetting,
  116. onSelect,
  117. onRowClick,
  118. handleNoneValue,
  119. refresh,
  120. updateColumn,
  121. }
  122. },
  123. })
  124. </script>
  125. <style lang="less">
  126. @import './index.less';
  127. </style>