问题描述:
一个表格10条数据,通过一次ajax请求,获取到了大部分数据,另外还需要根据第一个ajax获得的结果,再次请求新的接口
新请求的接口返回的数据,赋值后,表格内容没有变化。
getList() {
this.loading = true;
listValuedomainBase(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
this.valuedomainBaseList = response.rows;
this.total = response.total;
this.loading = false;
this.valuedomainBaseList.forEach((res,item)=>{
getItemCountBydomainId(res.id).then(itemRes=>{
res.itemCount=itemRes.data;
});
});
});
}
其中,通过console.log打印this.valuedomainBaseList的数据是变化了的
<el-table-column label="项目数" align="center" width="80" prop="itemCount">
<template slot-scope="scope">
<span>{{ scope.row.itemCount }}</span>
</template>
</el-table-column>
经过反复研究发现,问题在于第一次请求,listValuedomainBase返回的结果集中,没有itemCount属性,这个属性是foreach追加的,所以导致不生效。
解决办法:
调整listValuedomainBase方法的返回值,增加初始字段itemCount即可。
增加初始字段,就是在Entity中加入
@TableField(exist = false)
private Long itemCount;