Element el-table中插入select下拉框 (每行数据只作用于当前行)
简介:
项目中遇到了这个问题,因为之前没有碰到过这种功能问题,所以一时间页无从下手,在网上搜了很多,可能是因为自己马虎或者没有仔细看,漏掉了很多细节,下面话不多说上代码
<el-table-column label="领域" align="center">
<template slot-scope="scope">
<el-select
v-model="scope.row.categoryname"
@change="changecategoryname(scope.row, scope.$index)"
size="small"
>
<el-option
v-for="item in list_firstmodule"
:label="item.realname"
:value="item.id"
:key="item.id"
></el-option>
</el-select>
</template>
</el-table-column>
<el-table-column label="项目" align="center">
<template slot-scope="scope">
<el-select
v-model="scope.row.parentprojectname"
style="width:150px"
@change="superprojectversion"
size="small"
>
<el-option
v-for="item in scope.row.list_super"
:label="item.projectname"
:value="item.id"
:key="item.id"
></el-option>
</el-select>
</template>
</el-table-column>
说明:
因为项目需求,我需要根据领域的选值来获取项目的值
如果是其他模块,一样的道理,这样做就对了~
上面的html代码没什么重点,主要是change事件需要传scope.row & scope.$index,另外下面的(不限于)项目模块遍历的值需要加上scope.row,其实就是把被遍历的数组变成了tabledata的值(下面会说明)
methods:
// 领域模块change获取项目模块的值
changecategoryname (row, index) {
let modules = row.categoryname
let supers = row.parentprojectname
this.$api.get("url",
{
params: {
key: supers,
category: modules,
},
}).then((res) => {
if (res.status == 200) {
this.list_super = res.data.data.data || [];
this.$set(this.tablelist[index],'list_super', this.list_super);
}
});
},
change事件方法,用到$set方法
可点击 vue使用set方法查看set具体使用方法
回归正题, 上面的方法中主要是最后一句,通过set方法使原本的table表格data数据添加"list_super"这一组数据,再联系上面的scope.row.list_super说明这种方法是把获取到的数据添加到data数据中,小白个人理解,如有问题请各位大佬多多指教~