2012-07-31 289 views
0

在我看來,我得到所有頁面

我得到我所有的頁面以及所有的子項和子項。如何獲得樹狀結構如下圖所示:如何動態構建樹狀結構

<% for page in @pages%> 
<li id="<%= page.id%>_page">  
    <div class="link">  
     #my attributes for the div< 
     /div>  
#here I got the all siblings of that page. But here the structure is of just two  
#levels. I need upto n-levels.  
<% @childs = page.*descendants* %>  
<% if [email protected]? && [email protected]? %> 
    <% for child in @childs%>  
    <ol class="child"> 
     <li id="<%= child.id%>_page"> 
      <div class="link"> 
       #my attributes for the div 
      </div> 
      </li> 
    </ol> 
    <%end%> 
    <%end%> 
</li> 
<%end%> 

我使用嵌套集,並希望這樣的結構:

page1 
    page 2 
    page 3 
      page 3.1 
        page 3.2 
          page3.4 
            ...so on to last child 
page 4 
page 5 
....so on to N-levels... 

回答

0

你想創建一個遞歸視圖中,這不只是很複雜,會混亂你的視圖代碼。 一個乾淨的方法是定義一個遞歸生成輸出的幫助函數。

我沒有任何具體的例子,但是你可以在this similar question

+0

嘗試..我得到了解決之後...我呈現局部而這又使itself..such爲 – Milind 2012-08-02 09:47:56

0

答案的一個嘗試..我得到了 解決方案之後檢查一個...我呈現局部而這又使自身..such作爲_show_children.html.erb ..

<% for page in @pages%> 
    <li id="<%= page.id%>_page">  
     <div class="link">  
      #my attributes for the div< 
      /div>  
    #here I got the all siblings of that page. But here the structure is of just two  
    #levels. I need upto n-levels.  
    <% @childs = page.descendants %>  
    <% if [email protected]? && [email protected]? %> 
     <% for child in @childs%>  
     <%= render :partial => "show_children", :locals => {:children => child`enter code here`.children }%> 
     <%end%> 
     <%end%> 
    </li> 
<%end%> 


.this partial will inturn render itself`