我使用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]);
Google组织采取的方式:
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','PARENT_NAME','HOVER over TITLE']
如何解析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",""]]
使用上面修改过的代码。
答案 0 :(得分: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);
}
}
}
import random
def random_gen(number1, number2):
for i in range(20):
num = random.randint(number1,number2)
if num % 2 == 0:
print("Random Number", num, "is even")
else:
print("Random Number", num, "is odd")
number1 = int(input("Enter First Integer: "))
number2 = int(input("Enter Second Integer: "))
random_gen(number1, number2)