I am learning vue.js. I am trying to build a form dynamically from data. The form is condition based. If I select some field from the select field the specific content related to that option has to show.
Here is the data
If the model of "id: 2" object is "germany", the show from "id: 3" has to become true and the show from 'id:4" and "id: 5" has to become false.
Similarly, If the model of "id: 2" object is "us", the show from the id: 4 has to become true and the show from "id:3" and "id: 5" has to become false.
data(){
return{
fields: [
{
id: 1,
type: "text",
placeholder: "Name",
model: "",
show: true
},
{
id: 2,
type: "select",
placeholder: "Select",
model: "",
show: true,
options: [
{value: "germany", text: "Gemany", id: "germany"},
{value: "us", text: "United States", id: "us"},
{value: "uk", text: "United Kingdom", id: "uk"}
],
events: "onChange"
},
{
id: 3,
type: "paragraph",
text: "Berlin",
show: false
},
{
id: 4,
type: "paragraph",
text: "New York",
show: false
},
{
id: 5,
type: "paragraph",
text: "London",
show: false
}
]
}
}
Here is template
<template>
<div>
<div class="" v-for="field in fields" :key="field.id">
<input :type="field.type" :placeholder="field.placeholder" v-model="field.model" :id="field.id" v-if="field.type == 'text' && field.show">
<select v-model="field.model" :id="field.id" v-if="field.type == 'select' && field.show" @change="onChange(field.id)">
<option v-for="option in field.options" :key="option.id" :value="option.value" :id="option.id">{{option.text}}</option>
</select>
<p v-if="field.type == 'paragraph' && field.show" :id="field.id">{{field.text}}</p>
</div>
<button @click="submit">Submit</button>
</div>
</template>
methods
onChange(e){
console.log(e)
}
How can I achieve this? Thank you.
fieldsis not JSON. It it is just a regular object. JSON is a string.