zenprogrammer
zenprogrammer

Reputation: 711

data change doesn't reflect in vue application

In my Vue application I am trying to create a simple table with changeable column sequence. In the data structure I keep all column data with its thead. I use compute to calculate rows from column data using matrix transposion. However any change I do to dynamicTable.columns doesn't reflect in the view.

function transpose(a)
{
  return a[0].map(function (_, c) { return a.map(function (r) { return r[c]; }); });
  // or in more modern dialect
  // return a[0].map((_, c) => a.map(r => r[c]));
}

var tData = {
    columns:[
    {thead:'isbn',
    tdata:[1,2]},
    {thead:'name',
    tdata:['rose','flower']},
    {thead:'price',
    tdata:['10','15']},
    {thead:'author',
    tdata:['john','jane']},
    {thead:'page count',
    tdata:['396','149']},
    {thead:'print date',
    tdata:['2001', '1996']}
    ]
}

var dynamicTable = new Vue({
  el: '#dynamicTable',
  data: tData,
    computed:{
        rows: function(){
            arr = [];
            this.columns.forEach(function(element){
                arr.push(element.tdata);
            });
            return transpose(arr)
        }
    }
})

For example I want to change the order of isbn column with price,

a=dynamicTable.columns[0]
b=dynamicTable.columns[2]
dynamicTable.columns[0]=b
dynamicTable.columns[1]=a

data changes but changes are not reflected. What is the problem here?

Upvotes: 1

Views: 591

Answers (1)

Aamir Khan
Aamir Khan

Reputation: 3031

As mentioned in the documentation, this is a JavaScript limitation. Direct changes to an array are not detected by Vue.

One workaround for this, is to use Vue.set(), as instructed in the link.

Upvotes: 1

Related Questions