【问题标题】:Update Vue Js table after one row was added添加一行后更新Vue Js表
【发布时间】:2019-11-29 08:02:47
【问题描述】:

我尝试在添加一个新行后更新表。现在可以添加新行,但我必须刷新页面才能看到新行。同样的问题我有更新/删除方法,但我会在之后找到方法。

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();
       }
     }           
});

【问题讨论】:

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


    【解决方案1】:

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

    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"
    >
    

    【讨论】:

    • 我得到了和以前一样的错误。我正在使用分页,并且在无法读取未定义的属性“currentPage”时出现错误。我将进行更多调试。
    【解决方案2】:

    我找到了一种从表中重新渲染数据的方法。

    v-if="renderComponent" 添加到

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

    创建一个新方法:

    forceRerender() {
        this.renderComponent = false;
        this.$nextTick(() => {
            this.renderComponent = true;
        });
    }
    

    我在活动结束后使用 forceRerender();,它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2013-11-19
      • 2017-09-09
      • 2018-12-15
      • 2019-04-02
      • 2018-02-08
      • 1970-01-01
      相关资源
      最近更新 更多