我正在使用Baum for Laravel来检索嵌套集,但我无法开始考虑如何将其转换为我的Google组织结构图所需的格式。
输出的JSON:
{
"1":{
"id":1,
"parent_id":null,
"lft":1,
"rgt":10,
"depth":0,
"name":"Jing's Team",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"2016-01-24 18:32:22",
"children":[
{
"id":2,
"parent_id":1,
"lft":2,
"rgt":3,
"depth":1,
"name":"Ann's Team",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"2016-01-24 18:32:22",
"children":[
]
},
{
"id":3,
"parent_id":1,
"lft":4,
"rgt":9,
"depth":1,
"name":"Mike's Team",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"2016-01-24 18:32:22",
"children":[
{
"id":4,
"parent_id":3,
"lft":5,
"rgt":8,
"depth":2,
"name":"Laidy's Team",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"2016-01-24 18:32:22",
"children":[
{
"id":5,
"parent_id":4,
"lft":6,
"rgt":7,
"depth":3,
"name":"Steve's Team",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"2016-01-24 18:32:22",
"children":[
]
}
]
}
]
}
]
}
}它是通过以下方式生成的:
Team::rebuild(true);
$nodes = Team::where('id', Auth::user()->team_id)->first();
$nodes = $nodes->getDescendantsAndSelf()->toHierarchy();
return view('team.index', ['nodes' => $nodes]);谷歌组织对此的看法是:
data.addRows([
[{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'},
'', 'The President'],
[{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'},
'Mike', 'VP'],
['Sam', 'Mike', ''],
['Bob', 'Jim', 'Bob Sponge'],
['Phil', 'Jim', 'Bob Sponge'],
['Carol', 'Bob', '']
]);“NAME”、“父名”、“悬停在标题上”
如何解析JSON才能将其转换为Google需要的格式?
到目前为止,我已经:
public function index()
{
Team::rebuild(true);
$nodes = Team::where('id', Auth::user()->team_id)->first();
$nodes = $nodes->getDescendantsAndSelf()->toHierarchy();
$data = [];
foreach($nodes as $node) {
$data[] = [$node->name, '', ''];
$data = $this->parseChildNode($node, $data);
}
return view('team.index', ['nodes' => json_encode($data)]);
}
protected $i = 0;
protected function parseChildNode($node, $data)
{
if ($node->children()->count() > 0) {
foreach ($node->children as $child) {
$data[] = [$child->name, $data[$this->i][0], ''];
$data = $this->parseChildNode($child, $data);
}
$this->i++;
return $data;
}
return $data;
}但它只下降了两个层次,没有进一步的:
我现在得到(一切都在“静的团队”下,这是不正确的):
[["Jing's Team","",""],["Ann's Team","Jing's Team",""],["Mike's Team","Jing's Team",""],["Laidy's Team","Jing's Team",""],["Steve's Team","Jing's Team",""]]上面修改的代码。
发布于 2016-01-27 23:22:59
下面的代码似乎适用于您的用例,最大的问题是$data[$this->i][0]
public function index()
{
Team::rebuild(true);
$node = Team::where('id', Auth::user()->team_id)->first();
$nodes = $nodes->getDescendantsAndSelf()->toHierarchy();
$data = [];
foreach($nodes as $node) {
$data[] = [$node->name, '', ''];
$data = $this->parseNodesChildren($node, $data);
}
return view('team.index', ['nodes' => json_encode($data)]);
}
protected function parseNodesChildren($node, &$data)
{
if ($node->children->count() > 0) {
foreach ($node->children as $child) {
$data[] = [$child->name, $node->name, ''];
$this->parseNodesChildren($child, $data);
}
}
}https://stackoverflow.com/questions/34979819
复制相似问题