I am using the Bootstrap Typeahead plugin as follows:
$('#Organisation').typeahead({
source: function (query, process) {
return $.get(AppURL + 'Organisations/Manage/SearchByName/',
{
term: query
},
function (data) {
var results = [];
var fullResults = [];
$.each(data, function (index, item) {
results.push(item.label);
fullResults.push({
id: item.ID,
label: item.label
});
});
return process(results);
});
},
updater: function (selection) {
$('#OrganisationID').val(selection);
}
});
I have two arrays returned, this is because Typeahead ONLY wants a list of strings instead of an object. The second array contains both a label and an identifier.
As soon as the user selects an item from the list, I need to get the identifier from this selection, and then use it to insert into the hidden field. But the choice will be just a name, since the identifier cannot be passed through Typeahead.
Any ideas on how I can solve this?
An example of two returned arrays:
Results: ["Organisation 1","Organisation 2"]
fullResults: [{"label":"Organisation 1","ID":2},{"label":"Organisation 2","ID":1}]
source
share