简体   繁体   English

AngularJS-JSON对象编辑

[英]Angularjs - JSON object editing

I am pretty good at using existing data but I am not an expert on editing JSON objects quite yet so a little help here would do a lot for me. 我非常擅长使用现有数据,但是我还不是编辑JSON对象的专家,因此这里的一些帮助对我有很大帮助。 For all I know this could be a rudimentary question but I am asking because I don't know. 就我所知,这可能是一个基本问题,但是我问是因为我不知道。

How would I update a value in JSON string that looks like below? 如何更新如下所示的JSON字符串中的值? It is a kineticjs object that I need to disable draggable elements in before I re-generate the stage. 这是一个dynamicjs对象,在重新生成舞台之前,需要禁用可拖动元素。

I created a fiddle based on some input below but I am not successful at it: http://jsfiddle.net/8Y6zZ/5/ 我根据下面的一些输入创建了一个小提琴,但是我没有成功: http : //jsfiddle.net/8Y6zZ/5/

For example, I would like to set "draggable": false, wherever I have "draggable": true, ... How could I do that with javascript (I am working in angularjs). 例如,我想设置"draggable": false,无论我在哪里设置"draggable": true, ...如何使用javascript做到这一点(我在angularjs中工作)。

{
    "attrs": {
        "width": 1276,
        "height": 660
    },
    "className": "Stage",
    "children": [
        {
            "attrs": {},
            "className": "Layer",
            "children": [
                {
                    "attrs": {
                        "x": 0,
                        "y": 0,
                        "draggable": false,
                        "name": "brochure-1.png"
                    },
                    "className": "Image"
                },
                {
                    "attrs": {
                        "x": 999,
                        "y": 288,
                        "draggable": true,
                        "name": "sample1.png",
                        "rotation": 0,
                        "scaleX": 1,
                        "scaleY": 1,
                        "offsetX": 0,
                        "offsetY": 0,
                        "skewX": 0,
                        "skewY": 0
                    },
                    "className": "Image"
                },
                {
                    "attrs": {
                        "x": 301,
                        "y": 115,
                        "draggable": true,
                        "name": "sample2.png",
                        "rotation": 0,
                        "scaleX": 1,
                        "scaleY": 1,
                        "offsetX": 0,
                        "offsetY": 0,
                        "skewX": 0,
                        "skewY": 0
                    },
                    "className": "Image"
                }
            ]
        }
    ]
}

Related to this question,, are there good ways to re-building JSON objects from "basic" JSON... I guess what I am asking is this... if a developer on the back end has no good way to translate data and they pass a badly organized JSON object, is there a way to manipulate the object to restructure it so it can be used well in angularjs.. 与此问题相关,是否有好的方法可以从“基本” JSON重新构建JSON对象...我想我要问的是...如果后端的开发人员没有很好的方法来转换数据并他们传递了一个组织不好的JSON对象,是否有一种方法可以操纵该对象以对其进行重组,以便可以在angularjs中很好地使用。

Use angular's foreach. 使用角度的foreach。 Then you just have to direct it to the list of children in your object. 然后,您只需要将其定向到对象中的子代列表即可。 Something like this should work with replace YOUR_OBJECT with the name of your JSON object. 这样的事情应该可以将YOUR_OBJECT替换为JSON对象的名称。

angular.forEach(YOUR_OBJECT.children.children, function(obj){
  obj.attrs.draggable = false;
});

Not sure on the json structure, but if you possibly have a bunch of child objects nested, you could use simple recursion to descend through all the children.. something like this: 不确定json结构如何,但是如果您可能嵌套了一堆子对象,则可以使用简单的递归来遍历所有子对象。如下所示:

function setDraggable(obj) {
  if(obj.attrs) {
    obj.attrs.draggable = false;
  }
  if(!obj.children) {
    return;
  }
  angular.forEach(obj.children, function(o) {
    setDraggable(o);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM