diff --git a/src/app/components/multiselect/multiselect.ts b/src/app/components/multiselect/multiselect.ts index de63585f7f7..13d3c224c75 100755 --- a/src/app/components/multiselect/multiselect.ts +++ b/src/app/components/multiselect/multiselect.ts @@ -178,7 +178,7 @@ export class MultiSelectItem { {{ placeholder || defaultLabel || 'empty' }} - + @@ -972,6 +972,8 @@ export class MultiSelect implements OnInit, AfterViewInit, AfterContentInit, Aft focusedOptionIndex = signal(-1); + selectedOptions: any; + get containerClass() { return { 'p-multiselect p-component p-inputwrapper': true, @@ -1086,7 +1088,15 @@ export class MultiSelect implements OnInit, AfterViewInit, AfterContentInit, Aft return ObjectUtils.isNotEmpty(this.maxSelectedLabels) && this.modelValue() && this.modelValue().length > this.maxSelectedLabels ? this.modelValue().slice(0, this.maxSelectedLabels) : this.modelValue(); }); - constructor(public el: ElementRef, public renderer: Renderer2, public cd: ChangeDetectorRef, public zone: NgZone, public filterService: FilterService, public config: PrimeNGConfig, public overlayService: OverlayService) {} + constructor(public el: ElementRef, public renderer: Renderer2, public cd: ChangeDetectorRef, public zone: NgZone, public filterService: FilterService, public config: PrimeNGConfig, public overlayService: OverlayService) { + effect(() => { + const modelValue = this.modelValue(); + const visibleOptions = this.visibleOptions(); + if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions) && modelValue) { + this.selectedOptions = visibleOptions.filter((option) => modelValue.includes(option[this.optionLabel]) || modelValue.includes(option[this.optionValue])); + } + }) + } ngOnInit() { this.id = this.id || UniqueComponentId(); @@ -1250,6 +1260,11 @@ export class MultiSelect implements OnInit, AfterViewInit, AfterContentInit, Aft }); } + + findSelectedOptionIndex() { + return this.hasSelectedOption() ? this.visibleOptions().findIndex((option) => this.isValidSelectedOption(option)) : -1; + } + onOptionSelectRange(event, start = -1, end = -1) { start === -1 && (start = this.findNearestSelectedOptionIndex(end, true)); end === -1 && (end = this.findNearestSelectedOptionIndex(start)); @@ -1930,6 +1945,7 @@ export class MultiSelect implements OnInit, AfterViewInit, AfterContentInit, Aft this.value = null; this.checkSelectionLimit(); this.updateModel(null, event); + this.selectedOptions = null; this.onClear.emit(); event.stopPropagation(); diff --git a/src/app/showcase/doc/multiselect/templatedoc.ts b/src/app/showcase/doc/multiselect/templatedoc.ts index 5f8b6b9eadf..253a85b4a5b 100644 --- a/src/app/showcase/doc/multiselect/templatedoc.ts +++ b/src/app/showcase/doc/multiselect/templatedoc.ts @@ -15,11 +15,11 @@ interface Country {
-
+
{{ option.name }},
-
Select Countries
+
Select Countries
@@ -60,11 +60,11 @@ export class TemplateDoc implements OnInit { basic: ` -
+
-
{{ option.name }},
+
{{ option.name }},
-
Select Countries
+
Select Countries
@@ -78,11 +78,11 @@ export class TemplateDoc implements OnInit {
-
+
-
{{ option.name }},
+
{{ option.name }},
-
Select Countries
+
Select Countries