2012-07-31 238 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`