ColorlibHQ / AdminLTE

AdminLTE - Free admin dashboard template based on Bootstrap 5
https://adminlte.io
MIT License
44k stars 18.17k forks source link

How can I ignore the click on a tree item that is already active? #1314

Closed rodriguezfa closed 7 years ago

rodriguezfa commented 7 years ago

Hello All, How can I implement a check on a "treeview-menu" item that already has the class="active" set and ignore the click on it?

Thanks Francisco

rodriguezfa commented 7 years ago

Modified the app.js with a check at the bottom to get the result I wanted. Can this be implemented as an option in AdminLTE or as default?

/* Tree()
   * ======
   * Converts the sidebar into a multilevel
   * tree view menu.
   *
   * @type Function
   * @Usage: $.AdminLTE.tree('.sidebar')
   */
  $.AdminLTE.tree = function (menu) {
    var _this = this;
    var animationSpeed = $.AdminLTE.options.animationSpeed;
    $(document).off('click', menu + ' li a')
      .on('click', menu + ' li a', function (e) {
        //Get the clicked link and the next element
        var $this = $(this);
        var checkElement = $this.next();

        //Check if the next element is a menu and is visible
        if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible')) && (!$('body').hasClass('sidebar-collapse'))) {
          //Close the menu
          checkElement.slideUp(animationSpeed, function () {
            checkElement.removeClass('menu-open');
            //Fix the layout in case the sidebar stretches over the height of the window
            //_this.layout.fix();
          });
          checkElement.parent("li").removeClass("active");
        }
        //If the menu is not visible
        else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
          //Get the parent menu
          var parent = $this.parents('ul').first();
          //Close all open menus within the parent
          var ul = parent.find('ul:visible').slideUp(animationSpeed);
          //Remove the menu-open class from the parent
          ul.removeClass('menu-open');
          //Get the parent li
          var parent_li = $this.parent("li");

          //Open the target menu and add the menu-open class
          checkElement.slideDown(animationSpeed, function () {
            //Add the class active to the parent li
            checkElement.addClass('menu-open');
            parent.find('li.active').removeClass('active');
            parent_li.addClass('active');
            //Fix the layout in case the sidebar stretches over the height of the window
            _this.layout.fix();
          });
        }
        //if this isn't a link, prevent the page from being redirected
        if (checkElement.is('.treeview-menu')) {
          e.preventDefault();
        }
        else
        {
            //Get the parent li and check if it's already active.
            var parent_li = $this.parent("li");
            if (parent_li.hasClass('active'))
            {
                e.preventDefault();
            }            
        }

      });
  };
almasaeed2010 commented 7 years ago

Hello @C09405,

We appreciate your suggestion. However, we do not believe this is a necessary option for the general use of AdminLTE at this point.

We apologize for any inconvenience.

Thanks!

BrstDev123 commented 7 years ago

I am getting issue in sidebar-menu, On first click menu toggle but working fine on further clicks.