Want to limit the selection.
tahiriqbalnajam opened this issue · 1 comments
tahiriqbalnajam commented
Hi,
I tried to limit the selection on callback onOptionClick by checking length of selectVals but no success,
If you can add this in options that will be great.
nobleclem commented
No need to add as an option. Just a little work and it can be done with onOptionClick
like this:
$('select[multiple]').multiselect({
onOptionClick: function( element, option ) {
var maxSelect = 3;
// too many selected, deselect this option
if( $(element).val().length > maxSelect ) {
if( $(option).is(':checked') ) {
var thisVals = $(element).val();
thisVals.splice(
thisVals.indexOf( $(option).val() ), 1
);
$(element).val( thisVals );
$(option).prop( 'checked', false ).closest('li')
.toggleClass('selected');
}
}
// max select reached, disable non-checked checkboxes
else if( $(element).val().length == maxSelect ) {
$(element).next('.ms-options-wrap')
.find('li:not(.selected)').addClass('disabled')
.find('input[type="checkbox"]').attr( 'disabled', 'disabled' );
}
// max select not reached, make sure any disabled checkboxes are available
else {
$(element).next('.ms-options-wrap')
.find('li.disabled').removeClass('disabled')
.find('input[type="checkbox"]').removeAttr( 'disabled' );
}
}
});