javascriptcsscookiesrazorserenity-platform

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


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.


Solution

  • @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
                  });
              });
          }