Reload Data of vue-tables-2 (Vuex)

2019-05-11 00:31发布

问题:

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

回答1:

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


回答2:

for reloading the vuetable with updated data you have two way:

1.

select the vuetable component with $refs in your code and then calling refresh method , something like this: html file:

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

js file:

this.$refs.userAdminVueTable.refresh()

2. When you have your data you can call setData method for your vuetable.

  this.vuetable.setData(updatedData);

in below i write an example for you, you can get inspire from that:

     this.Service.GetAllRecords().then(result => {
      this.vuetable.setData(result);
    }).catch(e => {
      this.showAlert = true  
      this.message = ` Exception : ${e}`
    })
  }


回答3:

You don't need to refresh it, if you refresh than state would be clear. You can use bellow example

methods:{
   onUpdate(rowIndex) {
     this.$refs.table.data.splice(rowIndex, 1);
    }
}
<v-server-table ref="table"
    name="UserAdmin" url="admin/master/?format=json" :columns="columns" :options="options">
</v-server-table>