Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
41
rated 0 times [  47] [ 6]  / answers: 1 / hits: 16812  / 6 Years ago, tue, september 4, 2018, 12:00:00

I have a dropdown list made of <select> and <option> tags, with a function to bring up a confirmation box when a selection is made. A simple yes/no.



Clicking yes will set the current state to the state chosen from the dropdown, whereas no will of course not change the state.



However, when clicking no, the dropdown list will remain highlighted on the state that we just declined changing to.



Is there a way in which I can get it to go back to the current state?



Here is my stackblitz to show what is going on



UPDATE



Some of the answers below have been good, but the one thing missing from them is that, if the state is not assigned to a value, I do not get the disabled option displayed.


More From » angular

 Answers
39

Hold prevois selected option in a variable preState and revert newState to preState when user clicks no.



export class AppComponent  {
state;
newState;
preState;
checkState = false;

options = [
{name:'dormant'},
{name:'active'},
{name:'finished'}
];

setState(state) {
this.preState = this.newState;
this.newState = state
this.checkState = true;
}

selectedOption(option) {
return this.newState === option;
}

yes() {
this.state = this.newState;
this.checkState = false;
}

no() {
this.checkState = false;
this.newState = this.preState;
}
}




UPDATE:



To show the disabled option mentioned in your UPDATE, put [selected]='!newState' on the disabled option:



<select (change)=setState($event.target.value)>
<option disabled [selected]='!newState'>--Pick an option--</option>
<option *ngFor=let option of options [value]=option.name [selected]=selectedOption(option.name)>{{option.name}}</option>
</select>

<div *ngIf=checkState class=check-state>
<p>Are you sure you want to change</p>
<p><span class=bold>{{state}}</span> to <span class=bold>{{newState}}</span></p>
<button (click)=yes()>yes</button><button (click)=no()>no</button>
</div>

[#53565] Friday, August 31, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;