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

Template for selected item in autocomplete (single selection mode) #2242

Closed
stephanie-dm opened this issue Mar 8, 2017 · 29 comments · May be fixed by #14143
Closed

Template for selected item in autocomplete (single selection mode) #2242

stephanie-dm opened this issue Mar 8, 2017 · 29 comments · May be fixed by #14143

Comments

@stephanie-dm
Copy link

I'm submitting a ... (check one with "x")

[ ] bug report => Search github for a similar issue or PR before submitting
[ x ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Current behavior

It's already possible to define an "item" template for the suggestions of the autocomplete, but this template is not used for the selected item. The selected item only looks at the "field" attribute, so it can only display 1 property of the object to which the ngModel is bound.

It's also possible to define a "selectedItem" template, but this template only seems to work in multiple selection mode (as stated in the documentation).

I've tried this example:

<p-autoComplete [(ngModel)]="customer" [suggestions]="customerResults" (completeMethod)="searchCustomers($event)" field="name">
  <template let-customer pTemplate="item">
    {{customer.name}} {{customer.firstname}}
  </template>
  <template let-cusstomer pTemplate="selectedItem">
    {{customer.name}} {{customer.firstname}}
  </template>
</p-autoComplete>

With this code my selected item only shows the "name" property of my customer-object. If I don't provide the field-property, my selected item is displayed as [object Object].

Expected behavior
I expect it to be possible to define a template for the selected item when the autocomplete is in single selection mode. It seems like I'm missing something as no one else seems to be asking this question, but I've tried everything I could find in the documentation, and nothing seemed to work.

Minimal reproduction of the problem with instructions

  • Angular version: ~2.4.0
  • PrimeNG version: 2.0.1
  • Browser: [all ]
@cagataycivici
Copy link
Member

This should be fixed in recent PrimeNG release, please try with it and if the issue persists drop a comment with along with a test case based on plunkr below and we'll review again.

http://plnkr.co/edit/Wj39h1?p=preview

@stephanie-dm
Copy link
Author

@cagataycivici : I can still reproduce this issue starting from the Plunkr you provided (this uses PrimeNG 4.1.0.RC2 if I'm correct):
http://plnkr.co/edit/vKhkap?p=preview

@cagataycivici
Copy link
Member

Ok reopening then to be reviewed. Thanks.

@cagataycivici cagataycivici added the Status: Pending Review Issue or pull request is being reviewed by Core Team label Jun 22, 2017
@cagataycivici cagataycivici reopened this Jun 22, 2017
@cagataycivici cagataycivici added this to the 4.1.0 milestone Jun 22, 2017
@cagataycivici cagataycivici self-assigned this Jun 22, 2017
@cagataycivici cagataycivici modified the milestones: 4.1.1, 4.1.0 Jun 23, 2017
@cagataycivici cagataycivici modified the milestone: 4.1.1 Jul 19, 2017
@SIlver--
Copy link

Is this planned for a new milestone?

@riisi
Copy link

riisi commented Aug 30, 2017

I'd also like this functionality.

@andrewaston13
Copy link

I also need this functionality

@wautersj
Copy link

Any news on this? Recent install, and still running against this issue. Tho it is documented in the spec: https://www.primefaces.org/primeng/#/autocomplete

@cagataycivici cagataycivici added this to the 5.1.0-RC1 milestone Dec 19, 2017
@cagataycivici
Copy link
Member

In single mode, there is an input so can't think of a way to use the selectedItemTemplate and the input at the same time so that's why it was closed I guess. Not sure how we can offer templating for the value prop of the input field.

@cagataycivici cagataycivici added Status: Discussion Issue or pull request needs to be discussed by Core Team and removed Status: Pending Review Issue or pull request is being reviewed by Core Team labels Dec 24, 2017
@cagataycivici cagataycivici removed this from the 5.1.0-RC1 milestone Dec 25, 2017
@stephanie-dm
Copy link
Author

@cagataycivici : I don't understand what you mean by your last comment. Is something unclear about my initial question?
In the example of my Plunkr: When I select "Smith John" in the autocomplete, I'd like to see "Smith John" instead of "Smith" in the input field of the autocomplete. Currently it's not possible to display more than 1 field/property of the selected object in the input of the autocomplete. This should be possible just like you can already define a selectedItemTemplate for the multiple selection mode and just like you can define an itemTemplate for the items in the suggestion list of the autocomplete.

@sometime
Copy link

sometime commented Jan 3, 2018

I also need this functionality

@togbyte
Copy link

togbyte commented Feb 21, 2018

I need this functionality too.

@fintrader
Copy link

fintrader commented May 17, 2018

@cagataycivici I've implemented templating before using contenteditable="true" on a div. I did this in AngularJS using uib-typeahead. Unfortunately the new Angular implementations all require the selector on an "input" which makes this difficult. I'm hoping PrimeNG can implement it so it uses a <div> with contenteditable="true" in place of an <input>

@flix-
Copy link

flix- commented Nov 21, 2018

Workaround using conversion method:

Lets say we have a User type:

interface User {
  firstName: string;
  lastName: string;
}

Use:

<p-autoComplete [field]="myUserConversionMethod" ...></p-autoComplete>

And in Component:

public myUserConversionMethod(user: User) {
  return user.firstName + ' ' + user.lastName;
}

@ben12
Copy link

ben12 commented Mar 2, 2019

Workaround works, thanks.

I notice a mismatch between code and documentation...

In the documentation 'field' is declared as 'any'.
So, I can use a conversion method or not (description suggests that i can not) ?

Name Type Default Description
field any null Field of a suggested object to resolve and display.

In AutoComplete component, 'field' attribute is declared as 'string'.
I must not use a conversion method ?

    @Input() field: string;

This needs to be clarified.

@dydFreeman
Copy link

dydFreeman commented May 3, 2019

@flix- can you provide a stackblitz with the workaround? [field] conversion doesn't seem to work.

@ironbirdie
Copy link

Same issue with angular 8. also related the attribute field can only be of type string. If it's a number it gives following error:

ERROR TypeError: itemValue.trim is not a function
at _loop_1 (autocomplete.js:398)
at AutoComplete.push../node_modules/primeng/components/autocomplete/autocomplete.js.AutoComplete.onInputChange (autocomplete.js:409)

@rafa-suagu
Copy link

@cagataycivici We also need this functionality.

A very bad workaround with a weird flickering:

@ViewChild('inputSearchTerm', {static: false}) inputSearchTerm: AutoComplete;

_searchTerm: string;

onSelectItem(item: any): void {
  this.inputSearchTerm.inputEL.nativeElement.value = this._searchTerm;
}

@adamzawalski
Copy link

Hi all,
is there an update to show more properties than just one.
I am using "primeng": "11.3.2", with "angular": "11.2.7" and when trying to workaround from @flix- I get an error:
Type '(user: User) => any' is not assignable to type 'string'.

@martin-segovia
Copy link

Any update on this issue?
(Using Angular 12.1.1 and PrimeNG 12.0.1 btw)

@tfohlmeister
Copy link

I stumbled across this issue and found that the conversion method still works. As described by @ben12 it is just a compiler error as the autocomplete component declares the field input as string.

You can force angular to ignore this template error by surrounding the variable with $any:

<p-autoComplete [field]="$any(myUserConversionMethod)" ...></p-autoComplete>

This actually works fine for me using primeng 12.2.2 and Angular 12.2.

@mertsincan mertsincan removed the Status: Discussion Issue or pull request needs to be discussed by Core Team label Nov 9, 2022
@mertsincan
Copy link
Member

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap?
Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

@sometime
Copy link

@mertsincan thanks!
But i don't use PrimeNG anymore.
I'm submitted this 4 years ago.
Because there was no reaction – switched to another UI.

@Leonardonline
Copy link

It still doesn't work.
Tested with version 14

@roosi-ann-kodality
Copy link

This issue is not solved. I need this functionality as well.

@jacqueslareau
Copy link

This is still an issue after all those years. Should be reopened. Workarounds don't seem to work. I'm on Angular 15 and PrimeNG 15.

@ahelmel
Copy link

ahelmel commented Jun 26, 2023

Is there any progress? I have the same issue and the workarounds do not work. Can also the documentation be extended so the devs know that the selectItem template only works for multiple but not single selection mode.

@ak293700
Copy link

ak293700 commented Oct 7, 2023

Same problem. At least the doc could be explicit

@pjlasl
Copy link

pjlasl commented Jan 30, 2024

Still an issue in v16

@uebelmar
Copy link
Contributor

still an issue in v17, selectedItem Template is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet