当前位置:   article > 正文

Vue3-Element表格组件(el-table)的简单封装_element 封装table

element 封装table

封装的表格-myTable.vue

 

  1. <template>
  2. <div>
  3. <el-table :data="dataSource" style="width: 100%">
  4. <template v-for="(column, ind, key) in columns" :key="column">
  5. <el-table-column v-if="column.scopeVal" :prop="column.prop" :label="column.label">
  6. <template #default="scope">
  7. <slot
  8. :column="column"
  9. :record="scope.row"
  10. :text="scope.row[column.prop]"
  11. :index="dataSource.indexOf(scope.row)"
  12. :name="column.prop">
  13. </slot>
  14. </template>
  15. </el-table-column>
  16. <el-table-column v-else :prop="column.prop" :label="column.label" />
  17. </template>
  18. </el-table>
  19. </div>
  20. </template>
  21. <script>
  22. import reactive from 'vue'
  23. export default {
  24. props: {
  25. dataSource: Array,
  26. columns: Array,
  27. }
  28. }
  29. </script>

父组件调用.vue

  1. <template>
  2. <el-card>
  3. <my-table :columns="columns" :dataSource="dataSource">
  4. <template #method="{ index }">
  5. <my-select :options="method_options" v-model="dataSource[index].method" placeholder="待选择"></my-select>
  6. </template>
  7. <template #value="{ index }">
  8. <el-input v-model="dataSource[index].value" placeholder="待填写"></el-input>
  9. </template>
  10. <template #text="{ index }">
  11. <el-input v-model="dataSource[index].text" placeholder="待填写"></el-input>
  12. </template>
  13. <template #notes="{ column, record, text, index }">
  14. <el-input v-model="dataSource[index].notes" placeholder="待填写"></el-input>
  15. </template>
  16. <template #action="{ index }" class="action">
  17. <el-link><el-icon><CirclePlusFilled /></el-icon></el-link>
  18. <el-link><el-icon><RemoveFilled /></el-icon></el-link>
  19. <el-link><el-icon><ArrowUpBold /></el-icon></el-link>
  20. <el-link><el-icon><ArrowDownBold /></el-icon></el-link>
  21. </template>
  22. </my-table>
  23. </el-card>
  24. </template>
  25. <script setup>
  26. import { reactive, ref } from "vue"
  27. import myTable from "../../components/table/myTable.vue";
  28. import mySelect from "../../components/select/myselect.vue"
  29. components: { myTable, mySelect }
  30. const method_options = [
  31. {
  32. label: '访问',
  33. value: '访问'
  34. },
  35. {
  36. label: '点击',
  37. value: '点击'
  38. },
  39. {
  40. label: '输入',
  41. value: '输入'
  42. },
  43. {
  44. label: '断言',
  45. value: '断言'
  46. },
  47. ]
  48. const columns = reactive([
  49. {
  50. label: '方法',
  51. prop: 'method',
  52. scopeVal: true,
  53. },
  54. {
  55. label: '元素定位',
  56. prop: 'value',
  57. scopeVal: true,
  58. },
  59. {
  60. label: '输入',
  61. prop: 'text',
  62. scopeVal: true,
  63. },
  64. {
  65. label: '备注',
  66. prop: 'notes',
  67. scopeVal: true,
  68. },
  69. {
  70. label: '状态',
  71. prop: 'status',
  72. },
  73. {
  74. label: '操作',
  75. prop: 'action',
  76. scopeVal: true,
  77. },
  78. ])
  79. let dataSource = reactive([
  80. {
  81. method: '访问',
  82. value: '',
  83. text: 'http://www.baidu.com',
  84. notes: '访问百度',
  85. status: '待运行',
  86. },
  87. {
  88. method: '点击',
  89. value: '//*[@id=kw]',
  90. text: '',
  91. notes: '点击搜索框',
  92. status: '待运行',
  93. },
  94. ])

说明

通过作用域插槽传递以下4个参数供使用

        column:表头

        record:当前行数据

        text:当前单元格数据

        index:当前行索引

若要自定义某一列的元素,在column中的对应列名下加上scopeVal: true

效果

最近在慢慢转vue3,过程中还是用vue2的写法和思维去写,虽说vue3兼容vue2,但有些地方还是有点区别,在作用域插槽上也花了点时间。。。

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

闽ICP备14008679号