AdminLTE icon indicating copy to clipboard operation
AdminLTE copied to clipboard

Sidebar menu, highlighting currently selected menu

Open samoei opened this issue 12 years ago • 16 comments

It seems there is something am not getting right with the sidebar-menu. I can not get it to assign the active css class to the currently selected menu (or treeview menu in the case of tree). The dashboard menu is always active. I have gone through your app.js especially SIDEBAR MENU function (around line 555) but i cant see where your are assigning the currently selected menu a css active class. Kindly clarify for me. Thanks

samoei avatar Sep 05 '14 16:09 samoei

Hi, You have to add active class manually to the menu item html page

< li class="active" >

So you have to set it on every menu, if you duplicate the menu on every page.

It is not set dynamically, so if you embed the menu (include once the menu for all pages) you will need custom script to detect the actual opened page and add the active class dynamically

lenamtl avatar Sep 05 '14 17:09 lenamtl

@lenamtl is right you will have to use a custom plugin or manually by adding the active class to the elements.

almasaeed2010 avatar Sep 06 '14 18:09 almasaeed2010

@lenamtl @almasaeed2010 Thanks guys for the clarification. Cheers

samoei avatar Sep 06 '14 18:09 samoei

I've seen this done automatically with Jquery on standard bootstrap navbar items. Loop through the hrefs and if the link matches the current page then add the active class.

http://stackoverflow.com/questions/11533542/twitter-bootstrap-add-active-class-to-li

abaldwin99 avatar May 08 '15 20:05 abaldwin99

In Laravel I do like this: Route:

Route::group(['prefix' => 'person'], function(){
Route::get('{id}/Personal','UsersController@personal')->name('personal');
});

Sidebar-menu

 <ul class="sidebar-menu">
          <li class="treeview {!! Request::is('person/*/Personal') ? 'active' : '' !!}">
                <a href="{!!route('personal', $user)!!}">
                    <i class="fa fa-briefcase"></i>&nbsp;<span>Persona Example</span>
                </a>
        </li>
</ul>

alpacin0 avatar Mar 07 '16 21:03 alpacin0

here is my solution:

//Enable sidebar dinamic menu
$.AdminLTE.dinamicMenu();
/* DinamicMenu()
     * dinamic activate menu
     */
    $.AdminLTE.dinamicMenu = function() {
        var url = window.location;
        // Will only work if string in href matches with location
        $('.treeview-menu li a[href="' + url + '"]').parent().addClass('active');
        // Will also work for relative and absolute hrefs
        $('.treeview-menu li a').filter(function() {
            return this.href == url;
        }).parent().parent().parent().addClass('active');
    };

coscho avatar Dec 01 '16 16:12 coscho

For Mutltilvel /* DinamicMenu() * dinamic activate menu */ $.AdminLTE.dinamicMenu = function () { var path = $(location).attr('pathname'); // Will only work if string in href matches with location $('.treeview-menu li a[href="' + path + '"]').parents().addClass('active'); };

TintinFr avatar Dec 22 '16 12:12 TintinFr

Hi, you can see my solution with JS and adapt it. #1482

lenamtl avatar Aug 25 '17 14:08 lenamtl

hi , i am using this Theme in my project , but when the page is load it shows the menu item for the first time . When i clicked the dashboard item , it is not opened later .

After Reload the page for the first time after reload when i clicked the pull container right option it is clicked but menu dashboard item is not shown -- image

here is my code --

image

please help me to solve this problem

Iqbal487 avatar Oct 05 '18 16:10 Iqbal487

In Laravel I do like this: Route:

Route::group(['prefix' => 'person'], function(){
Route::get('{id}/Personal','UsersController@personal')->name('personal');
});

Sidebar-menu

 <ul class="sidebar-menu">
          <li class="treeview {!! Request::is('person/*/Personal') ? 'active' : '' !!}">
                <a href="{!!route('personal', $user)!!}">
                    <i class="fa fa-briefcase"></i>&nbsp;<span>Persona Example</span>
                </a>
        </li>
</ul>

Thanks buddy :smiley:

dineshsuthar92 avatar Nov 28 '18 12:11 dineshsuthar92

For laravel, I don't play with blade, I use jQuery, just like this comment: https://github.com/almasaeed2010/AdminLTE/issues/1482#issuecomment-349304120 that worked to me.

neoacevedo avatar Feb 22 '19 20:02 neoacevedo

This code will helpful to multiple multilevel in side menu...

$(document).ready(function(){
                /** add active class and stay opened when selected */
                var url = window.location;
                // for sidebar menu entirely but not cover treeview
                $('ul.sidebar-menu a').filter(function() {
                     return this.href == url;
                }).parent().addClass('active');

                // for treeview
                $('ul.treeview-menu a').filter(function() {
                     return this.href == url;
                }).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');
            });

manikandanjmk avatar Apr 29 '19 12:04 manikandanjmk

$.AdminLTE.dinamicMenu = function() { var url = window.location; // Will only work if string in href matches with location $('.treeview-menu li a[href="' + url + '"]').parent().addClass('active'); // Will also work for relative and absolute hrefs $('.treeview-menu li a').filter(function() { return this.href == url; }).parent().parent().parent().addClass('active'); };

not working i have added this to my js file still not worked let me know how to apply this

Love9056 avatar May 29 '19 07:05 Love9056

This code will helpful to multiple multilevel in side menu...

$(document).ready(function(){
                /** add active class and stay opened when selected */
                var url = window.location;
                // for sidebar menu entirely but not cover treeview
                $('ul.sidebar-menu a').filter(function() {
                     return this.href == url;
                }).parent().addClass('active');

                // for treeview
                $('ul.treeview-menu a').filter(function() {
                     return this.href == url;
                }).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');
            });

Where to apply this. Kindly help. I am using this template as masterpage in asp.net web forms.

DEVRam86 avatar Aug 03 '19 09:08 DEVRam86

In my case, none of the above was not working.

This is what I have done to set the multilevel menu selected.

var pageUrl = window.location.pathname; var menu = $('.sidebar .sidebar-menu li a[href="'+pageUrl+'"]').parent('li'); menu.addClass('active');

var treeLi = menu.parent().parent(); if (treeLi.hasClass('treeview')) treeLi.addClass('active');

muhammad-fahd avatar Oct 02 '20 10:10 muhammad-fahd