2016-03-02 115 views
0

嗨,我有一個返回一個JSON對象像這樣的RESTfulAPI:重新排列angularjs一個JSON對象來創建一個嵌套的UL

{ 
data: [ 
     { 
     id: 4, 
     name: "Naranja", 
     cant_portion: 2, 
     portion_name: "Piezas", 
     foodgroup_name: "Frutas" 
     } 
     ] 
} 

,我想重新安排它,這樣我可以有這樣的事情:

{ 
data: [ 
     { 
     foodgroup_name: "Frutas", 
     items:[ 
       { 
       portion_name: "Piezas", 
       items:[ 
         { 
         id: 4, 
         name: "Naranja", 
         cant_portion: 2, 
         } 
        ] 
       } 
       ]    
     } 
     ] 
} 

這樣做的想法是,我想有一個嵌套的ul和第一個ul應該給我的食物組的名稱,然後下一個ul應該給食物組的部分的類型,並在該ul把一些李,與符合這兩個要求的項目。

現在在我的API中,我可以返回每個食物組的名稱,所以我對那個第一個ul很好,但在第二個部分去的地方,並非每個食物組都有所有的部分, 2.

+0

我們可以使用jQuery的解決方案嗎? –

+0

您在問題中添加了Laravel標記,那麼爲什麼我們不使用它來簡化? –

+0

@Reddy我不喜歡,但它會幫助 –

回答

1

一下:

private function _rearrangeData($data = array()) 
{ 
    $out = array(); 
    if (!count($data)) { 
     return $data; 
    } 
    $foodgroup = array(); 
    $portion = array(); 
    $foodgroup_name = $data[0]['foodgroup_name']; 
    $portion_name = $data[0]['portion_name']; 
    foreach ($data as $item) { 
     if ($item['foodgroup_name'] == $foodgroup_name) { 
      if ($item['portion_name'] == $portion_name) { 
       unset($item['foodgroup_name']); 
       unset($item['portion_name']); 
       array_push($portion, $item); 
      } else { 
       $arr = [ 
        'portion_name' => $portion_name, 
        'items' => $portion 
       ]; 
       $portion_name = $item['portion_name']; 
       unset($item['foodgroup_name']); 
       unset($item['portion_name']); 
       $portion = [$item]; 
       array_push($foodgroup, $arr); 
      } 
     } else { 
      $arr = [ 
       'portion_name' => $portion_name, 
       'items' => $portion 
      ]; 
      array_push($foodgroup, $arr); 

      $arr = [ 
       'foodgroup_name' => $foodgroup_name, 
       'items' => $foodgroup 
      ]; 
      $foodgroup_name = $item['foodgroup_name']; 
      $portion_name = $item['portion_name']; 
      unset($item['foodgroup_name']); 
      unset($item['portion_name']); 
      $portion = [$item]; 
      $foodgroup = []; 
      array_push($out, $arr); 
     } 
    } 
    $arr = [ 
     'portion_name' => $portion_name, 
     'items' => $portion 
    ]; 
    array_push($foodgroup, $arr); 
    $arr = [ 
     'foodgroup_name' => $foodgroup_name, 
     'items' => $foodgroup 
    ]; 
    array_push($out, $arr); 
    return $out; 
} 

編輯:與DB:

$food_group = DB::table('foods')->join('food_group', 'food_group.id', '=', 'foods.food_group_id')->groupBy('food_group_id')->get(); 
$out = []; 
foreach ($food_group as $item) { 
    $foods = DB::table('foods')->join('portions', 'portions.id', '=', 'foods.portion_id')->where('food_group_id', $item->id)->groupBy('portion_id')->get(); 
    $arr1 = []; 
    foreach($foods as $item2) { 
      $por_food = DB::table('foods')->where('food_group_id',$item->id)->where('portion_id',$item2->id)->get(); 
      $arr = [ 
       'portion_name' => $item2->name, 
       'items' => $por_food 
      ]; 
      array_push($arr1, $arr); 
    } 
    $arr2 = [ 
      'foodgroup_name'=>$item->name, 
      'items'=>$arr1 
    ]; 
    array_push($out, $arr2); 
} 
return response()->json(['data'=>$out],200); 
+0

http://codebeautify.org/jsonviewer/cb9ab918 –

+0

woow我真的很感動,那裏有一些不錯的代碼D :,只有一件事我該如何在我的控制器中實施?因爲爲了得到我給你的數組,我做出這樣的:return response() - > json(['data'=> Food :: all()],200) –

+0

添加上面的函數,然後更改'Food ::所有()'到'$ this - > _rerangerangeData(Food :: all())' –

相關問題