Vuetify数据表未获取更新的数组值

问题描述 投票:0回答:2

我用一些用户数据填充了Vuetify数据表。当我从数据表中删除用户时,我会像这样更新用户数组:

handleDelete(user) {
  confirm("Are you sure you want to delete this user?") &&
    axios
      .delete("user/" + user.id)
      .then(response => {
        // Delete user from user array
        this.users = this.users.filter(function(el) {
          return el.id != user.id;
        });
      })
},

当我注册新用户时,数组也会更新,但现在是这样的:

handleRegister(user) {
  axios
    .post("user/register", user)
    .then(response => {
      // Update User array
      this.users.push(response.data.user);
    })
},

这一切都很好,除了我更新用户时。在我的函数中,我在用户数组中搜索该用户对象,并将其替换为更新的对象。但是不知何故,数据表不会使用新值进行更新。更新功能如下所示:

   handleUpdate(user) {
      const id = user.id;
      axios
        .put("user/" + id, user)
        .then(response => {
          // TODO: Update User array
          let foundIndex = this.users.findIndex(
            x => x.id == response.data.user.id
          );
          this.users[foundIndex] = response.data.user;
        })
    },

[当我console.log时,this.users[foundIndex]response.data.user的值显示正确的值。但是以某种方式看来数据表没有更新。

javascript arrays vuetify.js
2个回答
0
投票
// it should be like this below handleDelete(user) { const _this_0 = this; confirm("Are you sure you want to delete this user?") && axios .delete("user/" + user.id) .then(response => { // Delete user from user array _this_0.users = _this_0.users.filter(function(el) { return el.id != user.id; }); }) }

handleRegister(user) { const _this_0 = this; axios .post("user/register", user) .then(response => { // Update User array _this_0.users.push(response.data.user); }) },

handleUpdate(user) {
      const id = user.id;
      const _this_0 = this;
      axios
        .put("user/" + id, user)
        .then(response => {
          // TODO: Update User array
          let foundIndex = _this_0.users.findIndex(
            x => x.id == response.data.user.id
          );
          _this_0.users[foundIndex] = response.data.user;
        })
    }

0
投票
updateUserArray(user) { // Look for the index of the user let index = this.users.findIndex( x => x.id == user.id ); // Remove the user from the array this.users= this.users.filter(function(el) { return el.id != user.id }); // Add the updated value to the array this.users.splice(index, 0, user); },

似乎我必须使用splice函数,因为这会强制DOM刷新,而直接更新数组则不需要。

© www.soinside.com 2019 - 2024. All rights reserved.