递归生成树

ExpandedBlockStart.gif 代码
class  Product  <  ActiveRecord::Base
  acts_as_tree :order 
=>   " name "

  def self.roots 
    self.find(:all,:conditions 
=>  [ " parent_id=? " , 0 ]) 
  end
  
  def is_leaf
?  
    
return   ! self.has_children ?    
  end 
  
  def is_root
?  
    
return  self.parent_id  ==   0  
  end 
end

 

ExpandedBlockStart.gif 代码
module ProductsHelper
  def get_tree(trees) 
    tree_html 
=   ""
    trees.each 
do   | tree |  
      
#if  tree.is_root?
        tree_html 
+=   "
  • #{tree.name}    #{link_to('修改',edit_admin_product_path(tree.id))} "
          #end

          
    if   ! tree.children.empty ?  then
            tree_html 
    +=   "
      "   +  get_tree(tree.children)  +   "
    "
          end
          tree_html 
    +=   "
  • "
        end 
        
    return  tree_html 
      end 
    end

     

     

    view中调用:

     

    < ul id = " browser "   class = " filetree " >
    <%=  get_tree(@products) %>
    ul >

     

     

    转载于:https://www.cnblogs.com/viaivi/archive/2010/03/10/1682841.html

    你可能感兴趣的:(递归生成树)