Here's my typeahed html and js codes:
HTML
<input id="student" name="student" required data-rule-validStudent="true" type="text" value="" class="form-control" />
TypeAhead code:
section scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function() {
var vm = {
bookIds: []
};
var students = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('firstName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/students?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#student').typeahead({
minLength: 2, //typeahead will query the server when user types atleast 3 letters
highlight: true, // the letters in the search result that match our query will be bold
},
{
name: 'students',
display: function (item) { return item.firstName + ' ' + item.middleName + ' ' + item.lastName + ' ' + '('+item.id+ ')' },
source: students.ttAdapter()
}).on("typeahead:select",
function(e, student) {
vm.studentId = student.id;
});
}
});
</script>
}
basically, I have an input field which is supposed to display firstname, middlename and last name. Typeahead does an auto complete for firstname only. It does fetch the correct records and why type in the first name, it does give me the suggestion for the full name. However, When I continue typying the middle name, auto complete does not work. Is there something I am missing in my code ?
I finally fixed this issue by creating a variable called FullName which is a concatenation of first name, middle name and last name. Then, I used it as a datumtokenizer instead of just first name.