I create a form using Typeahead. I have two input fields next to each other, and I need autocomplete on each of them. My HTML looks like this:
<select id="numerator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="numeratorId" class="typeahead" type="text" />
<br/>
<select id="denominator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="denominatorId" class="typeahead" type="text" />
Each of the fields inputwill be autocompleted by viewing the API endpoint. It should look like /api/1.0/code?type=presentation&code=123or /api/1.0/code?type=chemical&code=123.
The parameter value typein the API call should depend on the value of the element <select>next to each input field.
The problem I am facing is that I do not know how to tell Bloodhound what should be a parameter type.
Ideally, I would like to pass it to Bloodhound, but I do not know how to do it. This is my JavaScript:
var bnfMatches = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/1.0/code?',
replace: function(url, uriEncodedQuery) {
url = url + 'code=' + uriEncodedQuery;
val = $('#numerator').val();
if (!val) return url;
return url + '&code_type=' + encodeURIComponent(val)
}
}
});
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
name: 'states',
displayKey: 'id',
source: bnfMatches.ttAdapter()
});
I would be very grateful for any suggestions.