当前位置:   article > 正文

vue3封装elementUi表格组件_vue3 封装elementui表格组件

vue3 封装elementui表格组件
  1. <template>
  2. <el-table
  3. ref="refTable"
  4. :data="tableData.data"
  5. :style="{ width: '100%', ...cellStyle }"
  6. :header-cell-style="headerCellStyle"
  7. :cell-style="cellStyle"
  8. :max-height="tableHeight"
  9. @selection-change="handleSelectionChange"
  10. @select="handleSelectChange"
  11. @select-all="handleSelectAllChange"
  12. >
  13. <el-table-column v-if="selectShow" type="selection" width="55" />
  14. <el-table-column
  15. v-for="item in tableData.titles"
  16. :key="item.prop"
  17. :prop="item.prop"
  18. :label="item.title"
  19. :width="item.width"
  20. show-overflow-tooltip
  21. >
  22. <template v-if="item?.custom" #default="scope">
  23. <slot :name="item.title" :scope="scope"></slot>
  24. </template>
  25. </el-table-column>
  26. <template #empty>
  27. <div class="empty" :style="{ height: tableHeight - 44 + 'px', ...cellStyle }">暂无数据</div>
  28. </template>
  29. </el-table>
  30. </template>
  31. <script setup>
  32. import { ref } from 'vue'
  33. //暂无数据列表
  34. let props = defineProps({
  35. // 表格数据以及表头数据
  36. tableData: {
  37. type: Object,
  38. default: () => {
  39. return {}
  40. },
  41. },
  42. // 设置斑马纹的颜色
  43. // stripeColor: {
  44. // type: String,
  45. // default: '#fff',
  46. // },
  47. // 表格高度
  48. tableHeight: {
  49. type: Number,
  50. default: 500,
  51. },
  52. // 表头样式
  53. headerCellStyle: {
  54. type: Object,
  55. default: () => {
  56. return { color: '#232932', background: '#f5f5f5' }
  57. },
  58. },
  59. // 表格样式
  60. cellStyle: {
  61. type: Object,
  62. default: () => {
  63. return { color: '#646a73', background: '#f5f5f5' }
  64. },
  65. },
  66. // 是否显示选择栏
  67. selectShow: {
  68. type: Boolean,
  69. default: true,
  70. },
  71. })
  72. let emit = defineEmits(['selectionChange', 'delete', 'selectChange', 'selectAllChange'])
  73. // 选中项发生改变
  74. const handleSelectionChange = (selection) => {
  75. emit('selectionChange', selection)
  76. }
  77. // 手动触发勾选
  78. const handleSelectChange = (selection, row) => {
  79. emit('selectChange', selection, row)
  80. }
  81. // 手动触发全选
  82. const handleSelectAllChange = (selection) => {
  83. emit('selectAllChange', selection)
  84. }
  85. const refTable = ref()
  86. // 回显选中
  87. const handleEcho = (echoData) => {
  88. clearSelection()
  89. if (echoData && echoData.length) {
  90. for (const row of echoData) {
  91. refTable.value.toggleRowSelection(row, true)
  92. }
  93. }
  94. }
  95. // 清空选择
  96. const clearSelection = () => {
  97. refTable.value.clearSelection()
  98. }
  99. defineExpose({ handleEcho, clearSelection })
  100. </script>
  101. <style lang="scss" scoped>
  102. .resource_button {
  103. cursor: pointer;
  104. }
  105. :deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  106. background: v-bind('props.stripeColor');
  107. }
  108. :deep(.el-table__header th .cell) {
  109. border-right: 1px solid #dfdfdf;
  110. }
  111. :deep(.el-table__header th:last-child .cell) {
  112. border-right: none;
  113. }
  114. .empty {
  115. text-align: center;
  116. width: 100%;
  117. display: flex;
  118. align-items: center;
  119. }
  120. </style>
调用组件
  1. <template>
  2. <div ref="rightTable" class="right-table">
  3. <TableList
  4. ref="tableList"
  5. :table-data="tableData"
  6. :table-height="appManageHeight"
  7. @selection-change="handleSelectionChange"
  8. >
  9. <template #角色="{ scope }">
  10. <span>{{ scope.row.type == 0 ? '管理员' : '普通用户' }}</span>
  11. </template>
  12. </TableList>
  13. </div>
  14. </template>
  15. <script setup>
  16. import { onMounted, reactive, ref } from 'vue'
  17. import TableList from '@/components/tableGroup/tableList.vue'
  18. import { debounce } from 'lodash'
  19. const tableData = reactive({
  20. data: [
  21. {
  22. nickName: '张三',
  23. username: 'zhangsan',
  24. type: 0,
  25. orgName: '华为',
  26. },
  27. {
  28. nickName: '李四',
  29. username: 'lisi',
  30. type: 1,
  31. orgName: '腾讯',
  32. },
  33. {
  34. nickName: '王五',
  35. username: 'wangsu',
  36. type: 1,
  37. orgName: '阿里',
  38. },
  39. {
  40. nickName: '赵六',
  41. username: 'zhaoliu',
  42. type: 0,
  43. orgName: '京东',
  44. },
  45. ],
  46. titles: [
  47. {
  48. title: '用户',
  49. prop: 'nickName',
  50. },
  51. {
  52. title: '登录账号',
  53. prop: 'username',
  54. },
  55. {
  56. title: '角色',
  57. prop: 'type',
  58. custom: true,
  59. },
  60. {
  61. title: '所属企业',
  62. prop: 'orgName',
  63. },
  64. ],
  65. })
  66. // 选中项发生改变
  67. const handleSelectionChange = debounce((val) => {
  68. console.log('val---', val);
  69. }, 100)
  70. const rightTable = ref()
  71. const appManageHeight = ref(0)
  72. onMounted(() => {
  73. appManageHeight.value = rightTable.value ? rightTable.value.clientHeight - 50 : 500
  74. })
  75. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/684256
推荐阅读
相关标签
  

闽ICP备14008679号