Created
June 24, 2021 14:19
-
-
Save ozknozsrt/4237b756948d3e1adba4763045dc3a4d to your computer and use it in GitHub Desktop.
bootstrap-select (selectpicker) make collapsible with optgroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Selectpicker converts to collapsible when the inside has a optgroup | |
$('.selectpicker').on('shown.bs.select', function (e, clickedIndex, isSelected, previousValue) { | |
if ($(e.target).find("optgroup").length > 0) { | |
//.bootstrap-select .dropdown-menu .dropdown-header | |
$(e.target).closest(".bootstrap-select").find(".dropdown-menu .dropdown-header").click(function (e) { | |
e.stopImmediatePropagation(); | |
var $this = $(this), | |
$thisOpt = $this.attr("class").split(" ")[1], | |
$opt = $this.siblings("li").not(".dropdown-header, .dropdown-divider"); | |
$($opt).filter(function (i, item) { | |
return $(item).hasClass($thisOpt); | |
}).toggleClass("d-block"); | |
}); | |
// Hide option tags when init without first element | |
var $this = $(e.target).closest(".bootstrap-select").find(".dropdown-menu .dropdown-header"), | |
$opt = $this.siblings("li").not(".dropdown-header, .dropdown-divider"); | |
$($opt).filter(function (i, item) { | |
return $(item).hasClass("optgroup-1"); | |
}).addClass("d-block"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment