I'm fetching user IDs and names via AJAX and using Select2 to search through them, but my users have requested the ability to select from the typeahead dropdown by pressing Tab, effectively treating it like pressing Enter. Here is my select2 declaration:
$("#user-select").select2({
ajax: {
url: "/api/User",
method: "get",
data: function (params) {
return {
search: params.term
};
},
beforeSend: function () {
$(".loading-results").text("Loading...");
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
allowClear: true,
placeholder: "Enter a User ID or Name",
templateResult: function (data) {
return "(" + data.id + ") " + data.name;
},
templateSelection: function (data) {
return "(" + data.id + ") " + data.name;
}
".select2-search__field" seems to be the focused element whenever the dropdown's visible, and the highlighted element gets the class "select2-results__option--highlighted".
I've tried a few solutions, but nothing seems to have worked, especially because this element appears and disappears anytime the dropdown opens. Unfortunately I lost the code from my attempts, but they consisted mainly of doing preventDefault when Tab is hit on the focused input and then triggering a click event on the highlighted element or triggering the enter key on the input.
I also tried adjusting the selectOnClose option, but that seems buggy in general and caused an endless loop when I had it running normally, much less trying to override it based on what key is being pressed.
[Edit]
The selected solution works, but doesn't account for the templateResult specified, instead showing "() undefined". So, I tweaked it to add the highlighted answer as the selected Option for the overlying Select, and then call the change event right on that Select.
...(Same as initial select2)
}).on('select2:close', function (evt) {
var context = $(evt.target);
$(document).on('keydown.select2', function (e) {
if (e.which === 9) { // tab
var highlighted = context.data('select2').$dropdown.find('.select2-results__option--highlighted');
if (highlighted) {
var data = highlighted.data('data');
var id = data.id;
var display = data.name;
$("#user-select").html("<option value='" + id + "' selected='selected'>" + display + "</option>");
$("#user-select").change();
}
else {
context.val("").change();
}
}
});
I've been trying to find a solution to this problem as well.
The main issue is that the select2 events doesn't provide any insight as to which key was pressed.
So I've come up with this hack to access the keydown event inside a select2 context.
I've been testing it to the best of my abilities and it seems to work perfectly.
selectElement
.select2({ options ... })
.on('select2:close', function(evt) {
var context = $(evt.target);
$(document).on('keydown.select2', function(e) {
if (e.which === 9) { // tab
var highlighted = context
.data('select2')
.$dropdown
.find('.select2-results__option--highlighted');
if (highlighted) {
var id = highlighted.data('data').id;
context.val(id).trigger('change');
}
}
});
// unbind the event again to avoid binding multiple times
setTimeout(function() {
$(document).off('keydown.select2');
}, 1);
});