From ca7b58d362e07da86684952d05ecffa1827aa399 Mon Sep 17 00:00:00 2001 From: Muhammad Sulaiman Yusuf Date: Wed, 7 Sep 2022 17:04:04 +0700 Subject: [PATCH] implement hierarchy --- app/Http/Controllers/DivisiController.php | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/DivisiController.php b/app/Http/Controllers/DivisiController.php index 06a533c..ea7c3d4 100644 --- a/app/Http/Controllers/DivisiController.php +++ b/app/Http/Controllers/DivisiController.php @@ -9,7 +9,8 @@ class DivisiController extends Controller { private function getAllChildren($divisi, $depth = 0, $array = []) { - $array[$divisi->id] = $divisi->name; + $divisi->depth = $depth; + array_push($array, $divisi); foreach($divisi->children as $child){ $array = $this->getAllChildren($child, $depth + 1, $array); } @@ -69,20 +70,10 @@ class DivisiController extends Controller return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=> 200], 200); } - public function search(Request $request) + public function search() { - $payload = $request->all(); - - if($payload['columns'][0]['value'] == "") - $this->list(); - - $dataBuilder = $this->setUpPayload($payload, 'm_divisi'); - $builder = $dataBuilder['builder']; - $countBuilder = $dataBuilder['count']; - $dataGet = $builder->get(); - $totalRecord = $countBuilder->count(); - - return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + return $this->list(); + // cant use builder for this case } public function list() @@ -91,7 +82,13 @@ class DivisiController extends Controller $divisions = []; foreach($parentMenus as $menu){ $childs = $this->getAllChildren($menu); - $divisions = $divisions + $childs; + foreach($childs as $d){ + $d->displayName = ' ' . $d->name; + for($i=0; $i < $d->depth; $i++){ + $d->displayName = '--' . $d->displayName ; + } + array_push($divisions, $d); + } } $countData = count($divisions); if($countData == 0)