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);
        }
    }
}
4
  • it would be useful if you included your JSON. Does it conform to the spec here: github.com/matfish2/vue-tables-2#server-side Commented Nov 29, 2017 at 2: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 Commented Nov 29, 2017 at 3:05
  • You could try running this.$forceUpdate() after running the UPDATE statement Commented Nov 29, 2017 at 6:09
  • There's a refresh() method but I do not know how to use "ref" yet which is required by the documentation (github.com/matfish2/vue-tables-2#methods). I'm unsure if this refreshes the data too. Commented Nov 29, 2017 at 8:39

3 Answers 3

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.