Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdminLTE3 - how to fix active sidebar #2068

Closed
jvbalcita opened this issue Feb 25, 2019 · 15 comments
Closed

AdminLTE3 - how to fix active sidebar #2068

jvbalcita opened this issue Feb 25, 2019 · 15 comments

Comments

@jvbalcita
Copy link

jvbalcita commented Feb 25, 2019

Environment:

  • AdminLTE Version: version 3 alpha 2
  • Operating System: All.
  • Browser (Version): All.

In the previous version of AdminLTE which is the Bootstrap 3 there is a fix to make the sidebar dynamically active when selected.

Here's the code used

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

but now in AdminLTE 3 this code is not working.
Is anyone already tried this? and produced a resolution?

Thanks in advance

@arbexmb
Copy link

arbexmb commented Mar 12, 2019

Hello @jvbalcita

Can you test this, please:

/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.nav-sidebar a').filter(function() {
    return this.href == url;
}).addClass('active');

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

@jvbalcita
Copy link
Author

it's working @arbex10 . Thank you very much!

@cristo89

This comment has been minimized.

@cristo89

This comment has been minimized.

@SupunSam
Copy link

SupunSam commented Oct 24, 2019

it's working @arbex10 . Thank you very much!

hey bro, this code paste where?sorry my English not good

You should place it bottom on your master layout HTML page or whatever the HTML page you're trying to have the ADMIN LTE Sidebar.

just open a simple <script></script> tag and put the code in there.

@renzo-tarazona07
Copy link

renzo-tarazona07 commented Oct 28, 2019

el código va dentro de un script en el pie de pagina antes de finalizar así:
this code insert into footer page...

    /** add active class and stay opened when selected */
    var url = window.location;

    // for sidebar menu entirely but not cover treeview
    $('ul.nav-sidebar a').filter(function() {
        return this.href == url;
    }).addClass('active');

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

@ashif9

This comment has been minimized.

@AshrafulRasel
Copy link

AshrafulRasel commented Mar 9, 2020

@arbexmb when blog/users this works but blog/users/3/edit for this url not working for menuopen

@GersonFD
Copy link

Hello @jvbalcita

Can you test this, please:

/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.nav-sidebar a').filter(function() {
    return this.href == url;
}).addClass('active');

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

It only works for the first element of second level, could you help me to work with the rest of levels?

@kpsanmao
Copy link

kpsanmao commented May 25, 2020

it's working @arbex10 . Thank you very much!

hey bro, this code ppaste where?sorry my english not good

放到基础模板或者每个页面的前面。

$(document).ready(function() {
  /** add active class and stay opened when selected */
  var url = window.location;

  // for sidebar menu entirely but not cover treeview
  $('ul.nav-sidebar a').filter(function() {
      return this.href == url;
  }).addClass('active');

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

你这典型的中式英语,茫茫多的英语中一眼就能看出来是国人!

@edward221096
Copy link

I searched too many documentation but @arbexmb code is working perfectly! Thank you!!

@JuanVqz
Copy link

JuanVqz commented Dec 13, 2020

I'm using rails 6 with turbolinks and webpacker this code works for me.

$(document).on("turbolinks:load", function() {
  var url = window.location

  $("ul.nav-sidebar a").filter(toggleNavSidebar);
  $("ul.nav-treeview a").filter(toggleNavTreeview);

  function toggleNavSidebar() {
    if (this.href != url) return

    $(this).addClass("active")
  }

  function toggleNavTreeview() {
    if (this.href != url) return

    $(this).parentsUntil(".nav-sidebar > .nav-treeview")
      .addClass("menu-open").prev("a").addClass("active");
  }
})

@GallitoMZ
Copy link

GallitoMZ commented May 24, 2021

@arbexmb when blog/users this works but blog/users/3/edit for this url not working for menuopen

I found a solution similar to the one mentioned:

`
/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.nav-sidebar a').filter(function() {
    if (this.href) {
        return this.href == url || url.href.indexOf(this.href) == 0;
    }


}).addClass('active');

// for treeview
$('ul.nav-treeview a').filter(function() {
    if (this.href) {
        return this.href == url || url.href.indexOf(this.href) == 0;
    }

}).parentsUntil(".nav-sidebar > .nav-treeview").addClass('menu-open').prev('a').addClass('active');

`

@yosaSoft
Copy link

Hello @jvbalcita

Can you test this, please:

/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.nav-sidebar a').filter(function() {
    return this.href == url;
}).addClass('active');

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

thank you it's ver helpful

@keserwang
Copy link

Hello @jvbalcita

Can you test this, please:

/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.nav-sidebar a').filter(function() {
    return this.href == url;
}).addClass('active');

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

This code should be placed in adminlte.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests