Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kev1n337 opened this issue May 3, 2023 · 10 comments
Open

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

Kev1n337 opened this issue May 3, 2023 · 10 comments
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add Type: New Feature Issue contains a new feature or new component request
Milestone

Comments

@Kev1n337
Copy link

Kev1n337 commented May 3, 2023

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 Kev1n337 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label May 3, 2023
@denny99
Copy link
Contributor

denny99 commented May 11, 2023

Yeah selected item only work in multiple mode...

In single mode the input is field with the selected field

@ThoSap
Copy link
Contributor

ThoSap commented Sep 3, 2023

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

@straiforos
Copy link

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.

@ThoSap
Copy link
Contributor

ThoSap commented Jan 26, 2024

@cetincakiroglu

@bluesbroz
Copy link

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;
}

@straiforos
Copy link

@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
Copy link

straiforos commented Mar 21, 2024

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.

@cetincakiroglu cetincakiroglu changed the title Autocomplete: Template selectedItem is ignored Autocomplete: selectedItem template only works if multiple="true" Mar 22, 2024
@cetincakiroglu cetincakiroglu added this to the 17.Future milestone Mar 22, 2024
@cetincakiroglu cetincakiroglu added Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add Type: New Feature Issue contains a new feature or new component request and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Mar 22, 2024
@cetincakiroglu
Copy link
Contributor

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.

@amoretti-dev
Copy link

v.17 still not working.
The best workaround for me is to use dropdown component with filter.

@gianluca-moro
Copy link
Contributor

So, is it still planned for v17 or v18?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add Type: New Feature Issue contains a new feature or new component request
Projects
Status: No status
Development

No branches or pull requests

8 participants