duanhunlou7051 2017-12-12 16:48
浏览 117

如何使用PHP生成fancytree json格式

please i use fancytree for the first time, and i found some difficults in generating a Json result for fancytree. i have a database table contain id, name, desc, parent_id.

i'm working on codeigniter this is my code :

public function my_tree(){
    $this->data['tree'] = array();
    $res = $this->crud->read('dbtree')->result_array();
//iterate on results row and create new index array of data
    foreach($res as $value){
        $this->data['tree'] = $res;
    }
$itemsByReference = array();
    // Build array of item references:
    foreach($this->data['tree'] as $key => &$item) {
       $itemsByReference[$item['id']] = &$item;
       // Children array:
       $itemsByReference[$item['id']]['children'] = array();
       // Empty data class (so that json_encode adds "data: {}" )
       $itemsByReference[$item['id']]['data'] = new StdClass();
    }

    // Set items as children of the relevant parent item.
    foreach($this->data['tree'] as $key => &$item)
       if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
          $itemsByReference [$item['parent_id']]['children'][] = &$item;

    // Remove items that were added to parents elsewhere:
    foreach($this->data['tree'] as $key => &$item) {
       if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
          unset($this->data['tree'][$key]);
    }
    // Encode:
    $this->data['page'] = "server_tree";
    $this->load->view('layout', $this->data);
}

How can i make this keys using php loop:

[
{"title": "Expanded folder with children", "expanded": true, "folder": true, "children": [
    {"key": "1_2", "title": "Expanded sub-item", "expanded": true, "children": [
        {"key": "1_2_1", "title": "Active sub-item (active and focus on init)", "active": true, "focused": true},
        {"key": "1_2_2", "title": "Basic <i>menu item</i> with <strong class='text-semibold'>HTML support</strong>"}
    ]},
    {"key": "1_3", "title": "Expanded sub-item", "children": [
        {"key": "1_3_1", "title": "Sub-item 2.2.1"},
        {"key": "1_3_2", "title": "Sub-item 2.2.2"}
    ]}
]},
{"key": "2", "title": "Menu item with key and tooltip", "extraClasses": "has-tooltip", "tooltip": "Look, a tool tip!"},
{"key": "3", "title": "Collapsed folder", "folder": true, "children": [
    {"key": "3_1", "title": "Sub-item 1.1"},
    {"key": "3_1", "title": "Sub-item 1.2"}
]},
{"key": "4", "title": "This is a selected item", "selected": true},
{"key": "5", "title": "Document with some children (expanded on init)", "expanded": true, "children": [
    {"key": "5_1", "title": "Document sub-item"},
    {"key": "5_2", "title": "Another document sub-item", "children": [
        {"key": "5_2_1", "title": "Sub-item 2.1.1"},
        {"key": "5_2_2", "title": "Sub-item 2.1.2"}
    ]}
]}

]

  • 写回答

1条回答 默认 最新

  • dpx86402 2017-12-12 23:44
    关注

    You could use the Marshal Serializer to easily create the structure.

    Then pass the generated array to json_encode function and then use it for fancytree.

    TreeMapper.php

    namespace MyApp\Mapper;
    
    use KingsonDe\Marshal\AbstractMapper;
    use MyApp\Data\Tree;
    
    class TreeMapper extends AbstractMapper {
    
        public function map(Tree $tree) {
            $output = [
                'key'   => $tree->getKey(),
                'title' => $tree->getTitle(),
            ];
    
            if ($tree->isFolder()) {
                $output['folder']   = true;
                $output['children'] = $this->collection($this, $tree->getChildren()); 
            }
    
            return $output;
        }
    }
    

    fancytree.php

    use KingsonDe\Marshal\Marshal;
    use MyApp\Mapper\TreeMapper;
    
    $treeCollection = getFancyTreeCollection();
    
    $data = Marshal::serializeCollection(new TreeMapper(), $treeCollection);
    
    echo json_encode($data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?