I need help in dynamically setting the input v-model. Based on the dropdown selection. I will get data from the backend. Right now, it's a dummy data.
example, there will be 3 input boxes in each row. numerator, denominator and the computed value. For each input box, I have passed v-model like this form['num_' + idx], form['den_' + idx], form['comp_' + idx]. I have created a form object in data(state).
if I bind the computed value input box (:value) with computed property, then I am not able to pass arguments. I need the current numerator, denominator value. it gives me an error computedValue is not a function.
Right now, I have tried putting this computedValue function in the methods section and also added a button next to the computed each input box. What I really need is, when the value in the numerator and denominator changes. it automatically calculates the value and show it in the third computed input box.
The value in the computed input box is not showing. Sometimes it shows value if I change row data in other rows.
<template>
<div>
<select v-model="service">
<option disabled value="">Please select one</option>
<option>Order to cash</option>
</select>
<select @change="changeAccountEvent($event)" >
<option disabled value="">Please select one</option>
<option>Nissan</option>
<option>Ford</option>
</select>
<div>
<ul>
<li v-for="(d,idx) in data" :key="d.metric">
<div class="flex px-4 py-2">
<div class="w-1/4">{{d.metric}}</div>
<div class="w-1/4 mr-2">
<input v-model="form['num_' + idx]" type="number">
</div>
<div class="w-1/4 mr-2">
<input v-model="form['den_' + idx]" type="number">
</div>
<input v-model="form['comp_' + idx]" type="number" >
<button
@click="computedValue(form['num_' + idx], form['den_' + idx], idx, d.formula)">get value
</button>
<!-- :value="computedValue(form['num_' + idx], form['den_' + idx]) -->
</div>
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data() {
return {
service: '',
account: '',
data: null,
form: {
},
};
},
methods: {
computedValue(a, b, c, d) {
console.log('a -> ', a, 'b -> ', b, 'c -> ', c, 'd -> ', d);
this.form[`comp_${c}`] = parseFloat(a) / parseFloat(b);
console.log(this.form);
},
changeAccountEvent(event) {
if (this.service !== '') {
this.account = event.target.value;
if (this.account === 'Ford') {
const fordData = [
{ metric: 'Days Sales Outstanding', formula: '/' },
{ metric: 'Past due percent', formula: '/' },
{ metric: 'Days', formula: '/' },
{ metric: 'percent', formula: '/' },
];
this.data = fordData;
}
if (this.account === 'Nissan') {
const nisData = [
{ metric: 'Days Sales Outstanding', formula: '/' },
{ metric: 'Past due percent', formula: '/' },
];
this.data = nisData;
}
} else {
// event.target.value = '';
alert('please select service line');
}
},
},
};
</script>
Please guide me.
Cheers Meet