AdminLTE icon indicating copy to clipboard operation
AdminLTE copied to clipboard

Active menu item with multi level submenus

Open lenamtl opened this issue 9 years ago • 35 comments

Hi, I'm using this code to add active class depending of the page.

This is working ok except for multi level submenus any clue how to fix this


/** 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;
}).closest('.treeview').addClass('active');


<li class="treeview">
	<a href="#">
	      <i class="fa fa-gears"></i>
	      <span>Settings</span>
		<i class="fa fa-angle-left pull-right"></i>
	</a>
		<ul class="treeview-menu">
			<li>
				<a href="#">
				      <i class="fa fa-bars"></i>
					Specials Settings
				</a>
				<ul class="treeview-menu">
					<li><a href="setting1.php">Setting 1</a></li>
				</ul>
			</li>
		</ul>
</li>

lenamtl avatar May 05 '17 14:05 lenamtl

I have fixed it by replacing the latest line. I have not test all possibles case so if there is an issue with a case let me know

/** 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( $( "ul.level-1" ) ).addClass('active');

lenamtl avatar May 10 '17 18:05 lenamtl

  1. nice script but it is not valid
  2. where you took the class ul.level-1 from? 😄 AdminLTE doesn't use something like this.

i fixed it to get it work on AdminLTE

/** 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');

i tested all variations till level 3 (all items) 😃

REJack avatar May 10 '17 21:05 REJack

Thanks, I didn't realized as it was working ... :) That could be great to have a label for 'snippet' so it's will be easier to search into the issue list.

lenamtl avatar May 11 '17 14:05 lenamtl

Note: The only problem with this solution is that only one page per menu / submenu item will work which is espected but I have a case that a page is opened by a click of a button on a page (not from the menu) when this occurred I loose active on the menu which is espected. So I will need to use a PHP solution instead of JS solution to add active class to the menu depending of the URL so I can check more than one url...

This is maybe possible with JS too I will do some test and let you know.

lenamtl avatar Aug 25 '17 14:08 lenamtl

It's not hard JS, you can use hash for this.

REJack avatar Sep 26 '17 16:09 REJack

Thanks for sharing this code @lenamtl. The modified code by @REJack works well. There are two issues though:

  1. There is flicker when I click on a new sub-menu. I think it closes & reopens the whole sub-menu. Is there a way to avoid that.
  2. How can I keep the left sidebar menu in its current state all the time (sub-menu open and active highlighted) till the user clicks something else on the menu. The moment I click something in the content area, the menu collapses. Were you able to get it working when a page is opened using a button etc @lenamtl.

Thanks

amsharma9 avatar Oct 12 '17 02:10 amsharma9

@amsharma9 Same problem here. The flicker is visually annoying I have noticed that there is no flicker on official website so i suppose they find a way to manage that https://adminlte.io/themes/AdminLTE/

legreco avatar Oct 16 '17 01:10 legreco

I think the reason official Live Preview website & document website doesn't have this issue is that it is made of HTML & Javascript. The left sidebar HTML code doesn't get refreshed. I use a MVC based PHP framework, that probably refreshes the whole page. I could be wrong in my understanding. I requested @almasaeed2010 to share the code of the website in #1661 but they haven't yet. He just pointed me to AdminLTE code, which I obviously have. I studied the website code using page sources of official website but didn't find any javascript to avoid the flicker.

amsharma9 avatar Oct 16 '17 02:10 amsharma9

I think they managed it in the backend see https://github.com/almasaeed2010/AdminLTE/issues/315

almasaeed2010 commented ; Hello, Although this might sound like a front-end job but it isn't. This should be implement as a back-end mechanism. This is because when the user loads a new page, the default setting will be applied and javascript will deal with the saved state after the page loads. Thus, causing unwanted transitions and animations (i.e., the sidebar would appear in its regular state for a moment before javascript applies the collapsed mode). This could be a front-end job if we were using Ajax or AngularJS since the only change is in the content page not the sidebar nor the navbar

legreco avatar Oct 16 '17 02:10 legreco

Hi @amsharma9

1- I'm using a older version with PHP (and savestate for sidebar and box) and lot of custom code, so maybe this is the reason I don't have the flicker issue.

2- Yes because the code is looking for the page url var url = window.location; this is a problem if the page is not in the menu or submenu link (that can be an edit page for example) we loose the menu active class when this occurred.

The only way I have found to fix this is to use PHP check page then add the active class to a menu, this way you can have a menu active for any page (different pages can set a menu active not only one)...

If there is another way using Javascript I will be happy to test it...

lenamtl avatar Oct 17 '17 16:10 lenamtl

I have fixed it now it will show only matching url as active rest as inactive

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().removeClass('active');

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

asayyed910 avatar Dec 05 '17 13:12 asayyed910

Thank you REJack.

jackthailand avatar Dec 07 '17 20:12 jackthailand

Hi,

I just tested your code modification and I don't see any difference, on my install it's acting the same .... Anyhow the important is that you have fixed your problem :)

lenamtl avatar Dec 08 '17 13:12 lenamtl

I was face same issue and i solve it by this solution

      $('#mainMenu ul li').find('a').each(function () {
            if (document.location.href == $(this).attr('href')) {
                $(this).parents().addClass("active");
                $(this).addClass("active");
                // add class as you need ul or li or a 
            }
        });

ashikjs avatar Dec 26 '17 06:12 ashikjs

I resolved it like this:

$('.sidebar-menu ul li').find('a').each(function () {
            var link = new RegExp($(this).attr('href')); //Check if some menu compares inside your the browsers link
            if (link.test(document.location.href)) {
                if(!$(this).parents().hasClass('active')){
                    $(this).parents('li').addClass('menu-open');
                    $(this).parents().addClass("active");
                    $(this).addClass("active"); //Add this too
                }
            }
        });

NGjata avatar Mar 28 '18 19:03 NGjata

@NGjata .. your script not work in single menu, than i add more line for works in both single menu and multiple menu. With this i resolved my menu..

// ----------------------------------------------------------------
var a = window.location.pathname;
$('.sidebar-menu > li > a[href="http://localhost' + a + '"').parent().addClass("active");
/* ----------------------------------------------------------------
 * just replace "http://localhost" with your server
 */ ----------------------------------------------------------------

$('.sidebar-menu ul li').find('a').each(function () {
            var link = new RegExp($(this).attr('href')); //Check if some menu compares inside your the browsers link
            if (link.test(document.location.href)) {
                if(!$(this).parents().hasClass('active')){
                    $(this).parents('li').addClass('menu-open');
                    $(this).parents().addClass("active");
                    $(this).addClass("active"); //Add this too
                }
            }
        });

cikal avatar Apr 10 '18 07:04 cikal

Hi @cikal could you explain your script please

lenamtl avatar Apr 10 '18 11:04 lenamtl

How about sidebar and topbar? (In case you put most used itens at top):

This code is just an complement from a previous solution in this page

/** 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');
  //Top bar
  $('ul.navbar-nav 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');

marceloagil avatar May 07 '18 17:05 marceloagil

Thank you @Agimovel your code is workly perfect !

AlifAbhiesa avatar Aug 07 '18 06:08 AlifAbhiesa

Hi @cikal. for me don't work single menu???

Ali12K avatar Oct 20 '18 14:10 Ali12K