index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div class="app-table" v-loading="loading">
  3. <div class="app-table__header">
  4. <div class="block block--left">
  5. <slot name="headerLeft"></slot>
  6. </div>
  7. <div class="block block--right">
  8. <slot name="headerRight"></slot>
  9. <el-button-group v-if="showToolbar">
  10. <slot name="toolbar"></slot>
  11. <el-button icon="Refresh" @click="refresh" />
  12. <el-button icon="Setting" @click="showTableSetting = true" />
  13. </el-button-group>
  14. </div>
  15. </div>
  16. <div class="app-table__container">
  17. <el-table ref="tableRef" height="100%" :header-cell-class-name="selectionType" v-bind="$attrs"
  18. :highlight-current-row="highlightCurrentRow" :empty-text="t('common.nodatas')" scrollbar-always-on @row-click="onRowClick"
  19. @expand-change="onRowClick" @select="onSelect" border>
  20. <!-- 展开行 -->
  21. <el-table-column type="expand" v-if="$slots.expand">
  22. <template #default="{ row, $index }">
  23. <slot name="expand" :row="row" :index="$index"></slot>
  24. </template>
  25. </el-table-column>
  26. <!-- 选择列 -->
  27. <el-table-column type="selection" width="55" align="center" fixed v-if="selectionType" />
  28. <!-- 序号 -->
  29. <el-table-column type="index" :label="t('common.orderindex')" align="center" :width="80" v-if="showIndex" />
  30. <!-- 数据列 -->
  31. <template v-for="(item, index) in columns" :key="index">
  32. <el-table-column :align="item.align ?? 'center'" :min-width="120" :width="item.width" :label="t(item.label)"
  33. :prop="item.field" :fixed="item.fixed || false" :sortable="item.sortable"
  34. :show-overflow-tooltip="item.field !== 'operate'" v-if="item.show ?? true">
  35. <template #default="{ row, $index }">
  36. <slot :name="item.field" :row="row" :value="row[item.field]" :index="$index">
  37. <span :class="item.className">{{ handleValue(row, item) }}</span>
  38. </slot>
  39. </template>
  40. </el-table-column>
  41. </template>
  42. <template #append v-if="$slots.append">
  43. <slot name="append"></slot>
  44. </template>
  45. </el-table>
  46. </div>
  47. <div class="app-table__footer">
  48. <slot name="footer"></slot>
  49. </div>
  50. <app-table-setting :columns="columns" @update="updateColumn" @closed="showTableSetting = false"
  51. v-if="showTableSetting" />
  52. </div>
  53. </template>
  54. <script lang="ts">
  55. import { defineComponent, shallowRef, PropType } from 'vue'
  56. import { handleNoneValue } from '@/filters'
  57. import { i18n } from '@/stores'
  58. import AppTableSetting from '../table-setting/index.vue'
  59. export default defineComponent({
  60. inheritAttrs: false,
  61. components: {
  62. AppTableSetting
  63. },
  64. emits: ['refresh', 'update:columns', 'rowClick', 'select'],
  65. props: {
  66. columns: {
  67. type: Array as PropType<Model.TableColumn[]>,
  68. default: () => ([])
  69. },
  70. // 是否显示头部工具栏
  71. showToolbar: Boolean,
  72. loading: Boolean,
  73. showIndex: Boolean,
  74. // 是否要高亮当前行
  75. highlightCurrentRow: {
  76. type: Boolean,
  77. default: true
  78. },
  79. // 选择列类型
  80. selectionType: {
  81. type: String as PropType<'single' | 'multiple'>,
  82. }
  83. },
  84. setup(props, { emit, expose }) {
  85. const tableRef = shallowRef()
  86. const tableRadio = shallowRef()
  87. const showTableSetting = shallowRef(false)
  88. const refresh = () => emit('refresh')
  89. const updateColumn = (value: Model.TableColumn[]) => emit('update:columns', value)
  90. const { global: { t } } = i18n
  91. // 当某一行被勾选时触发的事件
  92. const onSelect = (selection: unknown[], currentRow: unknown) => {
  93. const el = tableRef.value
  94. const rows = selection.filter((e) => {
  95. if (props.selectionType === 'single') {
  96. if (e === currentRow) {
  97. el.setCurrentRow(currentRow) // 高亮行
  98. return true
  99. } else {
  100. el.toggleRowSelection(e, false) // 单选取消其他选中的行
  101. return false
  102. }
  103. }
  104. return true
  105. })
  106. emit('select', rows, currentRow)
  107. }
  108. // 当某一行被点击时选中该行
  109. const onRowClick = (row: unknown) => {
  110. // if (props.selectionType) {
  111. // const el = tableRef.value
  112. // const selection = el.getSelectionRows()
  113. // const selected = selection.find((e: unknown) => e === row)
  114. // el.toggleRowSelection(row, selected ? false : true)
  115. // onSelect(el.getSelectionRows(), row)
  116. // }
  117. emit('rowClick', row)
  118. }
  119. const handleValue = (row: { [key: string]: unknown }, column: Model.TableColumn) => {
  120. const value = row[column.field]
  121. if (Number.isFinite(value) && column.decimal) {
  122. return Number(value).toFixed(column.decimal)
  123. }
  124. return handleNoneValue(value)
  125. }
  126. // 监听滚动条
  127. // onMounted(() => {
  128. // const scrollbar = tableRef.value?.scrollBarRef
  129. // const wrap = scrollbar?.wrapRef
  130. // wrap?.addEventListener('scroll', (e: Event) => {
  131. // const el = e.target as HTMLElement
  132. // console.log(el.scrollTop)
  133. // })
  134. // })
  135. // 暴露组件属性
  136. expose({
  137. elTable: tableRef
  138. })
  139. return {
  140. tableRef,
  141. tableRadio,
  142. showTableSetting,
  143. onSelect,
  144. onRowClick,
  145. handleValue,
  146. refresh,
  147. updateColumn,
  148. t
  149. }
  150. },
  151. })
  152. </script>
  153. <style lang="less">
  154. @import './index.less';
  155. </style>