<el-table-column prop="content_zh" label="名称" min-width="280">
<template v-slot:default="scope">
<div
v-loading="scope.row.content_zh == null || scope.row.content_zh == ''"
element-loading-background="rgba(0, 0, 0, 0)"
></div>
<p>
{{ scope.row.content_zh }}
</p>
</template>
</el-table-column>
当使用el-table时,如果有一列数据需要异步请求,并在等待该列数据的时候展示加载效果。可以通过scope.row.content_zh == null || scope.row.content_zh == ''来判断数据是否为空,如果为空此时v-loading=true。当拿到返回值后可以为scope.row.content_zh赋值,此时v-loading自动变为flase,取消加载效果。
Transform() {
this.indexarr = []
for (var i = 0; i < 5; i++) {//i小于的是每页数据的数量
if (
this.tableData.slice((this.currentPage - 1) * this.pagesize, this.currentPage * this.pagesize)[i].content_zh == null ||
this.tableData.slice((this.currentPage - 1) * this.pagesize, this.currentPage * this.pagesize)[i].content_zh == ''
) {
this.indexarr.push(i)
}
}
for (const value of this.indexarr) {
axios
.get('https://mock.apifox.cn/m1/1320300-0-default/translate')
.then((response) => {
this.tableData.slice((this.currentPage - 1) * this.pagesize, this.currentPage * this.pagesize)[value].content_zh =
response.data.data[0]
})
.catch((err) => {
console.log(err)
this.$message('数据正在处理中, 敬请期待')
})
}
},
通过第一个for循环,寻找出当前表格有哪些行缺少数据,然后通过请求返回来的数据来给缺少的行赋值。可以在页面刚进入和翻页时调用此方法。