primeng icon indicating copy to clipboard operation
primeng copied to clipboard

autocomplete dont render selecteditem Templete with multiple=false

Open Kirbylix opened this issue 5 months ago • 3 comments

Describe the bug

AutoComplete component with multiple set to false, the selectedItemTemplate is not rendered after selecting an item. This works correctly when multiple=true.

Pull Request Link

No response

Reason for not contributing a PR

  • [ ] Lack of time
  • [ ] Unsure how to implement the fix/feature
  • [ ] Difficulty understanding the codebase
  • [ ] Other

Other Reason

This issue appears to be related to the template rendering logic for single-selection mode.

Workaround: None found for multiple=false.

Reproducer

https://stackblitz.com/edit/github-d16kd56z-m8fkpq9h?file=src%2Fapp%2Fapp.component.html

Environment

PrimeNG Version: 20.0.1 Angular Version: 20.1.7 Browser(s): Chrome OS: Windows 11

Angular version

20.1.7

PrimeNG version

v20

Node version

22.10.2

Browser(s)

Chrome

Steps to reproduce the behavior

  1. Create an AutoComplete component with: [(ngModel)] bound to a single object multiple set to false selectedItemTemplate defined in the template
  2. Search for and select an item.
  3. Observe the rendered value in the input field.
  <p-autocomplete
    [(ngModel)]="value1"
    [suggestions]="items"
    (completeMethod)="search($event)"
    [multiple]="false"
  >
    <ng-template pTemplate="selectedItem" let-item>
      Custom selected item label
    </ng-template>
  </p-autocomplete>

Expected behavior

The selectedItemTemplate should be applied to the selected value when multiple=false, similar to how it works when multiple=true.

Kirbylix avatar Aug 14 '25 14:08 Kirbylix

Do you guys have any workaround besides enabling multiple="true"? By now I think that using the p-select + filter would allow users to have a pretty similar experience

AndreSantosGHC avatar Aug 26 '25 17:08 AndreSantosGHC

We also encounter a similiar issue since v19.

Given this simple template,

<!-- selected: ModelSignal ; suggesstions: Signal<SelectedItem> -->
<p-autoComplete [(ngModel)]="selected" [suggestions]="searchUdSuggestions()" (completeMethod)="search($event)">
</p-autoComplete>

Suggestions items are displayed using the "label" property. When we select an item, the input's value besome "[object Object]".

As a workaround, I had to write this directive... if it can help.

@Directive({
  standalone: false,
  selector: 'p-autoComplete, p-autocomplete',
})
export class AutocompleteLabelDirective implements OnDestroy {

  field: InputSignal<string> = input();
  label: InputSignal<string> = input();
  optionLabel: InputSignal<string> = input();

  readonly autoComplete = inject(AutoComplete, { optional: true });
  
  private sub$: Subscription;

  constructor() {
    if (this.autoComplete && this.autoComplete.multiple !== true) {
      this.sub$ = this.autoComplete.onSelect.subscribe(item => {
        this.autoComplete.inputEL.nativeElement.value = resolveFieldData(item.value, this.field() || this.label() || this.optionLabel() || 'label');
      })
    }
  }

  ngOnDestroy(): void {
    this.sub$?.unsubscribe();
  }

}

lmorel3 avatar Sep 26 '25 13:09 lmorel3

Specifying optionLabel works for me if you only need to display text of selected item: https://v20.primeng.org/autocomplete#objects Since the item template works, the options template can still be customized and the selected item can be displayed as text instead of [object Object] until they fix the selecteditem support...

szlezakbartosz avatar Dec 09 '25 07:12 szlezakbartosz