在使用ElementUI时想在表格的列中嵌套额外的组件使用
代码如下:
<el-table :data="pjTestData" :border="true" :highlight-current-row="true" id="pjtable" style="width:634px;position:relative;top:50px;height:266px;margin-bottom:25px;">
<el-table-column prop="name" label="调查项目" :resizable="false" width="354">
</el-table-column>
<el-table-column prop="key" label="是否进行" :resizable="false" width="109">
<el-switch style="display: block" v-model="key" active-color="#13ce66" inactive-color="#ff4949" active-text="是"
inactive-text="否">
</el-switch>
</el-table-column>
<el-table-column prop="rank" :resizable="false" label="满意度" width="169">
<el-rate v-model="pjTestData.rank" show-text>
</el-rate>
</el-table-column>
</el-table>
<script>
var vm = new Vue({
el: "#app",
data: {
pjTestData: [{
name: 'Q1销售人员接待情况的整体满意度',
key: true,
rank: null
},
{
name: 'Q2销售顾问是否有主动出迎',
key: false,
rank: null
},
{
name: 'Q3销售顾问是否有向您详细地介绍汽车配置和优点和好处',
key: true,
rank: null
}
]
}
})
</script>
但是这样就会产生一个问题,组件中的v-model="key"不能获取到列属性中prop的值,导致组件无法正常使用,通过查阅了解到一种方法可以解决以上问题。
解决后的代码:
<el-table :data="pjTestData" :border="true" :highlight-current-row="true" id="pjtable" style="width:634px;position:relative;top:50px;height:266px;margin-bottom:25px;">
<el-table-column prop="name" label="调查项目" :resizable="false" width="354">
</el-table-column>
<el-table-column prop="key" label="是否进行" :resizable="false" width="109">
<template slot-scope="scope">
<el-switch style="display: block" v-model="scope.row.key" active-
color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否">
</el-switch>
</template>
</el-table-column>
<el-table-column prop="rank" :resizable="false" label="满意度" width="169">
<template slot-scope="scope">
<el-rate v-model="scope.row.rank" show-text></el-rate>
</template>
</el-table-column>
</el-table>
只要在el-table-column中想单独重复使用的组件外包裹一层<template slot-scope="scope">/template>就可以了,组件中的邦定值要设置成scope.row.属性值就可以了!
效果图:
![](https://img-blog.csdnimg.cn/2019120212123818.png)
这样就可以单独选取每一行的值不会互相影响了!