index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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" v-bind="$attrs" v-loading="loading" element-loading-background="rgba(0, 0, 0, .5)"
  16. height="100%" highlight-current-row scrollbar-always-on border>
  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. <template v-for="(item, index) in columns" :key="index">
  25. <el-table-column :class-name="item.className" :align="item.align ?? 'left'" :min-width="120"
  26. :width="item.width" :label="item.label" :prop="item.prop" :fixed="item.fixed || false"
  27. :sortable="item.sortable" v-if="item.show">
  28. <template #default="{ row, $index }">
  29. <slot :name="item.prop" :row="row" :value="row[item.prop]" :index="$index">
  30. {{ handleNoneValue(row[item.prop]) }}
  31. </slot>
  32. </template>
  33. </el-table-column>
  34. </template>
  35. </el-table>
  36. </div>
  37. <div class="app-table__footer">
  38. <slot name="footer"></slot>
  39. </div>
  40. <app-table-setting :columns="columns" @update="updateColumn" @closed="showTableSetting = false"
  41. v-if="showTableSetting" />
  42. </div>
  43. </template>
  44. <script lang="ts">
  45. import { defineComponent, shallowRef, PropType } from 'vue'
  46. import { handleNoneValue } from '@/filters'
  47. import AppTableSetting from '../table-setting/index.vue'
  48. export default defineComponent({
  49. inheritAttrs: false,
  50. components: {
  51. AppTableSetting
  52. },
  53. emits: ['refresh', 'update:columns'],
  54. props: {
  55. columns: {
  56. type: Array as PropType<Ermcp.TableColumn[]>,
  57. default: () => ([])
  58. },
  59. showHeader: {
  60. type: Boolean,
  61. default: true
  62. },
  63. loading: {
  64. type: Boolean,
  65. default: false
  66. },
  67. },
  68. setup(props, { emit, expose }) {
  69. const tableRef = shallowRef()
  70. const showTableSetting = shallowRef(false)
  71. const refresh = () => emit('refresh')
  72. const updateColumn = (value: Ermcp.TableColumn[]) => emit('update:columns', value)
  73. expose({
  74. elTable: tableRef
  75. })
  76. return {
  77. tableRef,
  78. showTableSetting,
  79. handleNoneValue,
  80. refresh,
  81. updateColumn,
  82. }
  83. },
  84. })
  85. </script>
  86. <style lang="less">
  87. @import './index.less';
  88. </style>