效果
將el-table和el-table-column獨立出來
el-table封裝如下:
MyTable.vue
<template>
<div class="my-table">
<el-table :data="data">
<my-column v-for="(item,index) in col" :key="index" :col="item"></my-column>
</el-table>
</div>
</template>
<script>
import MyColumn from './MyColumn'
export default {
components: {
MyColumn
},
props: {
col: {
type: Array
},
data: {
type: Array
}
}
}
</script>
<style scoped>
</style>
el-table-column封裝如下:
MyColumn.vue
<template>
<el-table-column
:prop="col.prop"
:label="col.label"
align="left">
<template v-if="col.children && col.children.length">
<my-column v-for="(item, index) in col.children"
:key="index"
:col="item"></my-column>
</template>
</el-table-column>
</template>
<script>
export default {
name: 'MyColumn',
props: {
col: {
type: Object
}
}
}
</script>
<style scoped>
</style>
使用
<template>
<div class="my-table">
<my-table v-if="tableShow" :col="col" :data="data"></my-table>
</div>
</template>
<script>
import MyTable from '../components/MyTable'
import { getBt } from '../common/api/system_userApi'
export default {
components: {
MyTable
},
data() {
return {
tableShow:true,// 使組件重新渲染變量
col: [
{
prop: 'date',
label: '日期'
},
{
label: '配送信息',
children: [
{
prop: 'name',
label: '姓名'
},
{
label: '地址',
children: [
{
prop: 'province',
label: '省份',
},
{
prop: 'city',
label: '市區(qū)'
},
{
prop: 'address',
label: '地址'
}
]
}
]
}
],
data: [
{
date: '2016-05-03',
name: '王小虎',
province: '上海',
city: '普陀區(qū)',
address: '上海市普陀區(qū)金沙江路 1518 弄',
zip: 200333
},
{
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀區(qū)',
address: '上海市普陀區(qū)金沙江路 1518 弄',
zip: 200333
}
]
}
},
created() {
this.init();
},
methods:{
//調(diào)接口從后臺獲取表頭數(shù)據(jù)
init(){
getBt({}).then(res => {
this.tableShow = false// 設(shè)置tableShow為false惕味,使組件重新渲染
this.col=res.tbCsBts
this.$nextTick(() => {// DOM 更新了
this.tableShow = true
})
});
}
}
}
</script>
<style scoped>
</style>