patternfly / patternfly-bootstrap-treeview

Tree View for Twitter Bootstrap -
http://jonmiles.github.io/bootstrap-treeview
Apache License 2.0
200 stars 105 forks source link

Nodes not initializing correctly #103

Closed pszNicx closed 5 years ago

pszNicx commented 5 years ago

Hi,

I've been running into an issue with unallocated nodes causing a few of the calls to not work properly. I'm not sure if I'm not doing something right but I worked around the issue by editing the bootstrap-treeview.js file.

My version of bootstrap-treeview.js says v2.1.0 at the top but I think it's actually 2.1.8 since I downloaded the latest. My jQuery is version 3.4.1 and Bootstrap is 3.3.7.

Even with a very simple data load it seems to have issues with finding/expanding/collapsing select nodes. When I debugged it I found the issue occurring at the $.grep call on line 1886 of bootstrap-treeview.js, where the value of this._orderedNodes (and this._nodes) are null.

I worked around my particular issue by adding the following just before that line:

if(!this._nodes)
{
    console.error('Nodes have not been initialized; setting them to empty.');
    this._nodes = {};
    this._orderedNodes = {};
}

Am I missing some initialization step or is there something actually broken here?

pszNicx commented 5 years ago

I figured out the issue. I was trying to process the tree in my JQuery ready block but that's before the treeview initializes. Example:

$(function() {
    $('#treeview').treeview({ levels: 3, highlightSelected: false, data: [
        {text: 'foo', id: 1, category: 'a', nodes: [
            {text: 'bar', id: 2, category: 'b', nodes: [
                {text: 'bla', id: 4, category: 'c', nodes: []}
            ]},
            {text: 'baz', id: 3, category: 'b', nodes: []}
        ]}
    ] });
    var tree = $('#treeview').treeview(true);
    var categoryNodes = tree.findNodes('b', 'category'); // <-- Throws Cannot read property 'length' error
});

To fix, I just needed to move my logic until after everything initializes.