R
R
roovwhite2018-07-09 23:41:32
Vue.js
roovwhite, 2018-07-09 23:41:32

How to write code to dynamically change the data in the table?

The data comes from the server in the form of an array of objects from which the table is formed. When the first data
arrives, the table is formed correctly. But the code does not react to subsequent data (the template is not redrawn). Can you help me figure out how to code correctly?

<table id="table-box">
    <tr v-for="item in items">
    <td class="column">{{item.date}}</td>
    <td class="column">{{item.regs}}</td>
    <td class="column">{{item.first_deposits}}</td>
    <td class="column">{{item.deposits}}</td>
    </tr>
</table>

$.ajax({
        type: 'post',
        url: '/api/v1/user',
        data: formData,
        success: function (data) {
            new Vue({
                   el:'#table-box',
                  data:{
                       items: data
                  }
            });
        },
        error: function (xhr, status, error) {
            
        }
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-07-10
@roovwhite

Initialize the Vue instance once, when the page opens:

const vm = new Vue({
  el: '#table-box',
  data: () => ({
    items: [],
  }),
});

And in the ajax request callback, just update the data:
success(data) {
  vm.items = data;
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question