vue.js - VueJs adding new key to object reference issue -


i have issue when adding new key object , dynamically modifying fields associated.

for example add new column, set column name "url", attempt update value of url row 1. in example value not update though field has v-model="row[field.name]. there should make sure row[field.name] changed when field.name changes

code: https://codepen.io/ruttyj/pen/zdgbpb

 <table>         <thead>             <tr>                   <template v-for="(field, f) in fields">                       <th>                           <flex-row>                               <flex-column style="width: 100%;">                                   <flex>                                           <input type="text" :value="field.name" @input="updatefield(f, 'name', $event.target.value)" style="width:100%">                                   </flex>                                   <flex style="width: 100%;">                                       <select :value="field.type"  @change="updatefield(f, 'type', $event.target.value)"                                         style="width:100%">                                         <option v-for="fieldtype in fieldtypeopts"                                            :value="fieldtype.value"                                            :selected="fieldtype.value == field.type">{{fieldtype.label}}</option>                                       </select>                                   </flex>                               </flex-column>                               <flex>                                   <button @click="removefield(f)"                                            style="height:100%;">x</button>                               </flex>                           </flex-row>                       </th>                   </template>                   <td>                       <button @click="newfield()">+</button>                   </td>             </tr>         </thead>         <tbody>             <tr v-for="(row, r) in rows">                 <td v-for="field in fields">                     <template>                         <template v-if="'checkbox' == field.type">                             <input type="checkbox"                                                        style="float:right;"                                             v-model="row[field.name]"                                  >                              </template>                          <input type="number"                                   v-else-if="'number' == field.type"                                     style="width:100%"                                                                :value="row[field.name]"                                               @input="updaterow(r, field.name, $event.target.value)">                          <input type="text" style="width:100%"                                     v-else                                         v-model="row[field.name]">                           {{field.name}}                           <pre>{{field}}</pre>                          <pre>{{row}}</pre>                      </template>                 </td>                 <td><button @click="removerow(r)">x</button></td>             </tr>         </tbody>         <tfoot>             <tr>                   <td v-for="(field, i) in fields">                    </td>                   <td>                       <button @click="newrow()">+</button>                   </td>             </tr>         </tfoot>   </table> 

fyi tried both v-model , :value/@input

you breaking reactivity. break reactivity if modify property not declared when mount component. you're trying directly modify objects in array added later , vue cannot track that. because of this, require programmers on our team use this.$set whenever modify property on object.

you can replace:

 this.rows[rowid][fieldname] = value; 

with

 this.$set(this.rows[rowid], fieldname, value) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -