holiber / sl-vue-tree

Customizable draggable tree component for Vue.js
https://holiber.github.io/sl-vue-tree/
MIT License
345 stars 78 forks source link

Is there a way to expand/collapse nodes programatically? #83

Closed aprln closed 4 years ago

aprln commented 4 years ago

Hi, Is there a way to expand/collapse nodes programatically? Thank you!

aprln commented 4 years ago

To answer my own question, you just need to set isExpanded to true/false on the node data you want to expand/collapse

alex-dow commented 4 years ago

To expand on this solution ;) , I wrote a wrapper component around sl-vue-tree and exposed these methods:

        expandNode(path) {
            const node = this.$refs['tree'].getNode(path);
            this._expandNode(node);
        },

        _expandNode(node) {
            this.$refs['tree'].updateNode(node.path, { isExpanded: true });
            if (node.level > 1) {
                this.expandNode(node.path.slice(0, node.path.length -1 ));
            }
        },

Through out my usage of sl-vue-tree, I stick with paths to reference nodes, so this works for me and seems to be inline with the rest of the methods in sl-vue-tree. Hope it helps.