phil o.O
phil o.O

Reputation: 416

AngularJS JSON Editor toggle editable

Currently, I'm using ng-jsoneditor.

I'm trying to toggle the state from editable to read-only.

To set the readonly property I have to specify the onEditable property of the options property. For instance:

 $scope.obj = {
            data: json, 
            options: {
                mode: 'tree',
                onEditable: function(){return false;}
            }
  };

I am able to get the changeOptions property to toggle the mode property, however I cannot get the onEditable property to toggle. I want the user to have the capability to edit the json while in the 'code' mode. This is what I tried:

    $scope.changeOptions = function () {
        $scope.obj.options.mode = $scope.obj.options.mode == 'tree' ? 'code' : 'tree';
        $scope.obj.options.onEditable = $scope.obj.options.onEditable == function(){return false;} ?  function(){return true;} :  function(){return false;};
    };

JsFiddle Example

Upvotes: 2

Views: 1988

Answers (1)

aitchkhan
aitchkhan

Reputation: 1952

Edit:

Didn't quite get your question at first, my bad.

In your example what you were doing wrong was checking a variable for a function definition which is onEditable(), which quite does not work.

So what I did was check if mode is tree or code then assign a function to onEditable option of ng-jsoneditor.

Here is the updated fiddle.

PS: I did clean some code as well.

Upvotes: 1

Related Questions