index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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" scrollbar-always-on @row-click="onRowClick"
  19. @expand-change="onRowClick" @select="onSelect" @select-all="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" :selectable="selectable" width="55" align="center" fixed
  28. v-if="selectionType" />
  29. <!-- 序号 -->
  30. <el-table-column type="index" :label="t('common.orderindex')" align="center" :width="80" v-if="showIndex" />
  31. <!-- 数据列 -->
  32. <template v-for="(item, index) in columns" :key="index">
  33. <el-table-column :align="item.align ?? 'center'" :min-width="120" :width="item.width"
  34. :label="t(item.formatLabel?.() ?? item.label)" :prop="item.field" :fixed="item.fixed || false"
  35. :sortable="item.sortable" :show-overflow-tooltip="item.field !== 'operate'" v-if="item.show ?? true">
  36. <template #default="{ row, index }">
  37. <slot :name="item.field" :row="row" :value="row[item.field]" :index="index">
  38. <span :class="item.className">{{ handleValue(row, item) }}</span>
  39. </slot>
  40. </template>
  41. </el-table-column>
  42. </template>
  43. <template #append v-if="$slots.append">
  44. <slot name="append"></slot>
  45. </template>
  46. </el-table>
  47. </div>
  48. <div class="app-table__footer">
  49. <slot name="footer"></slot>
  50. </div>
  51. <app-table-setting :columns="columns" @update="updateColumn" @closed="showTableSetting = false"
  52. v-if="showTableSetting" />
  53. </div>
  54. </template>
  55. <script lang="ts">
  56. import { defineComponent, shallowRef, PropType } from 'vue'
  57. import { handleNoneValue } from '@/filters'
  58. import AppTableSetting from '../table-setting/index.vue'
  59. import { i18n } from '@/stores';
  60. export default defineComponent({
  61. inheritAttrs: false,
  62. components: {
  63. AppTableSetting
  64. },
  65. emits: ['refresh', 'update:columns', 'rowClick', 'select', 'selectable'],
  66. props: {
  67. columns: {
  68. type: Array as PropType<Model.TableColumn[]>,
  69. default: () => ([])
  70. },
  71. // 是否显示头部工具栏
  72. showToolbar: Boolean,
  73. loading: Boolean,
  74. showIndex: Boolean,
  75. // 是否要高亮当前行
  76. highlightCurrentRow: {
  77. type: Boolean,
  78. default: true
  79. },
  80. // 选择列类型
  81. selectionType: {
  82. type: String as PropType<'single' | 'multiple'>,
  83. }
  84. },
  85. setup(props, { emit, expose }) {
  86. const tableRef = shallowRef()
  87. const tableRadio = shallowRef()
  88. const showTableSetting = shallowRef(false)
  89. const refresh = () => emit('refresh')
  90. const updateColumn = (value: Model.TableColumn[]) => emit('update:columns', value)
  91. const { global: { t } } = i18n
  92. // 当某一行被勾选时触发的事件
  93. const onSelect = (selection: unknown[], currentRow: unknown) => {
  94. const el = tableRef.value
  95. const rows = selection.filter((e) => {
  96. if (props.selectionType === 'single') {
  97. if (e === currentRow) {
  98. el.setCurrentRow(currentRow) // 高亮行
  99. return true
  100. } else {
  101. el.toggleRowSelection(e, false) // 单选取消其他选中的行
  102. return false
  103. }
  104. }
  105. return true
  106. })
  107. emit('select', rows, currentRow)
  108. }
  109. // 选择列是否可以勾选,通过 callback 改变选中值
  110. const selectable = (row: unknown) => {
  111. let isSelected = true
  112. const callback = (value: boolean) => {
  113. isSelected = value
  114. }
  115. emit('selectable', row, callback)
  116. return isSelected
  117. }
  118. // 当某一行被点击时选中该行
  119. const onRowClick = (row: unknown) => {
  120. // if (props.selectionType) {
  121. // const el = tableRef.value
  122. // const selection = el.getSelectionRows()
  123. // const selected = selection.find((e: unknown) => e === row)
  124. // el.toggleRowSelection(row, selected ? false : true)
  125. // onSelect(el.getSelectionRows(), row)
  126. // }
  127. emit('rowClick', row)
  128. }
  129. const handleValue = (row: { [key: string]: unknown }, column: Model.TableColumn) => {
  130. const value = row[column.field]
  131. const formattedValue = column.formatValue ? column.formatValue(value) : value
  132. if (Number.isFinite(formattedValue) && column.decimal) {
  133. return Number(formattedValue).toFixed(column.decimal)
  134. }
  135. return handleNoneValue(formattedValue)
  136. }
  137. // 监听滚动条
  138. // onMounted(() => {
  139. // const scrollbar = tableRef.value?.scrollBarRef
  140. // const wrap = scrollbar?.wrapRef
  141. // wrap?.addEventListener('scroll', (e: Event) => {
  142. // const el = e.target as HTMLElement
  143. // console.log(el.scrollTop)
  144. // })
  145. // })
  146. // 暴露组件属性
  147. expose({
  148. elTable: tableRef
  149. })
  150. return {
  151. tableRef,
  152. tableRadio,
  153. showTableSetting,
  154. onSelect,
  155. selectable,
  156. onRowClick,
  157. handleValue,
  158. refresh,
  159. updateColumn,
  160. t
  161. }
  162. },
  163. })
  164. </script>
  165. <style lang="less">
  166. @import './index.less';
  167. </style>