Collapsible Lists With Bootstrap: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Category:Bootstrap Category:jQuery Category:JavaScript Category:Web Development <syntaxhighlight lang="twig" enclose="div" highlight="3,4,7"> <div class="pan...") |
No edit summary |
||
| Line 1: | Line 1: | ||
[[Category:Bootstrap]] [[Category:jQuery]] [[Category:JavaScript]] [[Category:Web Development]] | [[Category:Bootstrap]] [[Category:jQuery]] [[Category:JavaScript]] [[Category:Web Development]] | ||
== HTML == | |||
<syntaxhighlight lang="twig" enclose="div" highlight="3,4,7"> | <syntaxhighlight lang="twig" enclose="div" highlight="3,4,7"> | ||
<div class="panel-group"> | <div class="panel-group"> | ||
| Line 34: | Line 36: | ||
** `aria-expanded="false"`: Hook to test state of the collapsible element. | ** `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`. | ** 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 == | |||
<syntaxhighlight lang="javascript" enclose="div"> | |||
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'); | |||
} | |||
} | |||
</syntaxhighlight> | |||
Revision as of 12:17, 10 February 2015
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}}
.
- 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
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');
}
}