gpt4 book ai didi

javascript - 如何更新 vueJs 数组列表的特定行?

转载 作者:搜寻专家 更新时间:2023-10-30 22:31:02 25 4
gpt4 key购买 nike

是否有一种正确的方法可以在不重新加载所有数据的情况下刷新 vueJS 数组列表中的特定行?

在本例中,它是一个电话列表。

<template>
<table class="table">
<tr v-for="phone in phones">
<td @click="edit(phone.id)">
{{phone.number}}
</td>
<td class="pointerCursor" @click="edit(phone.id)">
{{phone.comment | truncate(90)}}
</td>
</tr>
</table>
</template>

<script>
export default {
data () {
return {
phones = [
{id:1, number: '001 656 88 44', comment:''},
{id:2, number: '610-910-3575 x2750', comment:'Quod odit quia'},
{id:3, number: '536.224.2639 x714', comment:'primary phone'},
{id:5, number: '0034 556 65 77', comment:'home phone phone'},
]
}
},

methods: {
edit(id) {
// update using an api that returns the updated data.
var updatedPhone = update(id)

// how to update with reloading all the phone list?
// ...
}
}
}
</script>

PS:这是一段演示问题的代码。

最佳答案

const currentIndex = this.phones.findIndex(p => p.id === id);
this.phones.splice(currentIndex, 1, updatedPhone)

如果您的目标环境不支持数组上的 findIndex,您可以使用其他一些方法来查找当前索引。一种方法是传递电话而不是它的 ID。

edit(phone) {
const currentIndex = this.phones.indexOf(phone);

// update using an api that returns the updated data.
var updatedPhone = update(phone.id)

this.phones.splice(currentIndex, 1, updatedPhone)
}

在这两种情况下,Vue 都会检测到更改并为您重新呈现。

关于javascript - 如何更新 vueJs 数组列表的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com