赞
踩
本文是对el-table的基本封装,很常规的操作,主要是对列的封装,可以根据列配置数组进行渲染,后续会追加分页,列分类格式化等扩展封装
//BaseTable.vue
<template> <el-table> <el-table-column v-for="(col,index) in config" :key="index" v-bind="col"> <template v-if="col.slot" #default="scope" > <slot :name="col.slot" :row="scope.row"></slot> </template> </el-table-column> </el-table> </template> <script lang="ts" setup> defineProps({ config:{ type:Object, default:()=>({}) } }) </script>
//index.vue
<template> <BaseTable :config="config" :data="tableData" :style="{width:'500px'}"> <template #btn="{row}"> <el-button type="primary">{{ row.btn }}</el-button> </template> </BaseTable> </template> <script lang="ts" setup> import BaseTable from './BaseTable.vue' const config=[ { type:'selection' }, { prop: 'date', label:'Date', width:'180' }, { prop: 'name', label:'Name', }, { prop: 'btn', label:'action', slot:'btn' }, ]; const tableData = [ { date: '2016-05-03', name: 'Tom', btn: 'confirm', }, { date: '2016-05-02', name: 'Tom', btn: 'confirm', }, { date: '2016-05-04', name: 'Tom', btn: 'confirm', }, ]
//BaseTable.vue
<template> <el-table v-bind="$attrs.table"> <el-table-column v-for="(col,index) in $attrs.config" :key="index" v-bind="col"> <template v-if="col.slot" #default="scope" > <slot :name="col.slot" :row="scope.row"></slot> </template> </el-table-column> </el-table> </template> <script lang="ts" setup> defineOptions({ inheritAttrs: false }) </script> </script>
//index.vue
<template> <BaseTable v-bind="config"> <template #btn="{ row }"> <el-button type="primary">{{ row.btn }}</el-button> </template> </BaseTable> </template> <script lang="ts" setup> import BaseTable from './BaseTable2.vue' const config={ table:{ data: [ { date: '2016-05-03', name: 'Tom', btn: 'confirm', }, { date: '2016-05-02', name: 'Tom', btn: 'confirm', }, { date: '2016-05-04', name: 'Tom', btn: 'confirm', }, ], style:{width:'500px'} }, config:[ { type:'selection' }, { prop: 'date', label:'Date', width:'180' }, { prop: 'name', label:'Name', }, { prop: 'btn', label:'action', slot:'btn' }, ] } </script>
//BaseTable.vue
<template>
<el-table>
<el-table-column v-for="(col,index) in $attrs.config" :key="index" v-bind="col">
<template v-if="col.slot" #default="scope" >
<slot :name="col.slot" :row="scope.row"></slot>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
</script>
//index.vue
<template> <BaseTable :config="config" :data="tableData" :style="{width:'500px'}"> <template #btn="{row}"> <el-button type="primary">{{ row.btn }}</el-button> </template> </BaseTable> </template> <script lang="ts" setup> import BaseTable from './BaseTable.vue' const config=[ { type:'selection' }, { prop: 'date', label:'Date', width:'180' }, { prop: 'name', label:'Name', }, { prop: 'btn', label:'action', slot:'btn' }, ]; const tableData = [ { date: '2016-05-03', name: 'Tom', btn: 'confirm', }, { date: '2016-05-02', name: 'Tom', btn: 'confirm', }, { date: '2016-05-04', name: 'Tom', btn: 'confirm', }, ] </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。