Here I want to create text box, which can show a matching list of customers after 3 input letters. I have used twitter typeahead with remote plugin. Whenever I enter 3 letters, it started showing a blank box but it doesn't contain any kind of list. Do I miss anything? Please help me out !! My markup and jQuery are as below...
<form>
<div class="form-group">
<label>Customer</label>
<input id="customer" type="text" value="" class="typeahead form-control" />
</div>
<div class="form-group">
<label>Movies</label>
<input type="text" value="" class="form-control" />
</div>
<button class="btn btn-primary">Submit</button>
</form>
<script>
$(document).ready(function () {
var viewmodel = {};
var customers = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/customers?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#customer').typeahead({
minLength: 3,
highlight: true
}, {
name: 'customers',
display: 'name',
source: customers
}).on("typeahead:select", function (e, customer) {
viewmodel.customerId = customer.id;
});
});
</script>
Twitter typeahead hasn't been updated for more than five years and its broken with new Bootstrap (4/5) and JQuery library. I suggest you to use JQuery Typeahead which is more recent and features complete. Here is a working example:
Of course you can get data from an Ajax API call using the source option from the plugin.