I ended up solving this by altering the chosen plugin and adding the following code to AbstractChosen.prototype.keyup_checker
:
case 65:
if (evt.ctrlKey && this.results_showing) {
if (typeof onSelectAll == 'function') {
onSelectAll($(this.container).attr('id'), $(this.form_field).attr('id'));
this.results_hide();
}
return true;
}
Basically, if Ctrl-A is pressed while the Chosen select is open, it will call a user-defined delegate onSelectAll
with the Chosen container's id and the underlying select's id as arguments. If the Ctrl key is not pressed, I want this to fall through to the default, which allows search string entry to work as usual.
Back on my page I have the following:
function onSelectAll(containerId, selectId) {
var ids = [];
$("#" + containerId).find('.active-result').each(function () { ids.push(parseInt($(this).attr('data-option-array-index'))); });
$(ids).each(function () { $($('#' + selectId + ' option')[this]).attr('selected', 'selected'); });
$('#' + selectId).trigger('chosen:updated');
}
This gets the array indexes of the visible results in the Chosen container, marks them as selected in the underlying select control, and then causes the Chosen container to update.
I am aware it's not entirely kosher to change the meaning of a standard keyboard shortcut, but in an informal poll of the three other devs most likely to actually use this thing, they all suggested it, so I think I'm in the clear for now. That said, suggestions to improve and clean up my javascript are quite welcome - I've been doing backend and SQL stuff for so long I've totally lost my UI chops.