twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.21k forks source link

Using typeahead to map input string to datum #609

Open HaroldPutman opened 10 years ago

HaroldPutman commented 10 years ago

I am trying to use typeahead to pick from a finite list matching human-readable strings to "keys". For example a list of Countries. I want my form to be able to get the ISO country code based on the country name the user types in. If a user autocompletes or selects 'Netherlands Antilles' I can pick the ISO code 'AN' from the datum passed to the autocompleted, or selected event. However if they simply type the whole word 'Chile' without autocomplete I don't know how to access the matching datum to get the ISO code 'CL'.

The other problem is if they autocomplete 'Chile' and then press (backspace, backspace,N,A) the input field now says China, but if they don't autocomplete, I'm not sure how to tell that the previous autocomplete is no longer valid.

It seems like this could be solved if there were a way to ask "what was the datum for the last hint shown" or maybe better to programmatically pass the typed value into the Dataset Query and ask "What datum would you suggest for this?"

Another viable option might be to allow an option to "autoselect" on blur. Probably also need a way to ask the typeahead if it has a valid match so I can know not to accept 'Elbonia'.

Svakinn commented 10 years ago

Check out this example: https://github.com/Svakinn/typeahead.js/blob/typeaheadSimple/Examples.md You use the valueKey for the iso code and the nameKey for the country name. Then you can use the restrictInputToDatum option to make sure that valid country is selected.

Note: this is from a Fork of typeahead but I think the nameKey option is supposed to be available in the upcomming v0.10.0 version.

HaroldPutman commented 10 years ago

yes! restrictInputToDatum is exactly what I need. typeahead:noSelect is awesome too.

Hopefully these will make it into the "official" typeahead.js