【问题标题】:how can i generate fancytree json format using php我如何使用php生成fancytree json格式
【发布时间】:2017-12-13 07:49:24
【问题描述】:

请我第一次使用fancytree,我发现在为fancytree生成Json结果时有些困难。 我有一个包含 id、name、desc、parent_id 的数据库表。

我正在使用 codeigniter 这是我的代码:

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);
}

如何使用 php 循环制作此键:

[
{"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"}
    ]}
]}

]

【问题讨论】:

  • 请阅读:stackoverflow.com/help/how-to-ask 简而言之:你想做什么,你尝试了什么,你得到了什么结果。这些细节应该在问题的正文中。顺便说一句:没有人会去异地链接。与您的问题相关的所有内容都应该在问题本身中。
  • 谢谢你我已经编辑了我的问题,正如我告诉你的那样,我不知道如何生成 fancytree json 格式

标签: php jquery mysql json fancytree


【解决方案1】:

您可以使用Marshal Serializer 轻松创建结构。

然后将生成的数组传递给json_encode函数,然后将其用于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);

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 2020-06-20
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多