question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

*reduce* need for displayWith function on mat-autocomplete

See original GitHub issue

Bug, feature request, or proposal:

Proposal

What is the expected behavior?

A simple way to display the selected autocomplete value in an input field.

What is the current behavior?

Currently when you setup an autocomplete and bind a [value] to the <mat-autocomplete>, the autocomplete displays the value UNLESS you assign a displayWith function. However, it makes no sense to me to default to showing the [value] since that is intended to be the behind-the-scenes value, not the displayValue. If we simply default to displaying the viewValue somehow, we would no longer need a displayWith function, and that would eliminate numerous other headaches reported in other issues on this forum.

What are the steps to reproduce?

Providing a StackBlitz/Plunker (or similar) is the best way to get the team to see your issue.
Plunker starter (using on @master): https://goo.gl/uDmqyY
StackBlitz starter (using latest npm release): https://goo.gl/wwnhMV

This proposal relies on an understanding of the typical autocomplete implementation e.g. here: https://plnkr.co/edit/W9vdWaGcoG90LKM8hHgI?p=preview

What is the use-case or motivation for changing an existing behavior?

displayWith function has numerous issues reported. For my purpose I see it as a potential blocker to generating dynamic forms in particular, for reasons i’m glad to detail if need be but I’ll spare at this time.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Is there anything else we should know?

i’m guessing the displayWith function was created in the first place to address a legitimate challenge. Namely, the autocomplete control needs a way to know what the displayValue is.

so in the demo example here:

<mat-form-field>
  <mat-select placeholder="State">
    <mat-option>None</mat-option>
    <mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option>
  </mat-select>
</mat-form-field>

the displayValue is state.name. Perhaps the new syntax that requires no displayValue would be like this:

<mat-form-field>
  <mat-select placeholder="State">
    <mat-option>None</mat-option>
    <mat-option *ngFor="let state of states" [value]="state.code" [dislayValue]="state.name"></mat-option>
  </mat-select>
</mat-form-field>

Such a design would continue to allow flexibility to assign the whole object or a particular field to the [value]…which is important to various use cases out there.

I don’t see any downside to eliminating the displayWith function as long as the viewValue is always shown I doubt people would care.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
kdubiouscommented, Dec 20, 2017

I’m getting tripped up on implementing what I think is a fairly standard use case for a combobox (autocomplete).

The list data comes from a backend service in a Key-Value pair format. {id:1, name:"One"}) I want to bind id, and show name. I also want to filter when I type.

Is this supported today?

8reactions
sarora2073commented, Jan 9, 2018

It’s supported today and you should be able to find sample working implementations elsewhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to display using [displayWith] in AutoComplete Material2
I know I have to use displayWith , however it isn't working as I'm expecting. The function called as [displayWith]="displayFn.bind(this)"> just ...
Read more >
Autocomplete | Angular Material
displayWith : ((value: any) => string) | null. Function that maps an option's control value to its display value in the trigger. @Input()....
Read more >
mat-autocomplete displayWith async (how to return from ...
I am trying to use the [displayWith] attribute in order to get to the Contact's name. However, in order to do that, I...
Read more >
Everything you need to know about Angular Material ... - Medium
This is my DisplayWith function that will accept the value and get the object from the filteredOptions array and return the CustomerId. So,we ......
Read more >
Angular Material Autocomplete - Multiple Use Cases covered
Angular Material Autocomplete - Multiple Use Cases covered0:00 Intro0:23 - Use Case: Autocomplete with native input8:15 - Implement ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found