vue数据更新table内容不更新解决方法

场景:

table组件绑定的数据变化时,页面没有重新渲染,常见于子组件中使用table组件

原理:

创建实例时 数组在vue中没有被监听到,属于非响应式数据,数组的下标变化无法监听到

解决方式:

    <el-table
          :key="tamptime"
          stripe
          border
          fit
          height="450"
          :data="tabledata"
          style="width: 100%"
        >
          <el-table-column
            v-for="i in pretabledatacolumn"
            :key="i.index"
            show-overflow-tooltip
            :prop="i"
            :label="i"
            width="230"
          />
        </el-table>

data(){
    return {
        tabledata:[],
        pretabledatacolumn:[],
        tamptime:new Date().valueOf()
    }
}
methods:{
	changeTableData(){
		this.tabledata = []
	}
}

1、利用vue中重写的数组方法
splice,split,concat…

changeTableData(){
	this.tabledata.splice(1,0)
}

2、为table绑定一个key,数据变化时更改key值,或者使用v-if绑定一个不重复的值触发组件渲染

changeTableData(){
		this.tamptime = new Date().valueOf()
		this.tabledata = newtabledata
	}

3、使用$set()

changeTableData(){
	this.$set(tabledata,1,'newvalue')
	}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 Vue 中使用表格组件并且更新数据但没有渲染出来,可能是因为你没有使用正确的方式来更新数据。 一种解决方法是使用 Vue 的响应式数据,确保每次更新数据时都能够自动重新渲染。 另一种解决方法是使用 Vue 的强制更新功能,可以手动触发组件的重新渲染。 以下是使用响应式数据更新表格的示例代码: ```html <template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(person, index) in people" :key="index"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> </tr> </tbody> </table> <button @click="updateData">Update Data</button> </div> </template> <script> export default { data() { return { people: [ { name: "John", age: 30 }, { name: "Jane", age: 25 }, { name: "Bob", age: 40 }, ], }; }, methods: { updateData() { // 修改数据并触发重新渲染 this.people[0].age = 31; this.people[1].age = 26; this.people[2].age = 41; }, }, }; </script> ``` 如果你使用了类似 `this.people[0].age = 31` 这样直接修改数据的方式,那么需要使用 `this.$forceUpdate()` 手动触发重新渲染。 ```html <template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(person, index) in people" :key="index"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> </tr> </tbody> </table> <button @click="updateData">Update Data</button> </div> </template> <script> export default { data() { return { people: [ { name: "John", age: 30 }, { name: "Jane", age: 25 }, { name: "Bob", age: 40 }, ], }; }, methods: { updateData() { // 修改数据 this.people[0].age = 31; this.people[1].age = 26; this.people[2].age = 41; // 手动触发重新渲染 this.$forceUpdate(); }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值