components icon indicating copy to clipboard operation
components copied to clipboard

Autocomplete binding with complex object list doesn't behave as it should

Open atul221282 opened this issue 9 years ago • 6 comments

Bug, feature request, or proposal:

Autocomplete binding with complex object doesn't behave as it should. I want to bind the autocomplete to complex object list and assign the selected complex object to another one. Updated plunker explaining the issue of displayWith

What is the expected behavior?

Example in documentation doesn't work with complex object list

What is the current behavior?

It works only with array list of string

What are the steps to reproduce?

Instead of the binding list with plain string list bind it with the list of complex object.

Providing a Plunker (or similar) is the best way to get the team to see your issue. Plunker template: http://plnkr.co/edit/CNcnsJWSSWUR7NishhpL?p=preview

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

Autocomplete should manage the complex object binding by providing property name dynamically

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

Angular Material v2.0.0-beta.3

Is there anything else we should know?

atul221282 avatar Apr 10 '17 13:04 atul221282

The autocomplete docs mention the displayWith attribute. You can provide a function that will return the string version of your object. Is that what you're looking for?

willshowell avatar Apr 10 '17 14:04 willshowell

I tried that but there is an issue in that function itself it just accepts the data as a parameter and in that function, this is referencing to function itself not to my component class, therefore, i cannot use dynamic property in that function. I will try to explain that by an example. I have got any object which can have any property like id, text or it can be value, name so therefore I am using the list and using the property name to populate the selected object. if u look at plunker u will understand what I am trying to achieve

atul221282 avatar Apr 10 '17 14:04 atul221282

Ahh ok I see now. You can do [displayWith]="displayFn.bind(this)" but that is arguably clunkier than necessary and may have unintended side effects? Perhaps @kara can weigh in

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

willshowell avatar Apr 10 '17 14:04 willshowell

Thanks for your workaround but as you said it is just a workaround. displayWith should be more flexible or complex object binding should be easier moreover in the plunker the selected value is not getting reflected in an autocomplete input. I have updated the plunker and one more step closer to what I want to achieve but the issue I am facing now is when I select the same value the mdInput is getting cleared. I know you guys are doing a great job but autocomplete is very buggy at the moment

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

atul221282 avatar Apr 10 '17 14:04 atul221282

If you need a specific context associated with the displayWith function, you can bind that context in your TypeScript code. Something like

@Component({
  selector: 'my-cool-component'
})
class MyCoolComponent {
  myDisplayWithFunction = this.someFunc.bind(this);
}

Then use [displayWith]="myDisplayFunction".

jelbourn avatar Apr 11 '17 23:04 jelbourn

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.