question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to remember the toggle state of the sidebar menu?

See original GitHub issue

I am using AdminLTE in combination with Codeigniter3. Therefore a lot of HTML is generated on the back-end of my application. I’d like to find a way to let the back-end know the toggle state (collapsed or open) of the sidebar menu so that I can generate the appropriate HTML for a page.

I tried to use the href in: <a href="<?= site_url("site/toggle");?>" class="sidebar-toggle" data-toggle="offcanvas" role="button"> but that doesn’t work because the href is not used.

Does anyone know a way to achieve this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:24

github_iconTop GitHub Comments

14reactions
rodruizcommented, Oct 12, 2018

Hi, do not use cookies. It’s easier to use LocalStorage. I did it here.

  • Store sidebar state
<script type="text/javascript">
    (function ($) {
        /* Store sidebar state */
        $('.sidebar-toggle').click(function(event) {
            event.preventDefault();
            if (Boolean(localStorage.getItem('sidebar-toggle-collapsed'))) {
                localStorage.setItem('sidebar-toggle-collapsed', '');
             } else {
                localStorage.setItem('sidebar-toggle-collapsed', '1');
             }
         });
    })(jQuery);
</script>
  • Recover sidebar state
<script type="text/javascript">
    /* Recover sidebar state */
     (function () {
        if (Boolean(localStorage.getItem('sidebar-toggle-collapsed'))) {
            var body = document.getElementsByTagName('body')[0];
            body.className = body.className + ' sidebar-collapse';
        }
    })();
</script>

This works perfectly for me.

Thanks.

13reactions
EnnioWolsinkcommented, Oct 27, 2017

Thanks @REJack for your script, works nicely!

I adapted it slightly so it uses localStorage. One of the advantages of this approach is that it remembers the state for all pages, whereas the cookie would be set per path. Basically this means the toggle state was remembered on a per page basis (at least in my browser, Chrome 61 on Windows 10), which may be ideal for some uses cases but not mine.

I also tried to reproduce the bug @kher09 mentioned, without much luck. Maybe someone else can chip in there.

    $.AdminLTESidebarTweak = {};

    $.AdminLTESidebarTweak.options = {
            EnableRemember: true,
            NoTransitionAfterReload: true
            //Removes the transition after page reload.
    };

    $(function () {
        "use strict";

        $("body").on("collapsed.pushMenu", function() {
            if($.AdminLTESidebarTweak.options.EnableRemember) {
                localStorage.setItem("toggleState", "closed");
            }
        });
            
        $("body").on("expanded.pushMenu", function() {
                if($.AdminLTESidebarTweak.options.EnableRemember) {
                    localStorage.setItem("toggleState", "opened");
                } 
        });

        if ($.AdminLTESidebarTweak.options.EnableRemember) {
            var toggleState = localStorage.getItem("toggleState");
            if (toggleState == 'closed'){
                if ($.AdminLTESidebarTweak.options.NoTransitionAfterReload) {
                    $("body").addClass('sidebar-collapse hold-transition').delay(100).queue(function() {
                        $(this).removeClass('hold-transition');
                    });
                } else {
                    $("body").addClass('sidebar-collapse');
                }
            }
        }
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remember the toggle state of the sidebar in AdminLTE3?
Adminlte3. Just simple add data-enable-remember="true" to your "pushmenu" button. Example code: <a class="nav-link" data-widget="pushmenu" ...
Read more >
Keep menu toggle state after page reload | Jin's blog
We need to store this state into localStorage and re-set it when page is reloaded.
Read more >
Toggle Sidebar Navigation HTML CSS Javascript - YouTube
Toggle Sidebar Navigation HTML CSS Javascript-------------------------------------------------------------------------Cheapest Domain ...
Read more >
how to remember the toggle state of the sidebar menu
Click on the toggle menu you will see a sidebar navigation slides out from the left side of the screen while pushing the...
Read more >
Sidebar, UI Widgets Webix Docs
The Menu button allows collapsing the menu items into a vertical navigation bar with icons. In this state menu items will be shown...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found