当前位置:   article > 正文

vue3.0 + elementUI 公共组件表格(二次封装)_vue3表格组件

vue3表格组件

 vue3.0基于elementUI 和 ts 的基础上对表格进行简单的封装,只是为了展示如何来简单的表格封装自定义,组件并不是很完美,后面根据自己的需求在进行完善!

组件的使用:

  1. <table-info
  2. :tableLabel="tableLabel2"
  3. :tableData="tableData2"
  4. >
  5. <template #matchingResult="{ row }">
  6. <!-- #matchingResult 是列表中自定义的名字 通过这种方式来自定义表格中的内容 -->
  7. <p :class="{icon: true,
  8. icon_green: row.matchingResult == 0,
  9. icon_red: row.matchingResult == 1}"></p>
  10. </template>
  11. <template #StateSort="{ row }">
  12. <span :class="{ optSpan: row.matchingResult == 0 }"
  13. @click="infoBtnFn(row)">
  14. {{ row.matchingResult == 0 ? "详情查看" : "无主体对象" }}
  15. </span>
  16. </template>
  17. </table-info>

  1. // 表格键名和类型对应数组
  2. tableLabel2: [
  3. {
  4. label: "序号",
  5. type: "seq",
  6. width: 60,
  7. },
  8. {
  9. label: "对象本体",
  10. keyName: "ObjOntology",
  11. // sortable: true, // 排序
  12. },
  13. {
  14. label: "对比对象",
  15. keyName: "comparisonObj",
  16. },
  17. {
  18. label: "匹配结果",
  19. type: "custom", // custom 自定义
  20. name: "matchingResult",
  21. },
  22. {
  23. label: "操作",
  24. type: "custom", // custom 自定义
  25. name: "StateSort",
  26. },
  27. ],
  28. // 表格数据
  29. tableData2: [
  30. {
  31. id: "01",
  32. ObjOntology: "AAA表",
  33. comparisonObj: "AAA表",
  34. matchingResult: 0,
  35. },
  36. {
  37. id: "02",
  38. ObjOntology: "BBB表",
  39. comparisonObj:"BBB表" ,
  40. matchingResult: 0,
  41. },
  42. {
  43. id: "03",
  44. ObjOntology: "CCC表",
  45. comparisonObj: "CCC表",
  46. matchingResult: 1,
  47. },
  48. {
  49. id: "04",
  50. ObjOntology: "******",
  51. comparisonObj: "******",
  52. matchingResult: 1,
  53. },
  54. {
  55. id: "05",
  56. ObjOntology: "******",
  57. comparisonObj: "******",
  58. matchingResult: 0,
  59. }
  60. ],

组件代码

  1. <!-- 普通简单表格 -->
  2. <template>
  3. <div class="panelTable">
  4. <el-table
  5. :style="{ width: '100%' }"
  6. :data="tableData"
  7. stripe
  8. height="100%"
  9. v-loading="loading"
  10. element-loading-text="加载中…"
  11. :default-sort="{ prop: '', order: 'descending' }"
  12. >
  13. <template v-for="(item, index) in tableLabel">
  14. <el-table-column
  15. v-if="item.type == 'seq'"
  16. :key="item.type"
  17. label="序号"
  18. :fixed="item.fixed"
  19. align="center"
  20. :width="item.width"
  21. >
  22. <template #default="scope">
  23. <span v-if="pageIndex && page.size">{{
  24. scope.$index + (pageIndex - 1) * page.size + 1
  25. }}</span>
  26. <span v-else>{{ scope.$index + 1 }}</span>
  27. </template>
  28. </el-table-column>
  29. <el-table-column
  30. v-else-if="item.type === 'custom'"
  31. :key="item.name"
  32. :fixed="item.fixed"
  33. :label="item.label"
  34. align="center"
  35. :sortable="item.sortable"
  36. :width="item.width"
  37. :show-overflow-tooltip="true"
  38. >
  39. <template #default="scope">
  40. <slot :name="item.name" :row="scope.row" :index="index + 1"></slot>
  41. </template>
  42. </el-table-column>
  43. <el-table-column
  44. v-else
  45. :key="index"
  46. :fixed="item.fixed"
  47. :prop="item.keyName"
  48. :label="item.label"
  49. align="center"
  50. :sortable="item.sortable"
  51. :width="item.width"
  52. :show-overflow-tooltip="true"
  53. >
  54. </el-table-column>
  55. </template>
  56. </el-table>
  57. <div class="panelPage">
  58. <el-pagination
  59. v-if="page.size"
  60. background
  61. layout="->,total,prev, pager, next"
  62. :total="page.total"
  63. :page-size="page.size"
  64. :current-page="page.currentPage"
  65. @current-change="pageChangeHandle"
  66. />
  67. </div>
  68. </div>
  69. </template>
  70. <script lang="ts">
  71. import { defineComponent, ref, toRefs, reactive, onMounted, watch } from "vue";
  72. export default defineComponent({
  73. name: "SimpleTable",
  74. props: {
  75. tableLabel: {
  76. type: Array,
  77. default: () => {
  78. return [];
  79. },
  80. },
  81. tableData: {
  82. type: Array,
  83. default: () => {
  84. return [];
  85. },
  86. },
  87. page: {
  88. type: Object,
  89. default: () => {
  90. return {
  91. size: 15,
  92. currentPage: 1,
  93. total: 0
  94. };
  95. },
  96. },
  97. loading: {
  98. type: Boolean,
  99. default: false,
  100. },
  101. wrapperHeight: {
  102. type: String,
  103. default: "100%",
  104. },
  105. },
  106. setup(props, { emit }) {
  107. // 数据
  108. const state = reactive({
  109. pageIndex: 0,
  110. });
  111. // 方法
  112. const methods = reactive({
  113. // 点击分页
  114. pageChangeHandle(val: number) {
  115. state.pageIndex = val;
  116. emit("pageChange", val);
  117. },
  118. });
  119. return {
  120. ...toRefs(state),
  121. ...toRefs(methods),
  122. };
  123. },
  124. });
  125. </script>
  126. <style lang="scss" scoped>
  127. </style>

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

闽ICP备14008679号