首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从orgchart json更新数据库

从orgchart json更新数据库
EN

Stack Overflow用户
提问于 2017-09-19 22:54:21
回答 2查看 2.3K关注 0票数 0

我正在使用jquery orgchart,我想将我的图表的更改保存到数据库中,orgchart有一个方法可以获得修改后的树结构getHierarchy()。问题是如何解析JSON并保存到数据库中。

演示JSON:

代码语言:javascript
复制
{
  "id": "1",
  "children": [
    {
      "id": "40",
      "children": [
        {
          "id": "53"
        }
      ]
    },
    {
      "id": "57",
      "children": [
        {
          "id": "72",
          "children": [
            {
              "id": "73"
            }
          ]
        }
      ]
    }
  ]
}

我想要一些类似于平面数组的东西,以便在数据库中轻松更新

代码语言:javascript
复制
(
    [0] => Array
        (
            [id] => 1
            [parentid] => 0
        )

    [1] => Array
        (
            [id] => 40
            [parentid] => 1
        )

    [2] => Array
        (
            [id] => 53
            [parentid] => 40
        )

    [3] => Array
        (
            [id] => 57
            [parentid] => 1
        )

    [4] => Array
        (
            [id] => 72
            [parentid] => 57
        )

    [5] => Array
        (
            [id] => 73
            [parentid] => 72
        )

)
EN

回答 2

Stack Overflow用户

发布于 2017-09-20 00:11:39

代码语言:javascript
复制
var jsonData = [JSON.parse('{"id":"1","children":[{"id":"40", "children":[{"id":"53"}]},{"id":"57","children":[{"id":"72","children":[{"id":"73"}]}]}]}')],
  outputData = [],
  parentId = 0;

function convert( data, parentId ){
  $.each( data, function(index, item){
    outputData.push({
      id: item.id,
      parent_id: parentId
    });

    if(item.hasOwnProperty('children')){
       convert(item.children, item.id );
    }
  });
}  

convert( jsonData, 0 );
  
console.log(outputData);
代码语言:javascript
复制
<body>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

</body>

票数 2
EN

Stack Overflow用户

发布于 2017-09-20 01:34:27

这是我的解决方案,不是最好的,但是PHP被发送到works..The,我在那里做了更新。

代码语言:javascript
复制
 $datax=json_decode($this->request->query['tree'],true);

         function tree_to_array($datas, $father = 0)
        {
            $array = Array();
            foreach ($datas as $val)
            {
                if(isset($val["id"]))
                {
                    $toSaveDB = array("id" => $val["id"], "parent_id" => $father);
                    $array[] = $toSaveDB;
                    $parent_id=$val["id"];

                    if(isset($val["children"]))
                    {
                        //Root problem.. this is only for nodes that have children
                          if($parent_id!=1)
                          {
                              $children = tree_to_array($val["children"], $parent_id);
                              if (!empty($children))
                              {
                                  $array = array_merge($array, $children);
                              }
                          }
                    }

                }
                else
                {
                    if (is_array($val))
                    {
                                    $children = tree_to_array($val, $parent_id);
                                    if (!empty($children))
                                    {
                                        $array = array_merge($array, $children);
                                    }
                    }
                 }
            }
            return $array;
        }


        $new_array=tree_to_array($datax, $father = 0);
        //Finally save to DB    
        foreach($new_array as $node)
        {

            $this->Area->id = $node['id'];
            $this->Area->saveField('parent_orgchart', $node['parent_id']);
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46303677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档