添加一行后更新Vue Js表

时间:2019-11-29 08:02:47

标签: vue.js bootstrap-vue vue-reactivity

我尝试在添加新行之后更新表。现在可以添加新行了,但是我必须刷新页面才能看到新行。相同的问题我有update / delete方法,但是之后我会找到方法的。

getCustomers() {
    let url = `/customers`;
    const p = axios
        .get(url);
    return p.then(response => {
        const i = response.data.data;
        return i || [];
    });
},

addCustomer() {
    let newCustomer = {
        customer_name: this.customer_name
    };
    axios
        .post("/customers", newCustomer)
        .then(response => {
            this.items = response.data;
            this.$nextTick(() => {
                this.$refs.modal.hide();
                this.items.customer_name = response.data.customer_name;
        });
    })
    .catch(e => {
        console.log(e);
    });
}

data() {
    return {
      items: [],
      ....
      ....
    }
}

<b-table
    show-empty
    stacked="md"
    :items="getCustomers"
    :fields="fields"
    :current-page="currentPage"
    :per-page="perPage"
    :filter="filter"
>

这适用于更新

this.$nextTick(() => {
    for(let item of this.items){
       if(parseInt(item.id) === parseInt(id)) {
          item.customer_name = response.data.customer_name;
          this.resetModal();
       }
     }           
});

2 个答案:

答案 0 :(得分:0)

请在下面的代码中查看我的评论:

getCustomers() {
    let url = `/customers`;
    const p = axios
        .get(url);
    this.customers = p.then(response => {
        const i = response.data.data;
        return i || [];
    });
},

addCustomer() {
    let newCustomer = {
        customer_name: this.customer_name
    };
    axios
        .post("/customers", newCustomer)
        .then(response => {
            this.items = response.data;
            this.$nextTick(() => {
                this.$refs.modal.hide();
                this.items.customer_name = response.data.customer_name;
                this.customers.push(response.data) // something like this can help
        });
    })
    .catch(e => {
        console.log(e);
    });
}

data() {
    return {
      items: [],
      customers: [] // add this to make reactive
    }
} 
mounted() {
  this.getCustomers(); // run the function when page/component mounted
}

<b-table
    show-empty
    stacked="md"
    :items="customers"
    :fields="fields"
    :current-page="currentPage"
    :per-page="perPage"
    :filter="filter"
>

答案 1 :(得分:0)

我找到了一种从表重​​新呈现数据的方法。

v-if =“ renderComponent” 添加到

  @Query("SELECT name,email FROM User where name in(select distinct name from User)")
    List<UserNew> fetchDistinctUser();

创建一个新方法:

data() {
  return {
    renderComponent: true,
  };
},

我在活动结束后使用 forceRerender(); ,它有效。

相关问题