gpt4 book ai didi

javascript - 拼接数组不重新渲染表格行Vuejs

转载 作者:行者123 更新时间:2023-11-29 23:17:58 26 4
gpt4 key购买 nike

我有一个用来自 ajax 请求的数据生成的用户表。表格大致如下所示:[表格图片][1]

当管理员编辑用户的用户名时,我希望用户的行重新呈现更改(特别是用户的名字和姓氏)。

我创建的表很好。这些模型工作正常。我的父组件在我的 edit() 方法中接收到编辑后的数据,但我似乎无法使用更改后的数据重新呈现我的目标行。如何更新目标行?

我尝试了以下方法,但没有用:

这是我的父 vue 组件,具有以下内容(我删除了不相关的细节):

模板:

<table>
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Created</th>
<th>Stat</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="(user, index) in listOfUsers" :key="'row' + user._id">
<td>{{user.first_name + ' ' + user.last_name}}</td>
<td>{{user.email}}</td>
<td>{{user.created}}</td>
<td>
<a v-if="user.confirmed" @click="determineButtonClicked(index, 'confirm')"></a>
<a v-else @click="determineButtonClicked(index, 'unconfirm')"></a>
</td>
<td class="buttonCase">
<a @click="determineButtonClicked(index, 'info')"></a>

<a v-if="user.blocked" @click="determineButtonClicked(index, 'blocked')"></a>
<a v-else @click="determineButtonClicked(index, 'block')"></a>

<a v-if="user.enforce_info === 'required'" @click="determineButtonClicked(index, 'enforceInfoActive')"></a>
<a v-else-if="user.enforce_info === 'done'" @click="determineButtonClicked(index, 'enforceInfoChecked')"></a>
<a v-else @click="determineButtonClicked(index, 'enforceInfo')"></a>

<modal v-if="usersList[index]" @toggleClickedState="setState(index)" @editUser="edit(index, $event)" :id="user._id" :action="action"></modal>
</td>
</tr>
</tbody>
</table>

脚本

<script>
export default {
created: function() {
let self = this;
$.getJSON("/ListOfUsers",
function(data){
self.listOfUsers = data;
});
},
data: function() {
return {
listOfUsers: [],
}
},
methods: {
edit(index, update){
let user = this.listOfUsers[index];
user.firstName = update.firstName;
user.lastName = update.lastName;

// this.listOfUsers.splice(index, 1, user)
this.listOfUsers.$set(index, user)
}
}
}
</script>

感谢您的宝贵时间和帮助![1]: /image/lYQ2A.png

最佳答案

Vue 没有在您的编辑方法中更新,因为对象本身没有被替换。对象的属性确实会改变,但 Vue 只是在寻找要改变的对象引用。

要强制数组检测实际对象引用的变化,您需要替换对象,而不是修改它。我不知道你到底想怎么做,但是这个 hacked together fiddle 应该可以证明这个问题,所以你可以解决它:http://jsfiddle.net/tga50ry7/5/

简而言之,如果您将编辑方法更新为如下所示,您应该会在模板中看到重新渲染:

methods: {
edit(index, update){
let currentUser = this.listOfUsers[index];
let newUser = {
first_name: update.firstName,
last_name: update.lastName,
email: currentUser.email,
created: currentUser.created
}

this.listOfUsers.splice(index, 1, newUser)
}
}

关于javascript - 拼接数组不重新渲染表格行Vuejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51975190/

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