Voltstriker Voltstriker - 6 months ago 9
jQuery Question

How do I modify my code so that my lists operate as accordions

I am still getting to know bootstrap. How do I modify my code so that clicking on the collapsible fields will collapse the other fields in the

<li>
tag (accordion). I understand that you have to use the
data-parent
value, however, when experimenting I was unable to get it to collapse.

Example: When clicking on the 'Users' link, close the 'Units of Study' and 'Enrolments' sections

<li>
<a class="admin collapsed sidebar-parent" data-toggle="collapse" href="#sidebar-admin" style="border-top: none;"><i class="fa fa-wrench" style="margin-right: 17px; margin-left: 1px;"></i>Admin</a>
<ul id="sidebar-admin" class="nav collapse">
<li>
<!-- Users -->
<a class="users collapsed sidebar-subparent" data-toggle="collapse" href="#sidebar-users"> &nbsp; &nbsp;<i class="fa fa-user" style="margin-right: 15px; margin-left: 2px;"></i>Users</a>
<ul id="sidebar-users" class="nav collapse">
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/Users")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-search" style="margin-right: 8px;"></i> Search Users</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/NewUser")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-plus-circle" style="margin-right: 12px;"></i> Add Users</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/Roles")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-users" style="margin-right: 10px; margin-left: -1px;"></i> Assign Roles</a>
</li>
</ul>

<!-- Units -->
<a class="unitsofstudy collapsed sidebar-subparent" data-toggle="collapse" href="#sidebar-units"> &nbsp; &nbsp;<i class="fa fa-university" style="margin-right: 12px;"></i>Units of Study</a>
<ul id="sidebar-units" class="nav collapse">
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/Units")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-search" style="margin-right: 8px;"></i> Search Units</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/NewOfferedUnit")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-plus-circle" style="margin-right: 10px;"></i> Register Units</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/NewUnit")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-plus-circle" style="margin-right: 10px;"></i> Register Semester</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/NewSemester")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-plus-circle" style="margin-right: 10px;"></i> Offer Units</a>
</li>
</ul>

<!-- Enrolments -->
<a class="enrolments collapsed sidebar-subparent" data-toggle="collapse" href="#sidebar-admenrolments"> &nbsp; &nbsp;<i class="fa fa-graduation-cap" style="margin-right: 12px; margin-left: -2px;"></i>Enrolments</a>
<ul id="sidebar-admenrolments" class="nav collapse">
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/Enrolments")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-search" style="margin-right: 8px;"></i> Search Enrolments</a>
</li>
<li class="sidebar-element">
<a href="@Url.Content("~/Admin/NewEnrolment")"> &nbsp; &nbsp; &nbsp; &nbsp;<i class="fa fa-plus-circle" style="margin-right: 10px;"></i> Enrol Students</a>
</li>
</ul>
</li>
</ul>
</li>

Answer

Accordion behavior is dependent on the panel class (http://getbootstrap.com/javascript/#collapse-options). So the immediate child of the parent #sidebar-admin must be a .panel..

http://www.codeply.com/go/Zq1utEY3dV

<ul id="sidebar-admin" class="nav collapse">
        <li class="panel">
            <a class="users collapsed sidebar-subparent" data-toggle="collapse" data-parent="#sidebar-admin" href="#sidebar-users"> &nbsp; &nbsp;<i class="fa fa-user" style="margin-right: 15px; margin-left: 2px;"></i>Users</a>
            <ul id="sidebar-users" class="nav collapse">
                <li class="sidebar-element">
                   ...