arvindr21 / jsTree-directive

An Angular Directive for jsTree. Docs :
http://jstree-directive.herokuapp.com/
51 stars 33 forks source link

jstree instance #22

Closed icruces closed 7 years ago

icruces commented 9 years ago

Hi, is there any way to retrieve the jstree instance in the controller so that I can use the jsTree API?

My problem is that I have multiple trees in a page and when I select one folder I would like to deselect any selected folder in the other trees. I think it could work by calling the "deselect_all" method in each tree.

It would be good if you could bind the instance with a scope variable:

<js-tree tree-instance="myTree" ...>

Thanks.

oxycoder commented 8 years ago

"To invoke a method on an instance you must obtain a reference of the instance and invoke the method". Following the current struct jsTree.directive.js, you can do following:

       manageInstance: function (s, e, a) {
            if (a.treeInstance) {
                // Using the first way to invoke method on instance
                s[a.treeInstance] = this.tree.jstree(true);
            }
        }

then

    treeDir.manageInstance(s, e, a);

Setup attribute in view

<js-tree tree-instance="myTree" ...>

Access instance and invoke jstree method in controller

    $scope.myTree.open_node('node_1');

List of available method for jstree: https://www.jstree.com/api/#/?q=(

qlux commented 8 years ago

Thank you, that's what I was looking for! Really useful to get the getSelected() method inside angular.

icruces commented 7 years ago

Thanks.

qlux commented 7 years ago

Actually since there is some activity on this thread I will add a better method: If you add a tree-instance to the jsTree declaration: <js-tree tree-id="dataTree" tree-instance="myTreeInstance" tree-plugins="........></js-tree>

you can access it inside the scope as follow:

$scope.myTreeInstance.deselect_all(); $scope.myTreeInstance.open_node(openFolder); $scope.myTreeInstance.select_node(selectNode);