primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Autocomplete: selectedItem template only works if multiple="true"

Open Kev1n337 opened this issue 1 year ago • 9 comments

Describe the bug

According to the documentation, there should be a selectedItem-Template, that is used for displaying the selected object from an autocomplete input. Currently this template is ignored and instead the [field]-attribute is used. This does not allow to specify a complex template when the suggestions are complex objects.

This issue has also been reported in #2242 @mertsincan said a new ticket should be opened if the issue still persists.

Environment

Current version of angular and primeng, happening in all browsers.

Reproducer

https://github-cnxtrn.stackblitz.io

Angular version

15.2.8

PrimeNG version

15.4.1

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

18.16.0

Browser(s)

No response

Steps to reproduce the behavior

  1. Open Stackblitz or use this snippet in your application:
<p-autoComplete
  [(ngModel)]="customer"
  (completeMethod)="searchCustomers($event)"
  [suggestions]="customerSuggestions"
  [forceSelection]="true"
  field="last_name"
>
  <ng-template pTemplate="selectedItem" let-customer>
    <div>{{ customer.first_name }} {{ customer.last_name }}</div>
  </ng-template>

  <ng-template let-customer pTemplate="item">
    <div>{{ customer.first_name }} {{ customer.last_name }}</div>
  </ng-template>
</p-autoComplete>
  1. See the input value of a selected item being "Doe" (from 'field') instead of "Jane Doe" (from 'selectedItem')

Expected behavior

When the selectedItem is specified it should be used instead of the field-attribute.

Kev1n337 avatar May 03 '23 11:05 Kev1n337

Yeah selected item only work in multiple mode...

In single mode the input is field with the selected field

denny99 avatar May 11 '23 12:05 denny99

Duplicates #2242 (opened in 2017) and #10360 (2021), but these issues were somehow closed. @mertsincan @cetincakiroglu

ThoSap avatar Sep 03 '23 12:09 ThoSap

This is very important to my project, and it is heartbreaking to see this has been ignored and closed out many times. BUMPing the thread.

straiforos avatar Jan 26 '24 14:01 straiforos

@cetincakiroglu

ThoSap avatar Jan 26 '24 15:01 ThoSap

You can use optionLabel as a function to make a custom value.

/**
 * Property name or getter function to use as the label of an option.
 * @group Props
 */
@Input() optionLabel: string | ((item: any) => string) | undefined;

field must be omitted in this case, otherwise it will be used instead.

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

bluesbroz avatar Feb 09 '24 14:02 bluesbroz

@bluesbroz This is the workaround that I am using. I do not like that it requires an additional property or class method to display something that should and could be defined in the template.

straiforos avatar Feb 13 '24 17:02 straiforos

At this point I am now using item templating and toString on my classes since it works well. I would like the templating to work for selected item while not in multi-select mode since my work had a use-case for tabular data display which is not possible via string.

straiforos avatar Mar 21 '24 02:03 straiforos

Hi all,

For now selectedItem template is only supported in multiple mode. I've updated the issue title and added it into 17.Future milestone. We'll add selectedItem support for single mode in the future.

cetincakiroglu avatar Mar 22 '24 15:03 cetincakiroglu