I have tried to use the same form for creating and for editing purpose. i have passed the id with props from one component to other and it works fine. using that id i have tried to fetch the data from vuex store state with the help of the getters. the getters returned the data correctly. But i can't populate to the form. i have tried the create, computed and mount properties but i does not work.
i have tried to populate the form using mount, create and computed.
i expect the output the form to be populate when i click the edit button but nothing to see in the form.
Template code
<template>
<b-form-group
label-cols="12"
label="Minimum Salary: "
>
<b-form-input
id="input-1"
v-model="record.min_salary"
type="number"
></b-form-input>
</b-form-group>
<b-form-group
label-cols="12"
label-cols-lg="3"
label-for="input-2"
label="Maximum Salary: "
label-align-sm="right"
label-align="left"
>
<b-form-input
id="input-2"
v-model="record.max_salary"
type="number"
></b-form-input>
</b-form-group>
<b-form-group
label-cols="12"
label-cols-lg="3"
label-for="input-2"
label="Tax Rate: "
label-align-sm="right"
label-align="left"
>
<b-form-input
id="input-2"
v-model="record.tax_rate"
type="number"
></b-form-input>
</b-form-group>
<b-form-group
label-cols="12"
label-cols-lg="3"
label-for="input-2"
label="Deductible: "
label-align-sm="right"
label-align="left"
>
<b-form-input
id="input-2"
v-model="record.deductible"
type="number"
></b-form-input>
</b-form-group>
</b-form>
</template>
Vue Script Code
<script>
import { mapGetters, mapActions } from "vuex";
export default {
props: ["id"],
data: () => ({
update: false,
record: {
min_salary: "",
max_salary: "",
tax_rate: "",
deductible: ""
}
}),
created() {
if (this.id != null) {
this.update = true;
this.fetchIncomeTaxRate(this.id);
this.createoredit = "EDIT";
}
},
mounted() {
if (this.id != null) {
this.record.min_salary = this.getIncomeTaxRate.min_salary;
this.record.max_salary = this.getIncomeTaxRate.max_salary;
this.record.tax_rate = this.getIncomeTaxRate.tax_rate;
this.record.deductible = this.getIncomeTaxRate.deductible;
}
},
methods: { ...mapActions(["fetchIncomeTaxRate"])},
computed: {...mapGetters(["getIncomeTaxRate"])}
};
</script>
Store Code
import axios from "axios";
import commonAPI from "../commonAPI";
const state = {
incomeTaxRate: []
};
const mutations = {
setIncomeTaxRate: (state, incomeTaxRate) =>
(state.incomeTaxRate = incomeTaxRate)
};
const getters = {
getIncomeTaxRate: state => {
return state.incomeTaxRate;
}
};
const actions = {
async fetchIncomeTaxRate({ commit }, id) {
const response = await axios.get(
commonAPI.PAYROLL_BASE_URL + `/income-tax-rates/${id}`
);
commit("setIncomeTaxRate", response.data);
}
};
export default {
state,
mutations,
getters,
actions
};