I have a Vue component, that creates a number of inputs based on the data fetched by the API. I run the values from the JSON through a method, that in turn converts this value to an input field if a certain condition matches. Here's how my component looks:
<template>
<div>
<div v-for="(val, index) in data">
<span>{{val.key}}</span>
<span :inner-html.prop="checkForEdit(val)"></span>
</div>
<b-button @click="submitData">Save</b-button>
<div>
</template>
export default {
name: "SomeComp",
data() {
return {
dynamicVars: {}
}
},
methods: {
...mapActions("api", ["getData"]),
checkForEdit(value) {
if(!value) return '';
if(value.mustEdit) {
this.dynamicVars[value.key] = '';
return '<input type="text" value= "'+ value.text +'" :model='+ this.dynamicVars[value.key]+'>';
} else {
return value.text;
}
},
submitData() {
console.log(this.dynamicVars); //Only the key is present, no value updated
}
},
created() {
this.getData();
},
computed: {
...mapState("api", ["data"]),
}
};
Here's how the data looks:
[
{key: 'name', text: 'John', mustEdit: false},
{key: 'age', text: '100', mustEdit: true}
]
This data can be anything, the fields are not fixed, only the format is. So I want to create dynamic vars object on the fly to send to the API that saves it.
Right now it only creates the variable inside dynamicVars however it doesn't seem that it actually reacts when I change the field value.