Lalit Sharma
Lalit Sharma

Reputation: 565

How update nested object array value directly by v-model in VUE?

I need to update my value which is in JSON thru v-model

{ class: "data.child",
    "myform.input1": [true, "<input1 value>"]
}

<input type="text" v-model="<what to put here?>" > //so that directly value can be update in my vue data property JSON mentioned above

Upvotes: 0

Views: 693

Answers (1)

Mosd
Mosd

Reputation: 1682

Cant do it directly with v-model, unless you want to change your input type to maybe multi select. If you really want the exact output, can listen onchange event like below. Or can just use v-model and enter your data as you want...but will need to convert to array.

const jsonData = { class: "data.child",
    "myform.input1": [true, "<input1 value>"],
    "myform.input2": [true, "<input1 value>"]
}


const App = {
template: `<div>
<input type="text" v-model="data['myform.input2']"/>
<input type="text" @change="update"/>
<p>{{JSON.stringify(data, null, 2)}}</p>
</div>`,
methods: {
update: function(event) {
this.data['myform.input1'] = [true, event.target.value];
}
}
,
data(){
return {data: jsonData}
}
}

new Vue({
render: h => h(App),
}).$mount("#app");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
</div>

Upvotes: 1

Related Questions