corejavascript / corejavascript/typeahead.js

Using non displayed dataset values

Open
#175 3 comments 0 reactions 0 assignees View on GitHub
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: [
'

',
'Unable to find a match to what has been entered.',
'
'
].join('\n'),
/*suggestion: function(obj) {
return '
' + obj.name + ' - ' + obj.id + '
';
}*/
}
}).bind('typeahead:change', function(ev, suggestion) {
console.log('Selection: ', suggestion);
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.