使用AngularJS根据输入值更新json文件

时间:2015-03-03 18:50:44

标签: javascript json angularjs

我编写了以下代码来从json文件中读取数据并绘制图形。现在我想通过输入字段从用户获取数据并使用新值更新json。我编写了代码来复制值并使其在范围之外可访问,我只是想知道如何使用新值更新现有的json。

json看起来像这样:

{

“节点”:[

          { "id": 1, "label": "End Product", "level": 0 },
          { "id": 2, "label": "A 1", "level": 1 },
          { "id": 3, "label": "A 2", "level": 1 },
          { "id": 4, "label": "A 3", "level": 1 },
          { "id": 5, "label": "B 1", "level": 2 },
          { "id": 6, "label": "C 1", "level": 3 },
          { "id": 7, "label": "B 2", "level": 2 },
          { "id": 8, "label": "B 3", "level": 2 }

        ],

“edge”:[

          { "from": 1, "to": 2 },
          { "from": 1, "to": 3 },
          { "from": 1, "to": 4 },
          { "from": 2, "to": 5 },
          { "from": 5, "to": 6 },
          { "from": 3, "to": 7 },
          { "from": 4, "to": 8 }

        ]

}

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

app.controller('bomController', ['$scope', 'appService', '$rootScope', function ($scope, appService, $rootScope) {
   
    var get = function () {
        appService.get().then(function (promise) {  

            $scope.graph = {
                
                options:  {
                    "hierarchicalLayout": {
                        "direction": "UD"
                        
                    },
                    "edges": {
                        "style":"arrow-center",
                        "color":"#c1c1c1"
                    },
                    "nodes": {
                        "shape":"oval",
                        "color":"#ccc"

                    }
                },
                
                data: {
                    nodes: promise.nodes,
                    edges: promise.edges
                }
            };
             
            
        });
        
    };
    
    $scope.newNode = {
        id: undefined,
        label: undefined,
        level: undefined,
        parent: undefined,
       
    };

    $scope.arrNode = {};
    $scope.update = function (nodes) {
        $scope.arrNode = angular.copy(nodes);
        $rootScope.nodex = angular.copy(nodes);
        
       
    };

   
    
    $scope.newEdge = {
        id: undefined,
        from: undefined, 
        to: undefined
       
    };
   
    $scope.arrEdge = {};
    $scope.updateE = function (edges) {
        $scope.arrEdge = angular.copy(edges);
        $rootScope.edgex = angular.copy(nodes);
    };
    
   
    get();
    

}]);



app.factory('appService', ['$q', '$http', '$rootScope', function ($q, $http, $rootScope) {
    
    return {
        get: function (method, url) {
            var deferred = $q.defer();
            $http.get('data.json')
              .success(function (response) {
                  deferred.resolve(response);
                  
              })
            return deferred.promise;
           
        },

     
        
    };
    
}]);
<body data-ng-app="bomApp">
    <div data-ng-controller="bomController">
        <h3>Create Your Own Graph</h3>
        <div class="node">
            <h4>Nodes:</h4>
            <div class="panel">
                <label>Id:</label>
                <input type="text" id="idNode" data-ng-model="newNode.id"/>
                <br />
                <label>Label:</label>
                <input type="text" id="label" data-ng-model="newNode.label" />
                <br />
                <label>Level:</label>
                <input type="text" id="level" data-ng-model="newNode.level" />
            </div>
            <div>
                <button id="addNode" data-ng-click="update(newNode)">Add</button>
                
            </div>
        </div>
        <div class="node">
            <h4>Edges:</h4>
            <div class="panel" >
                <label>Id:</label>
                <input type="text" id="idEdge" data-ng-model="newEdge.id"/>
                <br />
                <label>From:</label>
                <input type="text" id="from" data-ng-model="newEdge.from"/>
                <br />
                <label>To:</label>
                <input type="text" id="to" data-ng-model="newEdge.to"/>
            </div>
            <div>
                <button id="addEdge" data-ng-click="updateE(newEdge)">Add</button>
                
            </div>
            
        </div> 

        <div data-custom-dir="" id="graph" data="graph.data" options="graph.options"></div>
        <div>
            <h3>Monitor The Changes</h3>
            <div class="node">
                <h4>Nodes:</h4>
                <div class="col">
                    <div id="nodes">
                        <pre>{{newNode | json}}</pre>
                        <pre>{{arrNode | json}}</pre>
                    </div>
                </div>
            </div>
            <div class="node">
                <h4>Edges:</h4>
                <div class="col">
                    <div id="edges">
                        <pre>{{newEdge | json}}</pre>
                        <pre>{{arrEdge | json}}</pre>
                    </div>
                </div>
            </div>
            
        </div>

    </div>
</body>

2 个答案:

答案 0 :(得分:0)

您可以使用.push()向javascript对象添加项目。要进行更新,您必须遍历该对象,直到找到匹配的id(或其他某个值)。

最后,您可以使用.appendTo()更新指示已进行了哪些更改的部分。

示例:

$('#addNode').click(function () {

    // pseudo code to add item to javascript object:
    // 'node' would be presumably nested based on the OP, 
    // but information on js object name is not provided

    var nodeId = $('#idNode').val();
    var nodeLabel = $('#label').val();
    var nodeLevel = $('#level').val();

    nodes.push({
        "id": nodeId,
            "label": nodeLabel,
            "level": nodeLevel,

    });

    // psudeo code for updating js object
    // loop through object to find matching id:

    var locate = 0;
    for (var i = 0; i < nodes.length; i++) {
        if(node[i].id == nodeId){
            locate = i;
            node[i].label = nodeLabel;
            node[i].level = nodeLevel;
        }
    }

    // Adding results to element

    var nodesElem = $('#nodes');
    var addElem = "<pre> { newNode | " + JSON.stringify(node[locate]) + " }";

    addElem.appendTo(nodesElem);

});

答案 1 :(得分:0)

如果您需要它能够更改现有记录,请尝试:

$scope.graph.data.nodes[$scope.newNode.id] = $scope.newNode;

 $scope.graph.data.edges.push($scope.newEdge);

这些方法函数调用应使用newNodenewEdge

更新现有数据