Answer the question
In order to leave comments, you need to log in
How to UNDO vue select?
There is a task, when changing, select
ask the user with the help of confirm
whether he really wants to change this value. The value itself is in vuex
, through v-model
and computed get/set
the value changes. There is no set
check with confirm
, but when you cancel the changes , it still select
changes, although the value in vuex
remains the same. How can I make the undo functionality so that onInput select
it does not work?
codesandbox
<template>
<div>
Current real value: {{ selected }}
<select v-model="selected" @input="onInput">
<option v-for="(item, index) of list" :key="index" :value="item">{{ item }}</option>
</select>
</div>
</template>
<script>
export default {
name: "toster",
data: () => ({
vuex: 3,
list: [1, 2, 3, 4, 5, 6]
}),
computed: {
selected: {
get() {
return this.vuex;
},
set(value) {
if (window.confirm("Realy change select?")) {
this.vuex = value;
}
}
}
},
methods: {
onInput({ target: { value } }) {
console.log("onInput", value);
}
}
};
</script>
Answer the question
In order to leave comments, you need to log in
<select ref="selectr" v-model="selected" @input="onInput(e)">
<option v-for="(item, index) of list" :key="index" :value="item">{{ item }}</option>
</select>
selected: {
get() {
return this.vuex;
},
set(value) {
if (window.confirm("Realy change select?")) {
this.vuex = value;
} else {
this.$refs.selectr.value = this.vuex;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question