封装 element-ui el-table

旧城等待, 2022-09-16 14:20 110阅读 0赞

element-ui el-table功能很强大,也很好用,但是组件写起来有些麻烦,需要进一步封装,下面是一个封装的例子,可以 formatData,可以利用 slot 动态传递模板,大家觉得有用可以看看

  1. const columns = [{ key: 'name', minWidth: 150, fixed: true, showOverflowTooltip: true}, { slot: 'actions'}];
  2. <v-table :data="data" :columns="columns">
  3. <template slot="actions">
  4. <<el-table-column>
  5. <el-button @click="buttonClick"></el-button>
  6. </<el-table-column>
  7. </template>
  8. <v-table>
  9. <template>
  10. <e-table
  11. :data="data"
  12. :border="!!border"
  13. style="width: 100%"
  14. :header-cell-style="headerCellStyle"
  15. :max-height="maxHeight"
  16. :empty-text="emptyText"
  17. >
  18. <!-- index column -->
  19. <template v-if="showIndex">
  20. <el-table-column
  21. :label="indexColumnOptions.label || '序号'"
  22. type="index"
  23. :fixed="indexColumnFixed"
  24. :width="indexColumnOptions.width || 50"
  25. :align="indexColumnOptions.align || 'left'"
  26. :show-overflow-tooltip="indexColumnOptions.showOverflowTooltip || false"
  27. ></el-table-column>
  28. </template>
  29. <!-- -->
  30. <template v-for="columnItem in columns">
  31. <template v-if="!columnItem.slot">
  32. <el-table-column
  33. :key="columnItem.key"
  34. :label="columnItem.label"
  35. :prop="columnItem.key"
  36. :align="columnItem.align || 'left'"
  37. :fixed="columnItem.fixed || false"
  38. :show-overflow-tooltip="columnItem.showOverflowTooltip || false"
  39. :min-width="columnItem.minWidth"
  40. >
  41. <template slot-scope="scope">
  42. { {
  43. columnItem.formatData
  44. ? columnItem.formatData(scope.row[columnItem.key], scope.row)
  45. : scope.row[columnItem.key] || ''
  46. }}
  47. </template>
  48. </el-table-column>
  49. </template>
  50. <template v-else>
  51. <slot :name="columnItem.slot"></slot>
  52. </template>
  53. </template>
  54. </e-table>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'VTable',
  59. props: {
  60. border: Boolean,
  61. showIndex: {
  62. type: Boolean,
  63. default: false,
  64. },
  65. indexColumnOptins: {
  66. type: Object,
  67. default() {
  68. return { };
  69. },
  70. },
  71. data: {
  72. type: Array,
  73. default() {
  74. return [];
  75. },
  76. },
  77. columns: {
  78. type: Array,
  79. default() {
  80. return [];
  81. },
  82. },
  83. headerCellStyle: {
  84. type: Object,
  85. default() {
  86. return { };
  87. },
  88. },
  89. maxHeight: String,
  90. emptyText: {
  91. type: String,
  92. default: '暂无相关信息',
  93. },
  94. },
  95. data() {
  96. return { };
  97. },
  98. computed: {
  99. indexColumnFixed() {
  100. return this.indexColumnOptins.fixed || this.columns.some((column) => !!column.fixed);
  101. },
  102. },
  103. mounted() { },
  104. };
  105. </script>
  106. <style scoped></style>

发表评论

表情:
评论列表 (有 0 条评论,110人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue结合ElementUI封装菜单管理

    前言 本文将介绍如何使用Vue和ElementUI开发一个简单的菜单管理系统。菜单管理系统可以让用户方便地添加、修改和删除菜单,是很多网站和应用程序的基础管理功能之一。