cheton / infinite-tree

A browser-ready tree library that can efficiently display a large amount of data using infinite scrolling.
https://infinite-tree.js.org
MIT License
170 stars 38 forks source link

Open or close alls Nodes #45

Closed oleg2204 closed 5 years ago

oleg2204 commented 5 years ago

Hi,

i try to achive that at first display all nodes are close (autoOpen=false) (no problem here yet), than if the user searches something and i apply the filter i want to open all rendered nodes. When unfilter is called i want all nodes be close again.

Is this somehow possible? I couldnt find a solution for this yet. I would be glad for help or a hint.

cheton commented 5 years ago

It may require to flatten the root node and pass openAllNodes=true as its parameter to return new nodes when calling filter:

filter(predicate, options) {
    :    :    :  
    traverse(rootNode);

    this.nodes = flatten(rootNode.children, {
        openAllNodes: true // open all nodes
    });

    // Update rows
    this.rows.length = this.nodes.length;
    for (let i = 0; i < this.nodes.length; ++i) {
        const node = this.nodes[i];
        this.rows[i] = this.options.rowRenderer(node, this.options);
    }

    this.update();
}

To revert back to previous open state when calling unfilter, flatten the root node again with openNodes state:


unfilter() {
    :    :    :  
    traverse(rootNode);

    this.nodes = flatten(rootNode.children, {
        openNodes: this.state.openNodes // open only desired nodes
    });

    // Update rows
    this.rows.length = this.nodes.length;
    for (let i = 0; i < this.nodes.length; ++i) {
        const node = this.nodes[i];
        this.rows[i] = this.options.rowRenderer(node, this.options);
    }

    this.update();
}
cheton commented 5 years ago

Not verified whether it will work or not, I will let you know the result when I have time to make a complete test.

oleg2204 commented 5 years ago

I just tried the code and it worked just like desired. Many thx! I needed to adjust the following a bit: I used _flattree.flatten(rootNode.children, { .... because flatten() seemed to be undefined