I am setting up a form with Typeahead. I have two input fields next to each other, and I need an 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 input
fields will autocomplete by looking at an API endpoint. This should be of the form /api/1.0/code?type=presentation&code=123
or /api/1.0/code?type=chemical&code=123
.
The value of the type
parameter in the API call should depend on the value of the <select>
element next to each input field.
The problem that I'm having is that I don't know how to tell Bloodhound what the type
parameter should be.
Ideally I'd like to pass it in to Bloodhound, but I don't know how to do that. 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;
// How to change this to denominator for denominator queries?
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'd be very grateful for any suggestions.
input
elements have sameid
?numeratorId
? – Suffuseid
's ofinput
elements, addedbnfMatches.initialize()
, selectedselect
element havingid
beginning with focusedinput
elementid
. – Suffuse