问题描述:
接口中拿到table表格需要的数组数据,对每一行(scope)数据进项打分,如果绑定数据v-model,没办法批量监控到具体的data中。
解决:
html代码:
<el-table-column
width="80"
align="center"
label="自评">
<template slot-scope="scope">
<select class="select" @change="radioChange" v-model="tableData[scope.$index].getScore">
<option :value="item.value" v-for="item in tableData[scope.$index].options">{{item.key}}
</option>
</select>
</template>
</el-table-column>
<%--
说明:
tableData[scope.$index].getScore : 动态获取 接口数组数据 的得分。
tableData[scope.$index].options:动态获取 接口数组数据 得分数组。
@change="radioChange:方便调试监控数据。无业务需要
--%>
js部分:
data: {
tableData: [],
},
updated() {
// 监听使用
console.log('updated', this.tableData)
},
methods: {
// 监听使用
radioChange() {
this.$forceUpdate();
},
// 获取接口数据
getDate() {
$.ajax({
type: 'post',
url: '',
dataType: 'json',
success: function (result) {
vm.tableData = result
// 给select 选择器动态options数据
$.each(vm.tableData, function (i, item) {
item.options = [
{key: 'A', value: item.a},
{key: 'B', value: item.b},
{key: 'C', value: item.c},
{key: 'D', value: item.d},
];
// 整个将绑定 v-model 的值
item.getScore = item.options[0].value;
})
// console.log('tableData', vm.tableData)
}
});
},
// 保存数据,并提交接口
saveData() {
// console.log(this.tableData)
let arr = [];
$.each(vm.tableData, function (i, item) {
// 获取需要的每行数据,最后放到一个数组中
let obj = {
twoId: item.twoId,
getScore: item.getScore,
};
arr.push(obj);
})
console.log('arr',arr)
$.ajax({
type: 'POST',
url: '',
data: {
params: JSON.stringify(arr)
},
dataType: 'json',
traditional: true,//这里设置为true
success: function (data) {}
});
},
后台接口:
@RequestMapping("")
@ResponseBody
public int saveSelfEva(@RequestParam Map<String, Object> params) {
List<Map> list = JSON.parseArray((String) params.get("params"), Map.class);
// ... 具体业务
}
原tableData 数据结构:
处理后的tableData 数据结构: