corejavascript / corejavascript/typeahead.js
Using non displayed dataset values
- Dominant language
- JavaScript
- Stars
- 968
- Forks
- 231
- PR merge metrics
- No merged PRs in 30d
Description
Does anyone know if there is a way to use the change event and get a different value from a dataset?
For example, using Bloodhound I am getting a JSON dataset. Each object contains 2 elements. Something like this:
```
[
{"id": "00002","name": "2"},
{"id": "00003","name": "THREE"},
{"id": "10011","name": "test1"}
]
```
The 'name' value is being displayed just fine, but I need to send the 'id' back to the server to be saved into a database. Full code is below.
```
var data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'data.json',
remote: {
url: 'data.json'
}
});
$('.inputobj').typeahead({
hint: true,
highlight: true,
minLength: 1,
},
{
name: 'typeahead',
display: 'name',
source: data,
limit: 10,
templates: {
empty: [
'
].join('\n'),
/*suggestion: function(obj) {
return '
}*/
}
}).bind('typeahead:change', function(ev, suggestion) {
console.log('Selection: ', suggestion);
});
```
Contributor guide
Assessment
This issue has not been assessed yet.