Skip to content

Commit

Permalink
Fixed #14172 - Dropdown | refactor template value and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Nov 22, 2023
1 parent 65fbc4a commit 385887a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class DropdownItem {
(keydown)="onKeyDown($event)"
>
<ng-container *ngIf="!selectedItemTemplate; else defaultPlaceholder">{{ label() === 'p-emptylabel' ? '&nbsp;' : label() }}</ng-container>
<ng-container *ngTemplateOutlet="selectedItemTemplate; context: { $implicit: modelValue() }"></ng-container>
<ng-container *ngTemplateOutlet="selectedItemTemplate; context: { $implicit: selectedOption }"></ng-container>
<ng-template #defaultPlaceholder>
<span *ngIf="label() === placeholder || (label() && !placeholder)">{{ label() === 'p-emptylabel' ? '&nbsp;' : placeholder }}</span>
</ng-template>
Expand Down Expand Up @@ -921,13 +921,20 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
return selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder || 'p-emptylabel';
});

selectedOption: any;

constructor(public el: ElementRef, public renderer: Renderer2, public cd: ChangeDetectorRef, public zone: NgZone, public filterService: FilterService, public config: PrimeNGConfig) {
effect(() => {
const modelValue = this.modelValue();
const visibleOptions = this.visibleOptions();

if (modelValue && this.editable) {
this.updateEditableLabel();
}

if(visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
this.selectedOption = visibleOptions[this.findSelectedOptionIndex()];
}
});
}

Expand Down Expand Up @@ -1104,7 +1111,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
}

getOptionLabel(option: any) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option && option?.label !== undefined ? option.label : option;
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option && option.label !== undefined ? option.label : option;
}

getOptionValue(option: any) {
Expand Down
11 changes: 6 additions & 5 deletions src/app/showcase/doc/dropdown/templatedoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Input, OnInit } from '@angular/core';
import { Code } from '../../domain/code';


@Component({
selector: 'dropdown-template-demo',
template: ` <section class="py-4">
Expand All @@ -11,11 +12,11 @@ import { Code } from '../../domain/code';
</p>
</app-docsectiontext>
<div class="card flex justify-content-center">
<p-dropdown [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [showClear]="true" placeholder="Select a Country">
<ng-template pTemplate="selectedItem">
<div class="flex align-items-center gap-2" *ngIf="selectedCountry">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedCountry.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedCountry.name }}</div>
<p-dropdown [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [showClear]="true" placeholder="Select a country">
<ng-template pTemplate="selectedItem" let-selectedOption>
<div class="flex align-items-center gap-2" *ngIf="selectedOption">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedOption.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country pTemplate="item">
Expand Down

1 comment on commit 385887a

@vercel
Copy link

@vercel vercel bot commented on 385887a Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.