<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column
v-for="(item, index) in tableHeader"
:key="index"
:label="item.text"
width="180"
:property="item"
>
<template slot-scope="scope">
{{ scope.row[scope.column.property.dataIndex] }}
{{[scope.column.property.hidden]}}
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableHeader: [
{ dataIndex: "date", text: "日期 ", hidden: false, type: "String" },
{ dataIndex: "name", text: "姓名 ", hidden: false, type: "int" },
{ dataIndex: "address", text: "地址", hidden: true, type: "String" },
{ dataIndex: "address", text: "地址", hidden: false, type: "String" },
],
tableData: [
{
date: "2016-05-02",
name: "王da虎",
address: "上海市普陀区金沙江路 1518 弄",
},
{
date: "2016-05-04",
name: "王小虎",
address: "上海市普陀区金沙江路 1517 弄",
},
{
date: "2016-05-01",
name: "王小虎",
address: "上海市普陀区金沙江路 1519 弄",
},
{
date: "2016-05-03",
name: "王小虎",
address: "上海市普陀区金沙江路 1516 弄",
},
],
};
},
};
</script>
:property 的 item 是 tableHeader 中的每条数据;
tableHeader 的决定了渲染表头,hidden可以决定该列是否展示(v-if判断);
template 中渲染的每一条 scope 插槽的数据都来自 el-table 的 :data;
[scope.column.property.hidden] 选择的是每条数据的 某个属性的 属性值;
至于动态添加 tableHeader,还是要获取到数据后遍历值然后重组一下数据,这个未来可能会补充