5

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);
        }
    }
}
Kay Singian
  • 1,301
  • 8
  • 20
  • 33
  • it would be useful if you included your JSON. Does it conform to the spec here: https://github.com/matfish2/vue-tables-2#server-side – trk Nov 29 '17 at 02:59
  • @82Tuskers I added the code, please see above. What I'm doing for now is adding a v-if to the table, when I run an UPDATE statement, I then set the v-if to false then set it to true again after 250seconds. Don't know if there's a better way – Kay Singian Nov 29 '17 at 03:05
  • You could try running `this.$forceUpdate()` after running the UPDATE statement – Jpod Nov 29 '17 at 06:09
  • There's a refresh() method but I do not know how to use "ref" yet which is required by the documentation (https://github.com/matfish2/vue-tables-2#methods). I'm unsure if this refreshes the data too. – Kay Singian Nov 29 '17 at 08:39

3 Answers3

6

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();
    }
}
tony19
  • 125,647
  • 18
  • 229
  • 307
Matanya
  • 6,233
  • 9
  • 47
  • 80
  • I'm doing this and no refresh is being triggered. The grid works fine, console.log(grid.refresh) shows it exists and is in scope, but no refresh actually occurs – Daniel Oct 22 '19 at 20:30
0

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}`
    })
  }
hamid hasani
  • 531
  • 2
  • 5
  • 14
0

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>