angular-ui typeahead select id instead of object
Asked Answered
C

1

16

I'm trying to set an id in the typeahead instead an object and it doesn't work like a select element.

Here is an example, where you can write the correct word in the typeahead, and then the select element is updated, but if you change the select, the typeahead isn't updated with the name, is updated with the id instead.

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

I'm doing something wrong?

Caviness answered 3/6, 2013 at 17:37 Comment(4)
You aren't doing anything "wrong", it's just not the behavior you were expecting. You aren't seeing the "id" of the object, you are seeing the index of the select option. Change the id value to anything else and you will still see "1" and "2" when you click on an item.Neille
This is not true, I have changed the id for 4 and 5, and it shows 4 and 5, it's not showing the index. I have update plunker.Caviness
Oops - bad conjecture on my part.Neille
I know this is an old thread, but ... I found this very useful #17856626Kaunas
J
22

What about something like that?

http://plnkr.co/edit/pUPzVK?p=preview (check the typehead and ng-options definition)

What you "did wrong" is missing the info, that typehead doesn't have exactly the same definition as ng-options (I've struggled with this for a while too).

Hope this helps

Joaquin answered 14/6, 2013 at 2:7 Comment(3)
Thanks @grizzlynetch, this is what I did at the end, but the problem is that I didn't have the object pointer to set as selected, only the id, because it comes from database. With the id works perfectly with ng-options but not with typeahead. So at the end I need to parse the complete list, check if the id match and then set as selected.Caviness
Have You tried something like storing objects in object instead of array and use id of the objects as key? E.g. var objects = {"5":{"id":"5","name":"obj #5",...},"13":{"id":"13","name":"obj #13",...}} then you can access the object directly by id objects["5"]Joaquin
awesome @GrizzlyNetch! this fixed my issue as well.Lema

© 2022 - 2024 — McMap. All rights reserved.