简体   繁体   English

删除一行后刷新Boostrap-Vue表

[英]Refresh Boostrap-Vue table after deleting a row

I'm using Bootstrap-Vue for my datatables and got the following table within my dashboard:我将 Bootstrap-Vue 用于我的数据表,并在我的仪表板中得到了下表:

在此处输入图片说明

I can succesfully delete items by clicking on the trash icon.我可以通过单击垃圾桶图标成功删除项目。 It sends an AJAX request using Axios.它使用 Axios 发送 AJAX 请求。 However, after deletion it still displays the item until I manually refresh the web page.但是,删除后它仍然显示该项目,直到我手动刷新网页。 How do I solve this?我该如何解决这个问题? I don't want to make another AJAX request to load in the updated version, I think the best way to solve it is just remove the deleted item row from the datatable.我不想发出另一个 AJAX 请求来加载更新的版本,我认为解决它的最好方法就是从数据表中删除已删除的项目行。

I tried giving my table a ref tag and call a refresh function using this.$refs.table.refresh();我尝试给我的表一个 ref 标签并使用this.$refs.table.refresh();调用一个刷新函数this.$refs.table.refresh(); but with no success.但没有成功。

My code:我的代码:

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

The deleteItem method should be like this: 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
                });

        },

So basically you don't need any refresh.所以基本上你不需要任何刷新。 If you remove the item for posts array Vue will automatically handle this for you and your table will be "refreshed"如果您删除posts数组的项目,Vue 将自动为您处理,您的表格将被“刷新”

try to remove that post with the given id after the successful delete :尝试在成功删除后删除具有给定ID 的帖子:

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

                });
    
 axios.delete('/api/products/' + id)
                .then(response => {
                this.getResults();

                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM