Angular - Reset selection if I choose not to accept change [closed]

Angular - Reset <select><option> selection if I choose not to accept change [closed]



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.


<select>


<option>


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.


yes


no



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


no



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



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.


state


disabled



This question appears to be off-topic. The users who voted to close gave this specific reason:




3 Answers
3



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


preState


newState


preState


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:


[selected]='!newState'


<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>





Please see my update
– physicsboy
Sep 4 '18 at 12:56





i added the considerations needed for your update
– Fartab
Sep 4 '18 at 13:51



Switch to reactive forms and register the default state.



You can then call reset on your form control with the default state.


reset



Stackblitz


<select [formControl]="select">
<option disabled>--Pick an option--</option>
<option *ngFor="let option of options" [value]="option.name" [selected]="selectedOption(option.name)">option.name</option>
</select>


select: FormControl = new FormControl(this.options[0].name);
defaultState = this.select.value;

constructor()
this.select.valueChanges.subscribe(value => this.setState(value));


...

no()
this.select.reset(this.defaultState);
this.checkState = false;



N.B. : I used a form control, but the same applies to form groups





Please see my update
– physicsboy
Sep 4 '18 at 12:56





You don't have any disabled in your code, and even if you did, my solution would take care of it (form controls have a disabled property)
– trichetriche
Sep 4 '18 at 12:57


disabled


disabled





the line select: FormControl = new FormControl(this.options[0].name); makes it default to an entry within the array. The disabled is in the first <option> tag
– physicsboy
Sep 4 '18 at 14:59


select: FormControl = new FormControl(this.options[0].name);


disabled


<option>





Because that's an example. You can put whatever you want as the default value. I chose the first option it is usually what people do (even you, the first option is selected in your example), but you can put whatever you want in it. The beauty of my solution is that whatever the first value is, resetting the control will always set it back.
– trichetriche
Sep 4 '18 at 15:06



Since you are not using two way binding the change you make to your variable doesn't affect the template. This is my approach:



HTML file


<select [(ngModel)]="state" (change)="setState()">
<option disabled [value]="null">--Pick an option--</option>
<option *ngFor="let option of options" [value]="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">lastState</span> to <span class="bold">state</span></p>
<button (click)="yes()">yes</button><button (click)="no()">no</button>
</div>



TS File


state;
lastState;
checkState = false;

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

constructor()
this.lastState = this.state;


setState()
this.checkState = true;


yes()
this.checkState = false;
this.lastState = this.state;


no()
this.checkState = false;
this.state = this.lastState;





Please see my update
– physicsboy
Sep 4 '18 at 12:56





@physicsboy one thing you can do is put null as the disabled option's value, then if your state is null you'll see the disabled option
– Nima Hakimi
Sep 4 '18 at 12:58

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế