I'm trying to change the source of typeahead, but all them answers I found didn't work for me (probably because of a newer bootstrap version). I made a backend search based on what the user searches. Here is my code:
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1,
limit: 2,
},
{
name: 'organizations',
source: substringMatcher(getOrganizationData())
}).on("input", function(e) {
organizationQuery = e.target.value;
// Here I want to update the source
// Not working:
//$(".typeahead").data('typeahead').source = substringMatcher(getOrganizationData())
// Not working:
//$('.typeahead').data('ttTypeahead').dropdown.datasets[0].source = substringMatcher(getOrganizationData())
// Not working:
// var autocomplete = $('input').typeahead();
// autocomplete.data('typeahead').source = substringMatcher(getOrganizationData());
});
And here is my getOrganizationData() method:
function getOrganizationData()
{
var tempResults = [];
$.getJSON( "search-organization?query="+organizationQuery, function( data ) {
$.each(data, function (key, val) {
var display = val.coc_number + ", " + val.name
tempResults[key] = display;
organizationHolder[display] = val.id;
});
});
return tempResults;
}
How am I supposed to find results based on what I type if I can't update the source? Thanks in advance!