javascript - Output nested set for Google Visualisation -


i using baum laravel retrieve nested set, can't begin think how format google organisation chart needs it.

the outputted 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":[                            ]                      }                   ]                }             ]          }       ]    } } 

which generated with:

    team::rebuild(true);     $nodes = team::where('id', auth::user()->team_id)->first();     $nodes = $nodes->getdescendantsandself()->tohierarchy();      return view('team.index', ['nodes' => $nodes]); 

the way google organisation takes it:

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 on title']

how can parse json format google needs it?


so far have:

public function index() {     team::rebuild(true);     $nodes = team::where('id', auth::user()->team_id)->first();     $nodes = $nodes->getdescendantsandself()->tohierarchy();      $data = [];     foreach($nodes $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 $child) {             $data[] = [$child->name, $data[$this->i][0], ''];             $data = $this->parsechildnode($child, $data);         }         $this->i++;         return $data;     }     return $data; } 

but goes down 2 levels , no further :(


i getting (everything under "jing's team" incorrect):

[["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",""]] 

with above altered code.

the following code seems works use case, biggest issue $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 $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 $child) {             $data[] = [$child->name, $node->name, ''];             $this->parsenodeschildren($child, $data);         }     } } 

Comments