2015-10-19 58 views
0

我有存儲層次結構的項目列表遍歷:通過層次結構,以顯示項目的列表中的Java

FriendList c = 
[ 
    { 
     name:"A", 
     nickname:"a", 
     children:[ 
      { 
       name:"A1", 
       nickname:"a1", 
       children:[] 
      }, 
      { 
       name:"A2", 
       nickname:"a2", 
       children:[ 
        { 
         name:"A21", 
         nickname:"a21", 
         children:[] 
        }, 
        { 
         name:"A22", 
         nickname:"a22", 
         children:[] 
        }, 
        { 
         name:"A23", 
         nickname:"a23", 
         children:[ 
          { 
           name:"A231", 
           nickname:"a231", 
           children:[] 
          }    
         ] 
        } 
       ] 
      }, 
      { 
       name:"A3", 
       nickname:"a3", 
       children:[] 
      }, 
      { 
       name:"A4", 
       nickname:"a4", 
       children:[ 
        { 
         name:"A41", 
         nickname:"a41", 
         children:[] 
        } 
       ] 
      } 
     ] 
    } 
] 

我需要使用的瀏覽器的Javascript/AngularJS並使用,以顯示該數據Java以下列方式生成PDF:

parent_name  parent_nickname  child_name  child_nickname 

    A     a     A1     a1 
    A     a     A2     a2 
    A2    a2     A21    a21 
    A2    a2     A22    a22 
    A2    a2     A23    a23 
    A23    a23    A231    a231 
    A     a     A3     a3 
    A     a     A4     a4 
    A4    a4     A41    a41 

當前答案僅顯示前兩列的結構。

+0

Follwing答案只顯示前兩列。 –

回答

1

你所要求的僅僅是一個樹遍歷像以下(僞代碼):

void visit(Node node) { 
    if (node.children != null) { 
     for (Node n : node.children) { 
      print(n); // print name and nickname 
      visit(n); 
     } 
    } 
}