当前位置:   article > 正文

vue3中对elementplus的table进行封装_vue3 封装element ui plus表单组件

vue3 封装element ui plus表单组件

子组件 table.vue

setup使用的是语法糖

  1. <template>
  2. <el-card>
  3. <!-- 表格 -->
  4. <el-table :data="tableData"
  5. border
  6. stripe
  7. style="width: 100%">
  8. <!-- 选择框 -->
  9. <el-table-column type="selection" width="50" v-if='select' @selection-change="handleSelectionChange"
  10. :multipleSelection='multipleSelection'
  11. >
  12. </el-table-column>
  13. <!--序号 -->
  14. <el-table-column type='index' width="80" v-if="showIndex" label="序号" >
  15. </el-table-column>
  16. <!-- 特殊处理列表 switch -->
  17. <template v-for="value in tableLabel ">
  18. <el-table-column v-if="value.type==='function'" :key="value.id" :prop='value.prop'
  19. :label='value.label' width="180" >
  20. <!-- vue3里不适应slot-scope ,使用#default="scope"-->
  21. <template #default="scope">
  22. <el-switch
  23. v-model="scope.row.mg_state"
  24. class="ml-2"
  25. style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
  26. />
  27. </template>
  28. </el-table-column>
  29. <!-- 特殊处理列表 操作,不会使用插槽 -->
  30. <el-table-column v-else-if="value.type==='opt'" :key="value.id" :prop='value.prop'
  31. :label='value.label' width="180" >
  32. <!-- vue3里不适应slot-scope ,使用#default="scope"-->
  33. <template #default="scope">
  34. <el-button size="small"
  35. type='primary'
  36. @click="handleEdit(scope.$index, scope.row)"
  37. >编辑</el-button
  38. >
  39. <el-button
  40. size="small"
  41. type="danger"
  42. @click="handleDelete(scope.$index, scope.row)"
  43. >删除</el-button
  44. >
  45. </template>
  46. <!-- 普通列表 -->
  47. </el-table-column>
  48. <el-table-column v-else :key="value.id"
  49. :prop='value.prop'
  50. :label='value.label'
  51. >
  52. </el-table-column>
  53. </template>
  54. </el-table>
  55. <!-- 分页 -->
  56. <div class="demo-pagination-block">
  57. <el-pagination
  58. v-model:currentPage="currentPage"
  59. v-model:page-size="pageSize"
  60. :page-sizes="[1, 5, 10, 15]"
  61. :small="small"
  62. :disabled="disabled"
  63. :background="background"
  64. layout="total, sizes, prev, pager, next, jumper"
  65. :total="total"
  66. @size-change="handleSizeChange"
  67. @current-change="handleCurrentChange"
  68. />
  69. </div>
  70. </el-card>
  71. </template>
  72. <script lang="ts" setup>
  73. import {ref,defineEmits,defineProps,} from 'vue'
  74. const props=defineProps({
  75. tableData:{
  76. type:Array,
  77. default:[]
  78. },
  79. tableLabel:{
  80. type:Object,
  81. default:[]
  82. },
  83. select:{
  84. type:Boolean,
  85. default:false
  86. },
  87. showIndex:{
  88. type:Boolean,
  89. default:false
  90. },
  91. currentPage:{
  92. type:Number,
  93. default:0
  94. },
  95. pageSize:{
  96. type:Number,
  97. default:0
  98. },
  99. total:{
  100. type:Number,
  101. default:0
  102. },
  103. })
  104. //注册emit
  105. const emit = defineEmits<{
  106. (e: "handleSizeChange", val: number): void;
  107. (e: "handleCurrentChange", val: number): void;
  108. }>();
  109. //定义emit的值
  110. const handleSizeChange = (val: any) => {
  111. emit("handleSizeChange", val);
  112. };
  113. const handleCurrentChange = (val: any) => {
  114. emit("handleCurrentChange", val);
  115. };
  116. </script>
  117. <style scoped>
  118. .demo-pagination-block{
  119. margin-top: 20px;
  120. text-align: center;
  121. }
  122. .el-pagination{
  123. text-align: center;
  124. }
  125. </style>

父组件

  1. <template>
  2. <div>
  3. <Table
  4. :tableData="tableData"
  5. :tableLabel="tableLabel"
  6. :showIndex="true"
  7. :select="true"
  8. :total='total'
  9. :currentPage='queryinfo.pagenum'
  10. :pageSize='queryinfo.pagesize'
  11. @handleSizeChange='handleSizeChange'
  12. @handleCurrentChange='handleCurrentChange'
  13. >
  14. </Table>
  15. <pre>
  16. {{ tableData }}
  17. </pre>
  18. </div>
  19. </template>
  20. <script lang='ts' setup>
  21. import { Users } from "../api/api";
  22. import { reactive, ref } from "vue";
  23. import Table from "../components/commen/table.vue";
  24. const queryinfo = reactive({
  25. query: "",
  26. pagenum: 1,
  27. pagesize: 2,
  28. });
  29. const showIndex = ref(true);
  30. const select = ref(true);
  31. let tableData = ref([]);
  32. let total=ref()
  33. const getUser = async () => {
  34. const res = await Users(queryinfo);
  35. console.log(res);
  36. tableData.value = res.data.data.users;
  37. total.value=res.data.data.total
  38. console.log(tableData.value);
  39. };
  40. const handleSizeChange=(val)=>{
  41. queryinfo.pagesize=val
  42. console.log('父子间',val)
  43. getUser()
  44. }
  45. const handleCurrentChange=(val)=>{
  46. queryinfo.pagenum=val
  47. console.log('父子间',val)
  48. getUser()
  49. }
  50. const tableLabel = reactive([
  51. { prop: "username", label: "用户名" },
  52. { prop: "role_name", label: "角色" },
  53. { prop: "mobile", label: "电话" },
  54. { prop: "email", label: "邮箱" },
  55. {
  56. prop: "mg_state",
  57. label: "状态",
  58. type: "function",
  59. callback: (row: any) => {
  60. return "123";
  61. },
  62. },
  63. { label: "操作",type:'opt' },
  64. ]);
  65. getUser();
  66. </script>
  67. <style lang="scss" scoped>
  68. </style>

操作里的按钮没有使用插槽

效果图

 

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

闽ICP备14008679号