ezraroi / ngJsTree

Angular Directive for the famous JS Tree
http://ezraroi.github.io/ngJsTree/
MIT License
270 stars 101 forks source link
angular jstree

Bower version Build Status Dependency Status Coverage Status Code Climate Built with Grunt NPM

ngJsTree

Angular Directive for the famous JS Tree library.

Dependencies

The ngJsTree depends on the following libraries:

Install

You can install the ngJsTree with bower:

bower install ng-js-tree --save

or with npm:

npm install ng-js-tree --save

or you can add the ngJsTree.min.js file to your HTML page:

<script src="https://github.com/ezraroi/ngJsTree/raw/master/jquery.js"/>
<script src="https://github.com/ezraroi/ngJsTree/raw/master/angular.js"/>
<script src="https://github.com/ezraroi/ngJsTree/raw/master/jstree.min.js"/>
<script src="https://github.com/ezraroi/ngJsTree/raw/master/ngJsTree.min.js"/>

Add the ngJsTree to your module dependencies

Documentation

You can find the JSTree documentation at this link

Usage

<div js-tree="treeConfig" ng-model="treeData" should-apply="ignoreModelChanges()" tree="treeInstance" tree-events="ready:readyCB;create_node:createNodeCB"></div>

Registering for events

You can register a callback for any Js Tree event in one of the following ways:

Example:

<div ng-controller='myCtrl'>
    <div js-tree="treeConfig" ng-model="treeData" should-apply="ignoreModelChanges()" tree="treeInstance" tree-events="ready:readyCB;create_node:createNodeCB"></div>
</div>
angular.module('myApp').controller('myCtrl', function($scope,$log) {
    $scope.readyCB = function() {
        $log.info('ready called');
    };

    $scope.createNodeCB = function(e,item) {
        $log.info('create_node called');
    };
);

Example:

<div ng-controller='myCtrl'>
    <div js-tree="treeConfig" ng-model="treeData" should-apply="ignoreModelChanges()" tree="treeInstance" tree-events-obj="treeEventsObj"></div>
</div>
angular.module('myApp').controller('myCtrl', function($scope,$log) {

    $scope.treeEventsObj = {
      'ready': readyCB,
      'create_node': createNodeCB,
      'select_node': selectNodeCB   // on node selected callback
    }

    function readyCB() {
        $log.info('ready called');
    };

    function createNodeCB(e,item) {
        $log.info('create_node called');
    };

    function selectNodeCB(node, selected, event) {
        $log.info("some_node_selected");
    };
);

NOTE: Only one of the methods can be used to pass event callbacks, tree-events will take precedence.

Using the Js Tree API from your controller

Add the tree attribute to the jstree directive and assign it with a name of a variable in your controller that will hold the jstree instance.

<div ng-controller='myCtrl'>
    <div js-tree="treeConfig" ng-model="treeData" tree="treeInstance"></div>
</div>
function yourCtrl($scope)  {
    var selected_nodes = $scope.treeInstance.jstree(true).get_selected();
}

Recreating the Tree

If from some reason you would like to recreate the tree, the right way to do it is update the tree configuration object. Once the directive will detect a change to the tree configuration it will destory the tree and recreate it.

this.treeConfig = {
    core : {
        multiple : false,
        animation: true,
        error : function(error) {
            $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
        },
        check_callback : true,
        worker : true
    },
    version : 1
};
this.reCreateTree = function() {
    this.treeConfig.version++;
}

Development

Prepare your environment

Build

TDD

License

MIT