Reload Data of vue-tables-2 (Vuex)

Kay Singian picture Kay Singian · Nov 29, 2017 · Viewed 17.1k times · Source

Module: https://github.com/matfish2/vue-tables-2

I'm creating a CRUD app. How can I reload the data fetched via Ajax call in vue-tables-2? I wanted to reload the table after an update statement is executed somewhere in my app.

Vue-tables is using vuex in my setup.

<v-server-table 
    name="UserAdmin" url="admin/master/?format=json" :columns="columns" :options="options">
</v-server-table>

EDIT: Added Javascript code of the table for data properties.

export default {
    data() {
        return {
            columns: ['ID','NAME', 'POSITION', 'APPLICATIONS','DESCRIPTION','STATUS','ENCODED_BY'],
            options: {
                responseAdapter: (resp) => {
                    resp = resp.map(item => ({ 
                        ID: item.ID,
                        FK_ID: item.FK_ID,
                        NAME: `${item.FIRSTNAME} ${item.LASTNAME}`, 
                        POSITION: item.POSITION,
                        APPLICATIONS: item.APPLICATIONS,
                        DESCRIPTION: item.DESCRIPTION,
                        STATUS: item.STATUS,
                        ENCODED_BY: item.ENCODED_BY,
                        TOTAL: item.TOTAL
                    }));
                    let count;
                    if(resp[0] != null) {
                        count = resp[0]['TOTAL']
                    }
                    else {
                        count = 0
                    }
                    return { 
                        data: resp,
                        count: count
                    }
                },
                headings: {
                    'ID': <span># &nbsp;</span>,
                    'NAME':'Name', 
                    'POSITION':'Position', 
                    'APPLICATIONS':'Applications', 
                    'DESCRIPTION':'Description', 
                    'STATUS': 'Status', 
                    'ENCODED_BY':'Encoded By', 
                    'edit': 'Options'
                },
                columnsClasses: {
                        ID: 'col-md-1',
                        NAME:'col-md-2 pointer',
                        POSITION: 'col-md-2',
                        APPLICATIONS: 'col-md-2',
                        DESCRIPTION: 'col-md-2',
                        STATUS: 'col-md-1',
                        ENCODED_BY: 'col-md-2',
                },
                templates: {
                    NAME: (h, row) => {
                        return <a on-click={ () => this.setUpdateID(row) }>{row.NAME}</a>
                },
                APPLICATIONS: (h,row) => {
                    return (<ul>{JSON.parse(row.APPLICATIONS).map((val)=>(<li>{val}</li>))}</ul>);
                },
                STATUS: (h, row) => {
                    if(row.STATUS == 1) {
                        return <span class="label label-success">Active</span>
                    }
                    else if(row.STATUS == 0) {
                        return <span class="label label-danger">Inactive</span>
                    }
                }
                },
            },
        }
    },
    methods: {
        setUpdateID: function(row) {
            this.$store.commit('SET_UPDATE_ID', row.FK_ID);
        }
    }
}

Answer

Matanya picture Matanya · Nov 29, 2017

As documented you should use the refresh method. You can read about refs here

<v-server-table ref="table"
    name="UserAdmin" url="admin/master/?format=json" :columns="columns" :options="options">
</v-server-table>

Javascript:

methods:{
   onUpdate() {
     this.$refs.table.refresh();
    }
}