custom-table.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <el-row>
  3. <el-col>
  4. <el-table :data="data" border :height="height" @selection-change="toSelect">
  5. <el-table-column type="selection" width="55" v-if="select"> </el-table-column>
  6. <template v-for="f in fields" :key="f.model">
  7. <el-table-column v-if="f.custom" :label="f.label" :prop="f.model" align="center" v-bind="f.options">
  8. <template v-slot="{ row }">
  9. <slot :name="f.model" v-bind="{ f, row }"></slot>
  10. </template>
  11. </el-table-column>
  12. <el-table-column v-else :label="f.label" :prop="f.model" align="center" :formatter="toFormatter"></el-table-column>
  13. </template>
  14. <el-table-column :label="$t('common.opera')" align="center" v-if="opera.length > 0">
  15. <template v-slot="{ row, $index }">
  16. <slot v-bind="{ row }">
  17. <template v-for="f in opera">
  18. <template v-if="display(f, row)">
  19. <el-link :key="f.method" :type="f.type || 'primary'" size="small" :underline="false" class="link" v-method="f.method" @click="handleOpera(f, row, $index)">
  20. {{ f.label }}
  21. </el-link>
  22. </template>
  23. </template>
  24. </slot>
  25. </template>
  26. </el-table-column>
  27. </el-table>
  28. </el-col>
  29. </el-row>
  30. <el-row justify="end">
  31. <el-pagination
  32. background
  33. layout="total, prev, pager, next"
  34. :page-sizes="[10, 20, 50, 100, 200]"
  35. :total="total"
  36. :page-size="limit"
  37. v-model:current-page="currentPage"
  38. @current-change="changePage"
  39. @size-change="sizeChange"
  40. >
  41. </el-pagination>
  42. </el-row>
  43. </template>
  44. <script setup>
  45. import { isFunction, get, isString, cloneDeep } from 'lodash-es'
  46. const props = defineProps({
  47. data: { type: Array, default: () => [] },
  48. height: { type: String, default: '60vh' },
  49. fields: { type: Array, default: () => [] },
  50. opera: { type: Array, default: () => [] },
  51. total: { type: Number, default: 0 },
  52. limit: { type: Number, default: 10 },
  53. select: { type: Boolean, default: false }
  54. })
  55. const emit = defineEmits(['query', 'toSelect'])
  56. const toSelect = (val) => {
  57. emit(`toSelect`, val)
  58. }
  59. const handleOpera = (field, data, index) => {
  60. let { method, confirm = false, methodZh, label, confirmWord } = cloneDeep(field)
  61. if (isFunction(methodZh)) methodZh = methodZh(data)
  62. else if (isString(methodZh)) {
  63. methodZh = label
  64. }
  65. if (confirm) {
  66. let word = methodZh ? methodZh : `您确认${label}该数据?`
  67. if (confirmWord) word = confirmWord
  68. ElMessageBox.confirm(word, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
  69. .then(() => {
  70. emit(method, cloneDeep(data), index)
  71. })
  72. .catch(() => {})
  73. } else emit(method, cloneDeep(data), index)
  74. }
  75. /**
  76. * 根据field中的 format函数 格式化该单元格数据
  77. * @param {Object} row 本行数据
  78. * @param {Object} column 本列实例
  79. * @param {any} cellValue 该单元格原数据
  80. */
  81. const toFormatter = (row, column, cellValue) => {
  82. // 先找到field
  83. const fields = get(props, 'fields')
  84. if (!fields) return cellValue
  85. let this_field = fields.find((fil) => fil.model === column.property)
  86. if (!this_field) return cellValue
  87. // 再找field中format函数
  88. let format = get(this_field, `format`, false)
  89. if (!format) return cellValue
  90. if (isFunction(format)) {
  91. const formatResult = format(cellValue, row, this_field)
  92. return formatResult
  93. }
  94. }
  95. const display = (field, row) => {
  96. let display = get(field, `display`, true)
  97. if (display === true) return true
  98. else {
  99. let res = display(row)
  100. return res
  101. }
  102. }
  103. const currentPage = ref(1)
  104. // 分页
  105. const changePage = (page = currentPage.value) => {
  106. emit('query', { skip: (page - 1) * props.limit, limit: props.limit })
  107. }
  108. </script>
  109. <style scoped>
  110. .el-row {
  111. padding-top: 20px;
  112. }
  113. .link {
  114. padding: 0 5px 0 0;
  115. }
  116. .page {
  117. margin: 10px 0 0 0;
  118. }
  119. </style>