Collapsible Lists With Bootstrap: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 5: | Line 5: | ||
<div class="panel-group"> | <div class="panel-group"> | ||
<div class="panel panel-default"> | <div class="panel panel-default"> | ||
<div class="panel-heading" role="tab" id="collapse-list-heading- | <div class="panel-heading" role="tab" id="collapse-list-heading-{{group.id}}"> | ||
<h2 class="panel-title" | <h2 class="panel-title">{{ group.name }} <button class="btn btn-default btn-xs btn-toggle-tutorial-list" type="button" data-toggle="collapse" data-target="#collapse-list-{{group.id}}" aria-expanded="false" aria-controls="collapse-list-{{group.id}}" title="toggle tutorial group"><span class="glyphicon glyphicon-chevron-down"></span></button></h2> | ||
</div> | </div> | ||
<div class="panel-body" | <div class="panel-body">{{ tutorials.count }} tutorial{% if tutorials.count != 1 %}s{% endif %}</div> | ||
<div class="panel-collapse collapse" id="collapse-list- | <div class="panel-collapse collapse" id="collapse-list-{{ group_id }}" role="tabpanel" aria-labelledby="collapse-list-heading-{{group_id}}" aria-expanded="false"> | ||
<ul class="list-group"> | <ul class="list-group"> | ||
<li class="list-group-item container-fluid">...</li> | <li class="list-group-item container-fluid">...</li> | ||
| Line 19: | Line 19: | ||
</div><!--end .panel--> | </div><!--end .panel--> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Bootstrap provides all the JavaScript to expand and collapse the list once the required classes and attributes are set. | |||
Assigning the `aria-expanded="false"` attribute will cause the list to be collapsed initially. | |||
This doesn't necessarily have to be contained in a `.panel-group` container. | This doesn't necessarily have to be contained in a `.panel-group` container. | ||
Latest revision as of 12:22, 10 February 2015
HTML[edit]
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="collapse-list-heading-{{group.id}}">
<h2 class="panel-title">{{ group.name }} <button class="btn btn-default btn-xs btn-toggle-tutorial-list" type="button" data-toggle="collapse" data-target="#collapse-list-{{group.id}}" aria-expanded="false" aria-controls="collapse-list-{{group.id}}" title="toggle tutorial group"><span class="glyphicon glyphicon-chevron-down"></span></button></h2>
</div>
<div class="panel-body">{{ tutorials.count }} tutorial{% if tutorials.count != 1 %}s{% endif %}</div>
<div class="panel-collapse collapse" id="collapse-list-{{ group_id }}" role="tabpanel" aria-labelledby="collapse-list-heading-{{group_id}}" aria-expanded="false">
<ul class="list-group">
<li class="list-group-item container-fluid">...</li>
<li class="list-group-item container-fluid">...</li>
<li class="list-group-item container-fluid">...</li>
</ul>
</div><!--end .panel-collapse-->
</div><!--end .panel-->
</div><!--end .panel-->
Bootstrap provides all the JavaScript to expand and collapse the list once the required classes and attributes are set.
Assigning the aria-expanded="false" attribute will cause the list to be collapsed initially.
This doesn't necessarily have to be contained in a .panel-group container.
- Header
- Target the heading element with `#collapse-list-heading-{{group.id}}
.
- Target the heading element with `#collapse-list-heading-{{group.id}}
Button.btn-toggle-tutorial-listis the button's selector for the purposes of custom jQuery. This is to update the button's icon depending on the state of the list (collapsed or expanded); it doesn't have anything to do with the needs of Bootstrap.type="button"prevents the button from acting as a submit button.data-target="#collapse-list-{{group.id}}"Element to target.aria-controls="collapse-list-{{group.id}}"is for the purposes of acccessibility.aria-expanded="false"allows jQuery to test the collapsed state of the list.
Listingsclass="panel-collapse collapse"Bootstrap classes for the collapsing element. Again,.panel-collapseis in there just because this is a panel.id="collapse-list-{{ group_id }}": target for the collapsible element; corresponds to thedata-targetattribute of the button control.aria-labelledby="collapse-list-heading-{{group_id}}": Accessibility aid.aria-expanded="false": Hook to test state of the collapsible element.Whatever is placed inside of the.collapseelement will be hidden while the element is collapsed. In this case it's a.list-group`.
jQuery to toggle button icon[edit]
toggleCollapseButton: function() {
if ($(this).attr('aria-expanded')==='true') {
$('span.glyphicon', $(this)).switchClass('glyphicon-chevron-up', 'glyphicon-chevron-down');
}
else {
$('span.glyphicon', $(this)).switchClass('glyphicon-chevron-down', 'glyphicon-chevron-up');
}
}