Collapsible Lists With Bootstrap

From Littledamien Wiki
Revision as of 12:17, 10 February 2015 by Video8 (talk | contribs)
Jump to navigation Jump to search

HTML

<div class="panel-group">
			<div class="panel panel-default">
				<div class="panel-heading" role="tab" id="collapse-list-heading-<nowiki>{{group.id}}</nowiki>">
					<h2 class="panel-title"><nowiki>{{ group.name }}</nowiki> <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-<nowiki>{{group.id}}</nowiki>" title="toggle tutorial group"><span class="glyphicon glyphicon-chevron-down"></span></button></h2>					
				</div>
				<div class="panel-body"><nowiki>{{ tutorials.count }}</nowiki> tutorial{% if tutorials.count != 1 %}s{% endif %}</div>
				<div class="panel-collapse collapse" id="collapse-list-<nowiki>{{ group_id }}</nowiki>" role="tabpanel" aria-labelledby="collapse-list-heading-<nowiki>{{group_id}}</nowiki>" 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-->

This doesn't necessarily have to be contained in a .panel-group container.

  • Header
    • Target the heading element with `#collapse-list-heading-{{group.id}}.
  • Button
    • .btn-toggle-tutorial-list is 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.
  • Listings
    • class="panel-collapse collapse" Bootstrap classes for the collapsing element. Again, .panel-collapse is in there just because this is a panel.
    • id="collapse-list-{{ group_id }}": target for the collapsible element; corresponds to the data-target attribute 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 .collapse element will be hidden while the element is collapsed. In this case it's a .list-group`.

jQuery to toggle button icon

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');
	}
}