ezraroi / ngJsTree

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

Tree will not update #70

Closed sinamics closed 8 years ago

sinamics commented 8 years ago

Hi I’m new to Angular and JS, so please bear with me.. I’m populating JsTree data from a ajax call, that works perfect, but when trying to add new Node, it won’t update the Tree dynamically.

So my Q is how can i update the Tree when addNewNode? It works fine when using static data array as in your demo.

added "data" to treeConfig

vm.treeConfig = {
            core : {
                multiple : false,
                animation: false,
                stripes : true,
                error : function(error) {
                    $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
                },
                check_callback : true,
                worker : true
            },
            'data': {
                'url': 'include/function/ajax-Docs.php?type=get_node',
                'data': function (node) {
                    return {'parent_id': node.id};
                },
                'success': function(data){
                    angular.copy(data, vm.treeData);
                },
                'error': function (node) {
                    //console.log(node);
                },
                "dataType": "json",
            },
            types : {
                default : {
                    icon : 'glyphicon glyphicon-flash'
                },
                star : {
                    icon : 'glyphicon glyphicon-star'
                },
                cloud : {
                    icon : 'glyphicon glyphicon-cloud'
                }
            },
            version : 1,
            plugins : ['state', 'contextmenu', 'wholerow', 'unique', 'types', 'ui']
        }

and addNewNode;

vm.addNewNode = function() {
            $.get('include/function/ajax-Docs.php?type=create_node', {
                'parent_id': vm.newNode.parent,
                'name': vm.newNode.text,
                'text': vm.newNode.text
            })
                .done(function (d) {
                    var data = $.parseJSON(d)
                    vm.treeData.push({ id : data.id.toString(), parent_id : vm.newNode.parent, name : vm.newNode.text, text : vm.newNode.text });

                })
                .fail(function () {
                    toaster.pop('failed', 'Ooops something wrong happend', 'Please contact Admin' + this.selectedNode);
                });
        };
ezraroi commented 8 years ago

Check the code in the github page branch