Cannot call method error - Bootstrap Treeview

1.3k views Asked by At

I am using Bootstrap Treeview in Unify templates. I am getting following error:

bootstrap-treeview.js:1212 Not initialized, can not call method : expandNode

when I try to expand node programmatically on page refresh to retain the expanded state of tree.

 if (document.location.href.indexOf(hash) > -1) {


            var nodeId = $.cookie('activeAccordionGroup');

            if (nodeId != null) {
                $('#tree').treeview('expandNode', [nodeId ]);
            }
        }

I am getting nodeId in the above code, from the cookie which was created while selecting the node. I am getting same error for all the methods explained in below link:

https://github.com/jonmiles/bootstrap-treeview#methods

1

There are 1 answers

0
Lorne Redmond On

I was troubleshooting the same error today but by calling a different method: collapseAll; on the bootstrap treeview plugin.

It wasn't until I applied something like the following that the treeview method behaved as expected:

setTimeout(function() {
  $("#tree").treeview("collapseAll", { silent: true })
 }, 1000);

I called the above just AFTER I setup the treeview initially inside an ajax call. In other words, I run the treeview function twice within the same ajax success function.

Once to apply treeview settings, populate the treeview with data and wireup events;

AND a second time to run a method on the treeview (but with a 1 second delay). My guess why this works in this manner, is that the treeview method-calls cannot engage the treeview until it is fully "initialized" (ie. completely rendered).