From 841f889339499947cc7a96effaa2b610fc32ed04 Mon Sep 17 00:00:00 2001 From: joseph rosenthal Date: Tue, 30 Jan 2024 06:40:55 -0500 Subject: [PATCH] fixes logic for selectedOption fixes logic for selectedOption 1) fixes logic for selectedOption 2) uses selectedOption for computing label -- note: issue with virtual scrolling with lazy loading (PrimeNG Demo) as a result keeping original logic for computing label when virtual load (no harm) --- src/app/components/dropdown/dropdown.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/app/components/dropdown/dropdown.ts b/src/app/components/dropdown/dropdown.ts index c1bd64f3bf7..6bcaa693e30 100755 --- a/src/app/components/dropdown/dropdown.ts +++ b/src/app/components/dropdown/dropdown.ts @@ -924,8 +924,12 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV }); label = computed(() => { - const selectedOptionIndex = this.findSelectedOptionIndex(); - return selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder || 'p-emptylabel'; + // because of timining issues with virtual scroll lazy load PrimeNG demo, keep original logic for virtual scroll + if (this.virtualScroll) { + const selectedOptionIndex = this.findSelectedOptionIndex(); + return selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder || 'p-emptylabel'; + } + return this.modelValue() ? this.getOptionLabel(this.selectedOption) : this.placeholder || 'p-emptylabel'; }); selectedOption: any; @@ -938,8 +942,11 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV const visibleOptions = this.visibleOptions(); if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) { - this.selectedOption = visibleOptions[this.findSelectedOptionIndex()]; - this.cd.markForCheck(); + const selectedOptionIndex = this.findSelectedOptionIndex(); + if (selectedOptionIndex !== -1 || !modelValue || this.editable) { + this.selectedOption = visibleOptions[selectedOptionIndex]; + this.cd.markForCheck(); + } } if (modelValue !== undefined && this.editable) {