Just one more thing - here comes an implementation for a PrimeNG dropdown with a PrimeNG confirmation dialog and proper PrimeNG select items (that are actually localised enums). I had a very hard time trying to find a solution for this...
Angular version is 15.0.4, PrimeNG version is 15.0.0 (but it also worked with Angular version 14.2.12 and PrimeNG version 14.2.2).
The two main problems are:
To get the currently selected item from the dropdown before the change happens (unfortunately, the currently selected item is not available in any event). This can be solved by using a @ViewChild on the dropdown and retrieving the value at run-time by means of onShow().
To restore the last selected item if the user cancels the change in the confirmation dialog (using a SelectItem explicitely will lead to strange errors). This can be solved by not typing or initialising selectedInterval and lastInterval as a SelectItem and only using the value field.
dropdown-test.component.html
<p-confirmDialog
[baseZIndex]="10000"
[dismissableMask]="true"
defaultFocus="reject"
rejectButtonStyleClass="p-button-text"
rejectLabel="No"
acceptButtonStyleClass="p-button-text"
acceptLabel="Yes">
</p-confirmDialog>
<p-toolbar>
<p-dropdown
#dropdownInterval
[options]="intervalOptions"
[(ngModel)]="selectedInterval"
(onShow)="onShow()"
(onChange)="onChange($event)">
</p-dropdown>
</p-toolbar>
dropdown-test.component.ts
import {Component, OnInit, ViewChild} from '@angular/core';
import {Dropdown} from 'primeng/dropdown';
import {ConfirmationService, MessageService, SelectItem} from 'primeng/api';
@Component({
selector: 'app-dropdown-test',
templateUrl: './dropdown-test.component.html',
styleUrls: ['./dropdown-test.component.css']
})
export class DropdownTestComponent implements OnInit {
@ViewChild('dropdownInterval') dropdownInterval: Dropdown;
intervalOptions: SelectItem[] = [];
selectedInterval: any; // Important NOT to type or initialise as SelectItem!
lastInterval: any; // Important NOT to type or initialise as SelectItem!
constructor(
private confirmationService: ConfirmationService,
private messageService: MessageService
) {
}
ngOnInit(): void {
this.intervalOptions = this.getSelectItemsEn();
this.selectedInterval = this.getSelectItemEnByUniqueName('MONTHLY');
}
onShow() {
this.lastInterval = this.dropdownInterval.value; // Get the current value from the dropdown before the change takes place!
}
onChange(event) {
if (this.lastInterval) { // Ignore the initial change in ngOnInit!
this.confirmationService.confirm({
header: 'Confirmation',
icon: 'pi pi-question-circle',
message: 'Do you want to change the selected item in the dropdown?',
accept: () => {
// TODO: Continue...
}, reject: () => {
this.lastInterval = this.selectedInterval;
}
});
}
}
getSelectItemsEn(): SelectItem[] {
const intervals: SelectItem[] = [];
intervals.push({label: 'Monthly', value: 'MONTHLY'});
intervals.push({label: 'Quarterly', value: 'QUARTERLY'});
intervals.push({label: 'Yearly', value: 'YEARLY'});
return intervals;
}
getSelectItemEnByUniqueName(uniqueName: any): SelectItem {
if (uniqueName === 'MONTHLY') {
return {label: 'Monthly', value: 'MONTHLY'};
} else if (uniqueName === 'QUARTERLY') {
return {label: 'Quarterly', value: 'QUARTERLY'};
} else {
return {label: 'Yearly', value: 'YEARLY'};
}
}
}