【问题标题】:Refresh Boostrap-Vue table after deleting a row删除一行后刷新 Bootstrap-Vue 表
【发布时间】:2019-04-02 05:15:27
【问题描述】:

我正在为我的数据表使用 Bootstrap-Vue,并在我的仪表板中获得了下表:

我可以通过单击垃圾桶图标成功删除项目。它使用 Axios 发送 AJAX 请求。但是,删除后它仍然显示该项目,直到我手动刷新网页。我该如何解决这个问题?我不想发出另一个 AJAX 请求来加载更新版本,我认为解决它的最佳方法是从数据表中删除已删除的项目行。

我尝试给我的表格一个 ref 标签并使用this.$refs.table.refresh(); 调用刷新函数,但没有成功。

我的代码:

<template>
<div>
    <b-modal ref="myModalRef" hide-footer title="Delete product">
        <div class="container">
            <div class="row">
                <p>Are you sure you want to delete this item?</p>
                <div class="col-md-6 pl-0">
                    <a href="#" v-on:click="deleteItem(selectedID)" class="btn btn-secondary btn-sm btn-block">Confirm</a>
                </div>
                <div class="col-md-6 pr-0">
                    <a href="#" v-on:click="$refs.myModalRef.hide()" class="btn btn-tertiary btn-sm btn-block">Cancel</a>
                </div>
            </div>
        </div>
    </b-modal>
    <div id="main-wrapper" class="container">
        <div class="row">
            <div class="col-md-12">
                <h4>Mijn producten</h4>
                <p>Hier vind zich een overzicht van uw producten plaats.</p>
            </div>

            <div class="col-md-6 col-sm-6 col-12 mt-3 text-left">
                <router-link class="btn btn-primary btn-sm" :to="{ name: 'create-product'}">Create new product</router-link>
            </div>
            <div class="col-md-6 col-sm-6 col-12 mt-3 text-right">
                <b-form-input v-model="filter" class="table-search" placeholder="Type to Search" />
            </div>
            <div class="col-md-12">
                <hr>
                <b-table ref="table" show-empty striped hover responsive :items="posts" :fields="fields" :filter="filter" :current-page="currentPage" :per-page="perPage">
                    <template slot="title" slot-scope="data">
                        {{ data.item.title|truncate(30) }}
                    </template>
                    <template slot="description" slot-scope="data">
                        {{ data.item.description|truncate(50) }}
                    </template>
                    <template slot="public" slot-scope="data">
                        <i v-if="data.item.public === 0" title="Unpublished" class="fa fa-circle false" aria-hidden="true"></i>
                        <i v-else title="Published" class="fa fa-circle true" aria-hidden="true"></i>
                    </template>
                    <template slot="date" slot-scope="data">
                        {{ data.item.updated_at }}
                    </template>
                    <template slot="actions" slot-scope="data">
                        <a class="icon" href="#"><i class="fas fa-eye"></i></a>
                        <a v-on:click="editItem(data.item.id)" class="icon" href="#"><i class="fas fa-pencil-alt"></i></a>
                        <a href="#" v-on:click="getID(data.item.id)" class="icon"><i class="fas fa-trash"></i></a>
                    </template>
                </b-table>
                <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0 pagination-sm" />
            </div>

        </div><!-- Row -->

    </div><!-- Main Wrapper -->
</div>

<script>
    export default {

        data() {
            return {
                posts: [],
                filter: null,
                currentPage: 1,
                perPage: 10,
                totalRows: null,
                selectedID: null,
                fields: [
                    {
                        key: 'title',
                        sortable: true
                    },
                    {
                        key: 'description',
                    },
                    {
                        key: 'public',
                        sortable: true,

                    },
                    {
                        key: 'date',
                        label: 'Last updated',
                        sortable: true,
                    },
                    {
                        key: 'actions',
                    }

                ],
            }
        },
        mounted() {
            this.getResults();
        },
        methods: {
            // Our method to GET results from a Laravel endpoint
            getResults() {
                axios.get('/api/products')

                    .then(response => {
                        this.posts = response.data;
                        this.totalRows = response.data.length;
                    });
            },
            getID: function(id){
                this.selectedID = id;
                this.$refs.myModalRef.show();
            },
            deleteItem: function (id) {
                axios.delete('/api/products/' + id)
                    .then(response => {
                        this.$refs.myModalRef.hide();
                        this.$refs.table.refresh();

                    });

            },
            editItem: function (id){
                this.$router.push({ path: 'products/' + id });
            }
        },

    }
</script>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component bootstrap-vue


    【解决方案1】:

    deleteItem 方法应该是这样的:

            deleteItem(id) {
                axios.delete('/api/products/' + id)
                    .then(response => {
                       const index = this.posts.findIndex(post => post.id === id) // find the post index 
                       if (~index) // if the post exists in array
                         this.posts.splice(index, 1) //delete the post
                    });
    
            },
    

    所以基本上你不需要任何刷新。如果您删除 posts 数组的项目,Vue 将自动为您处理此问题,并且您的表格将被“刷新”

    【讨论】:

    • 对不起,是我,我不小心投了反对票,我想点击 +1,因为你提供了一个很好的替代解决方案
    • 好吧,不用担心。我的回答对你有帮助吗?
    • 我不是提问者,我已经使用this.posts= this.posts.filter(post=&gt;post.id!=id)给出了解决方案
    • 抱歉。我的错。您的解决方案很棒,但拼接速度更快。 filter 将遍历数组的所有项。但是,您的答案需要投票,因为它是正确的。我现在就去做。
    • 谢谢,这成功了!请问您为什么使用 const ?
    【解决方案2】:

    在成功删除后尝试使用给定的 id 删除该帖子:

         axios.delete('/api/products/' + id)
                    .then(response => {
                     this.posts= this.posts.filter(post=>post.id!=id)
    
                    });
        
    

    【讨论】:

    • 谢谢,这很有帮助。我有一个类似的用例,在删除记录后,我不确定是否应该调用该方法再次获取记录并重建表的数组或删除记录并使用您的方法过滤掉这些记录,而不是调用后端资源的次数
    • 删除操作成功,无需调用后端资源
    【解决方案3】:
     axios.delete('/api/products/' + id)
                    .then(response => {
                    this.getResults();
    
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-28
      • 2012-08-03
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      相关资源
      最近更新 更多