New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collapse/expand optgroups #730
Comments
you can subscribe to the "open" event which is now called after the dropdown is opened and use jquery to modify the dom to create expand collapse twisties. |
For what it's worth after a year, I needed to do this recently so here's how I did it. In your CSS, hide the sub results by default: .select2-result-sub > li.select2-result {
display: none;
} Then add this to your Javascript file: $('.select2-results').on('click', 'li', function(){
$(this).find('li').show();
}); Although the optgroup has its own Select 2 class, attaching an event handler on either of them (.select2-result-with-children or .select2-result-label) did not work for me. |
add the function under below. $(document).ready is not working $(window).load(function() {
$('.select2-results').on('click', 'li', function(){
if($(this).find('li').is(":visible"))
$(this).find('li').hide();
else
$(this).find('li').show();
});
}); |
|
@pirasterize Great! |
Hi guys, This is my solution, Hope it's useful. |
Toggling works, but if you keep the dropdown open after a selection( |
This would be a nice option to add to the core library. Adding collapsed options on the open event does work, but the scrolling behavior (when the dropdown is reopened after one or more values have been selected) is confusing and much more difficult to override. |
It's not an issue - just a feature request.
I think, adding support for expandable optgroups can be useful in some situations. With this feature enabled we'll see only optgroup labels at first. And after clicking the optgroup header it will expand to show all it's options
The text was updated successfully, but these errors were encountered: