Preserve Navigation bar (Hide/Show) when navigated to Other module

241 views Asked by At

How to preserve the navigation bar (minimized position) when navigated to other module. Collapsed Navigation Bar

sidebar-collapse will not preserve if navigated to other page.

4

There are 4 answers

0
Helloall On BEST ANSWER

@minhhungit Thank you for your demo.

I have used cookie implementation similar to ThemePreference.

Views/Shared/_Layout.cshtml

var theme = !themeCookie.IsEmptyOrNull() ? themeCookie : "blue";
// Add following lines
var sbNavCookie = Context.Request.Cookies["SBNavigationPreference"];
var sbNav = !sbNavCookie.IsEmptyOrNull() && sbNavCookie == "true" ? true : false;

in Body Tag,

<body ... class="...@(sbNav?" sidebar-collapse":"")">

In Script Tag,

<script type="text/javascript">
    $().ready(function () {
// Add new lines
        let sidebarToggleButton = $('.main-header .sidebar-toggle');
        if (sidebarToggleButton) {
          $(sidebarToggleButton).on('click', function (e) {
              e.preventDefault();
              let isSidebarCollapse = $('body').hasClass('sidebar-collapse');
              $.cookie('SBNavigationPreference', !isSidebarCollapse, {
                  path: Q.Config.applicationPath,
                  expires: 365
              });
          });
      }


0
minhhungit On

This is demo and source code I created

https://minhhungit.github.io/2020/10/16/011-keep-menu-toggle-state-after-page-reload/

namespace J {
    export function storageSet(name, val) {
        if (typeof (Storage) !== "undefined") {
            localStorage.setItem(name, val);
        } else {
            //window.alert('Please use a modern browser to properly view this template!');
        }
    }

    export function storageGet(name) {
        if (typeof (Storage) !== "undefined") {
            return localStorage.getItem(name);
        } else {
            return null;
        }
    }
}

In _Layout.cshtml put this code

<script type="text/javascript">
  $().ready(function () {
      new DailyTimelog.Common.SidebarSearch($('#SidebarSearch'), $('#SidebarMenu')).init();
      // ...
      // ...
      $(window).bind("load resize layout", doLayout);
      doLayout();
  });
  </script>

  // Add these lines
  <script type="text/javascript">
      let sidebarToggleButton = $('.main-header .sidebar-toggle');
      if (sidebarToggleButton) {
          $(sidebarToggleButton).on('click', function (e) {
              e.preventDefault();
              let isSidebarCollapse = $('body').hasClass('sidebar-collapse');
              J.storageSet('IS_SIDEBAR_COLLAPSE', !isSidebarCollapse);
          });
      }
  </script>

And LeftNavigation.cshtml

<script type="text/javascript">
    function openWindow(url, width, height) {
        height = height || (screen.availHeight - 60);
        // ...
        // ...
    }
    
    // Add these lines
    let isSidebarCollapseCache = J.storageGet('IS_SIDEBAR_COLLAPSE');
    if (isSidebarCollapseCache == 'true') {
        $('body').toggleClass('sidebar-collapse', true);
    }
    else {
        $('body').toggleClass('sidebar-collapse', false);
    }
</script>

``

0
Battleaxe On

Do you want your nav-bar to change from big to small when you move to a different place?

(sorry, I'm not allowed to comment yet).

0
Battleaxe On

Do you want your nav-bar to change from big to small when you move to a different place? It is always nice if you add some code :)

const menuToggle = document.querySelector('.menu-toggle');
const mainUl = document.querySelector(".main-ul");
const navBtn = document.querySelectorAll(".nav-a");

// Open and close meny on click
menuToggle.addEventListener("click", function () {
  menuToggle.classList.toggle("open");
  mainUl.classList.toggle("open");
});

// Close menu when clicked on an a-tag in the menu
navBtn.forEach((button) => {
  button.addEventListener("click", function () {
    navBtn.forEach((button) => button.classList.remove("active"));
    this.classList.add("active");
    mainUl.classList.toggle("open");
    menuToggle.classList.toggle("open");
  });
});