1

In AngularJS , I am trying to make and editable TreeView from JSON data. I want to get a nodevalue from JSON and edit it using forms. While editing, the original JSON should change immediately. I created below plunkr for this but not getting an idea, how to achieve this.

Plunkr

var app = angular.module('plunker', []);

app.directive('collection', function () {       
    return {
        restrict: "E",
        //replace: true,
        scope: {collection: '='},
        //controller: 'TreeController',
        //bindToController: true,       
        template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"         
    }
})

app.directive('member', function ($compile) {
    var linkerfunc = function(scope, element, attrs) {  
                    var collectionSt = '<collection collection="member.children"></collection>';
                    $compile(collectionSt)(scope, function(cloned, scope)   {                                           
                        element.append(cloned); 
                     });
                     scope.ShowDetailsFunc = function() {   
                       console.log('in function scope showdetails')
                        scope.ShowDetailsCtrlFunc(element,event);                     
                    } 
    }
    return {
        restrict: "E",
        //replace: true,
        scope: {member: '=', ShowDetailsCtrlFunc : '&'},
        template: "<li><span ng-click=ShowDetailsCtrlFunc($event)>{{member.NodeName}}</span></li>",     
        controller: 'MainCtrl',
        //controllerAs: 'MainCtrl',
        //bindToController: true,
        link: linkerfunc        
    }
})

app.controller('MainCtrl', function ($scope,$timeout) {     

    $scope.Intialfunc = function() { 
        $scope.testdata = []
        var myjsondata = JSON.parse('{ "NodeName": "Parent", "NodePath": "1", "children": [ { "NodeName": "mychild", "NodePath": "1.1", "children": [ { "NodeName": "chld1", "NodePath": "1.1.1", "children": [] } ] } ] }');
        $scope.testdata.push(myjsondata);
            //console.log($scope.testdata) //This one is showing
            $scope.changename = $scope.testdata[0].children[0].children[0].NodeName;    

        }   

    $timeout(function(){ $scope.Intialfunc(); }, 1000)

    $scope.ShowDetailsCtrlFunc = function(event) {
            console.log("in function ShowDetailsCtrlFunc"); 

            //event.stopImmediatePropagation(); 
      };
});

I tried angular.copy but that needs the portion of JSON data in a $scope variable and updates that variable as expected.

But my JSON data going to be a huge and i dont know how to update it without using variable. Please help.

1 Answer 1

2

You can use ng-change to perform an action when an input changes:

<input type="text" ng-model="changename" ng-change="inputChange()">

Then, just create a function in your controller that updates what you need it to:

$scope.inputChange = function() {
  // do stuff here
  $scope.testdata[0].children[0].children[0].NodeName = $scope.changename;
  // write object to JSON
  var JSONstring = $scope.testdata.toJSON();
}

I would also recommend looking at ng-model-options and changing the debounce settings so that you aren't constantly toJSONing with every keystroke.

http://plnkr.co/edit/dPWsowm4Puwajgk6CfvA?p=preview

Sign up to request clarification or add additional context in comments.

3 Comments

Bravo !! This is exactly i want. Didn't think that this could be so simple. Thanks @Pop-A-Stash.
But there is just one problem. If the path "$scope.testdata[0].children[0].children[0].NodeName" is too long, it may give performance issue.
I agree. This is not at all a long term solution. I'm simply showing you how you can enact change when a form field changes. Ideally you would map each node in your JSON to a scope variable AND a matching form input with an ng-change directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.