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.

[BUG] The opened Treeview menu won't collapse when the Treeview was initiated more than once

See original GitHub issue

Hi guys, I run Adminlte with Laravel using Inertiajs with Vue (SPA). the app is loading the adminlte on the login page so after logging in and being redirected to the dashboard the treeview needs to be initiated. I initiate the treeview with this:

$('[data-widget="treeview"]').each(function () { adminlte.Treeview._jQueryInterface.call($(this), 'init') })

and then the treeview is working properly.

but, the problem is when I refresh the page on the dashboard, the treeview can open but won’t collapse.

I think it’s because the treeview init runs more than once, first when the page refreshed and then when my code initiates the treeview again. how to check whether the treeview was initiated or not?, so I can condition my code to only run when the treeview is not initiated yet, or is there any other solution with this?, sorry for my bad English, I appreciate the help.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
ImCreator2commented, Nov 15, 2022

faced same problem while working with laravel & Vue 3 with inertiajs. found a fix for this after working it on 3 days, please checkout.

Add below lines in <script setup> of Vue component.

import { onMounted, onUnmounted } from "vue";

onMounted( () => {
    $('[data-widget = "treeview"]').Treeview('init');
    // $('#mainSidebar').Treeview('init');

    console.log('Main sidebar mounted.');
})

onUnmounted(() => {
    $(document).on('click','[data-widget="treeview"] .nav-link', function (e) {
        e.stopImmediatePropagation();
    });

    console.log('Treeview stopped.');
})

Hope this works for others too.

1reaction
galihsetyocommented, May 27, 2022

Update: there is still a problem with the code above, so here is my solution, so I make a function to initiate the TreeView in app.js:

let treeViewInit = function(){
    $('[data-widget="treeview"]').each(function () {
        adminlte.Treeview._jQueryInterface.call(
            $(this),
            "init"
        );
    });
}

and run that function with the Lodash once function:

let treeViewInitOnce = _.once(treeViewInit);

after that, I make a global property to call that Lodash function:

createInertiaApp({
    resolve: name => import(`./Pages/${name}`),
    title: title => title ? `${title} - Walisongo` : 'Walisongo',
    setup({ el, App, props, plugin }) {
        const myApp = createApp({ render: () => h(App, props) })
            .use(plugin)
            .mixin({ methods: {} })

        myApp.config.globalProperties.$treeViewInitOnce = treeViewInitOnce;

        myApp.mount(el);

        return myApp;
    },
})

and then I use that in my component:

let _this = this;

let treeView;
$(window).on('load.lte.treeview', function (e) {
    treeView = e;
});

var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
        clearInterval(readyStateCheckInterval);

        if(treeView == undefined) {
            _this.$treeViewInitOnce();
        }
    }
}, 10);
Read more comments on GitHub >

github_iconTop Results From Across the Web

AdminLTE 3 expend menu doesn't close/collapsed when ...
Right now all menu items are opening one by one and they can only be closed ... if it is not in treeview...
Read more >
How to collapsed nav-treeview when I click the submenu in ...
Here is what you've to do... Add a menu-open class in the li element right after the nav-item class (which is the first...
Read more >
Java TreeView User's Manual - SourceForge
Any operation which selects genes in one view, either due to genome ordering, hierarchical clustering, or per-gene statistics, selects the genes in all...
Read more >
Tree View API - Visual Studio Code
For example, the built-in References Search View extension shows reference search results as a separate view. References Search View. The Find All References ......
Read more >
How to expand tree view by default when page open in...
But when i run the page by default it was displayed in Collapse mode but i want to in expand. i have done...
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