Skip to content

Instantly share code, notes, and snippets.

@Exadra37
Last active February 11, 2020 17:02
Show Gist options
  • Save Exadra37/6b36bc5ee185c55f9ed8 to your computer and use it in GitHub Desktop.
Save Exadra37/6b36bc5ee185c55f9ed8 to your computer and use it in GitHub Desktop.
Allow only 1 Selection by Group when using multiselect in option groups boxs with plugin https://github.com/silviomoreto/bootstrap-select
<!-- http://silviomoreto.github.io/bootstrap-select/ -->
<select id="compare" class="selectpicker" data-style="btn-success" data-max-options="2" title="Compare Settings" data-width="25em" multiple>
<optgroup class="multiple-options" label="Compare" data-max-options="1">
<option class="exadra37-1-selection-by-group exadra37-group-compare" title="Compare Daily" selected>Daily</option>
<option class="exadra37-1-selection-by-group exadra37-group-compare" title="Compare Weekly">Weekly</option>
<option class="exadra37-1-selection-by-group exadra37-group-compare" title="Compare Monthly">Monthly</option>
</optgroup>
<optgroup class="multiple-options" label="Against" data-max-options="1">
<option class="exadra37-1-selection-by-group exadra37-group-against" title="Against Previous Week" selected>Previous Week</option>
<option class="exadra37-1-selection-by-group exadra37-group-against" title="Against Last Year">Last Year</option>
</optgroup>
</select>
<!-- END -->
/**
* @author Paulo Silva(Exadra37) <exadra37atgmailpointcom>
* @package Exadra37\BootstrapSelect
* @version 1.0.0
* @since 29/07/2014 - v.1.0.0
* @link http://silviomoreto.github.io/bootstrap-select/
*/
$(function() {
$('.selectpicker').selectpicker();
// Override default behavior of plugin to allow only 1 selection by Group when using multi-select
// Caveats:
// - Once the plugin don't copy data-type attribute or any other one i end up using dummy class names to achieve the desired result
// - The dummy class names have "special" names in order to try to avoid conflicts with classes used in css
$('.selectpicker').data('selectpicker').$menu.on('click', 'li a', function(e) {
var selectOnlyOneOptionByGroup = $(this).hasClass('exadra37-1-selection-by-group');
// Only override the one declared in the html with dummy class "exadra37-1-selection-by-group"
if(selectOnlyOneOptionByGroup) {
var isClickTriggeredClass = $(this).hasClass('exadra37-click-triggered');
var optionGroupClickedClass = $(this).attr('class');
// only apply the action to the option we have clicked
// will not apply the action to the clicks triggered inside of this if
// achieved by injecting a dummy class "exadra37-click-triggered" in the trigered event
if (!isClickTriggeredClass) {
var lisSelected = $(this).parent('li').siblings('.selected');
// loop all parent siblings that have class "selected" and remove it by triggering the click event on it
// if we only use the jquery method remove class, the button value will not update properly
$.each(lisSelected, function() {
var optionGroupToTriggerClass = $(this).children('a').attr('class');
// only apply the action to the parent siblings that belongs to the same group
if (optionGroupToTriggerClass == optionGroupClickedClass) {
// apply a dummy class so that we only go inside this if this class is not present
// trigger the click event on all other parent siblings
$(this).children('a').addClass('exadra37-click-triggered').trigger('click');
};
});
} else {
// remove the dummy class once we don't need it anymore
$(this).removeClass('exadra37-click-triggered');
}
}
});
});
@golikovplus
Copy link

Great! It was helpful! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment