Answer the question
In order to leave comments, you need to log in
How to track field changes in a component to enable save buttons?
There are date fields, let's say 'name' and ''name_changed'.
It's under surveillance:
watch:{
name(val, old){
this.name_changed = true;
}
}
Answer the question
In order to leave comments, you need to log in
Watchers are usually a hallmark of jailbreak =) It's
better to keep the original data and compare it with the current one. So the honest changed will be (you rather have "touched").
Here's roughly:
const api = {
async getData() {
return {name: 'Foo', email: '[email protected]'};
}
}
new Vue({
el:'#app',
template: `<div v-if="current">
<input v-model="current.name" />
<input v-model="current.email" />
<button :disabled="!isChanged">submit</button>
</div>`,
data() {
return {
original: null,
current: null,
}
},
async created() {
const data = await api.getData();
this.original = JSON.stringify(data);
this.current = data;
},
computed: {
isChanged() {
return JSON.stringify(this.current) !== this.original;
}
}
});
<input type="text" v-model="name" @input="name_changed = true">
and there will be happiness
the problem is that apparently when the name field is filled on the first request, accordingly the name_changed field becomes true and the save button is initially available
watch:{
name(val, old){
this.name_changed = true;
}
}
this.$watch('name', () => this.name_changed = true);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question