generate dynamic DATA variables with VUE

1

I am trying to define dynamic variables for VUE , to then use them in v-for . when loading the page they work, but if later they change their values, they do not change the values in the view.

VUE

const app = new Vue({
        el: '#mainCreate',
        data: {
            modules : [],
            feature: []
        },
    });

This function I am using to generate the variables within feature :

 /*crea dinamicamente los feautres en module*/
    function creatDataFeatures (){
        $.each(app.modules, function(index,module){
            app.feature[index] = module.features;
        });
    }

This is how I am carrying it in view:

<!-- caracteristicas-->
<table class="table table-striped mt-3">
   <tbody>
      <tr v-for="feat in feature[index]">
           <td>
              <input type="checkbox" checked>
          </td>
           <td>
              @{{feat.title}}
           </td>
           <td class="text-info">
              @{{feat.price ? '$ '+feat.price : '$ 0'}}
           </td>
        </tr>
     </tbody>
   </table>

When loading the page, I load the data correctly, but if I then change the values ejm: feature[index] = [other array] (I change it for the same data format as the start ones) in the view, nothing changes. a solution or an alternative would be great.

    
asked by Michael 13.03.2018 в 00:38
source

1 answer

2

Solved

The line app.feature[index] = module.features; falls in the limitation of Vue with respect to the matrices.

Fixed using VUE.SET see: link

    $.each(app.modules, function(index,module){
        Vue.set(app.feature, index, module.features); // changed this line
    });
  

Due to limitations in JavaScript, Vue can not detect the following changes in an array:

     

1. When you directly establish an element with the index, for example vm.items[indexOfItem] = newValue

     

2. When the size of the array is modified, ejm. vm.items.length = newLength

     

To overcome warning 1, both of the following will do the same as vm.items[indexOfItem] = newValue , but will also activate status updates in the reactivity system:

// Vue.set
Vue.set(vm.items, indexOfItem, newValue)
    
answered by 13.03.2018 в 14:59