rails顯示家族樹(組織結構圖)

  • model
    一個person可以有多個son,但只有一個father。具體代碼如下:
class Person < ActiveRecord::Base
     ...
   
     has_many :son_relationships, class_name: "Relationship",
                                     foreign_key: 'father_id',
                                     dependent: :destroy
     has_one :father_relationships, class_name: "Relationship",
                                   foreign_key: 'son_id',
                                   dependent: :destroy                                  
   
     has_many :sons, through: :son_relationships, source: :son                               
     has_one :father, through: :father_relationships, source: :father
   
     ...
end
 class Relationship < ActiveRecord::Base
     belongs_to :father, class_name: 'Person'
     belongs_to :son, class_name: 'Person'
   
     validates :father_id, presence: true
     validates :son_id, presence: true
 end

這樣就能使用person.sons來查看person的兒子。以及person.father來查看person的父親。

  • 完全用css來顯示族譜(組織結構)圖。原代碼請點我
   * {
       margin: 0;
       padding: 0;
     }
   .tree{
     width:760px; 
     margin:40px auto 0 auto
     }
   .tree ul {
     padding-top: 20px; position: relative;
     
     transition: all 0.5s;
     -webkit-transition: all 0.5s;
     -moz-transition: all 0.5s;
   }
   
   .tree li {
     float: left; text-align: center;
     list-style-type: none;
     position: relative;
     padding: 20px 5px 0 5px;
     
     transition: all 0.5s;
     -webkit-transition: all 0.5s;
     -moz-transition: all 0.5s;
   }
   
   /*We will use ::before and ::after to draw the connectors*/
   
   .tree li::before, .tree li::after{
     content: '';
     position: absolute; top: 0; right: 50%;
     border-top: 1px solid #ccc;
     width: 50%; height: 20px;
   }
   .tree li::after{
     right: auto; left: 50%;
     border-left: 1px solid #ccc;
   }
   
   /*We need to remove left-right connectors from elements without 
   any siblings*/
   .tree li:only-child::after, .tree li:only-child::before {
     display: none;
   }
   
   /*Remove space from the top of single children*/
   .tree li:only-child{ padding-top: 0;}
   
   /*Remove left connector from first child and 
   right connector from last child*/
   .tree li:first-child::before, .tree li:last-child::after{
     border: 0 none;
   }
   /*Adding back the vertical connector to the last nodes*/
   .tree li:last-child::before{
     border-right: 1px solid #ccc;
     border-radius: 0 5px 0 0;
     -webkit-border-radius: 0 5px 0 0;
     -moz-border-radius: 0 5px 0 0;
   }
   .tree li:first-child::after{
     border-radius: 5px 0 0 0;
     -webkit-border-radius: 5px 0 0 0;
     -moz-border-radius: 5px 0 0 0;
   }
   
   /*Time to add downward connectors from parents*/
   .tree ul ul::before{
     content: '';
     position: absolute; top: 0; left: 50%;
     border-left: 1px solid #ccc;
     width: 0; height: 20px;
   }
   
   .tree li a{
     border: 1px solid #ccc;
     padding: 5px 10px;
     text-decoration: none;
     color: #666;
     font-family: arial, verdana, tahoma;
     font-size: 11px;
     display: inline-block;
     
     border-radius: 5px;
     -webkit-border-radius: 5px;
     -moz-border-radius: 5px;
     
     transition: all 0.5s;
     -webkit-transition: all 0.5s;
     -moz-transition: all 0.5s;
   }
   
   /*Time for some hover effects*/
   /*We will apply the hover effect the the lineage of the element also*/
   .tree li a:hover, .tree li a:hover+ul li a {
     background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
   }
   /*Connector styles on hover*/
   .tree li a:hover+ul li::after, 
   .tree li a:hover+ul li::before, 
   .tree li a:hover+ul::before, 
   .tree li a:hover+ul ul::before{
     border-color:  #94a0b4;
   }
  • 根據上面的model結構和css的樣式,將person的數據進行如下的處理:
    app/helpers/people_helper.rb
 def draw_tree(person)
   #定義一個hash,用來存儲每一個person的數據
   generation_hash ||= {}
   #定義一個hash,用來記錄某個person的下一代的<ul></ul>是否已經添加
   h ||= {}
   
   #每一個person對應的數據為一個數組,用數組是因為要讓下一代的數據插入到該person的<li></li>中
   generation_hash[person.id] = ["<li><a href=\"\#\">#{person.name}</a>","</li>"]

   #如果person有下一代,那么則進行以下操作
   unless person.sons.empty?
     #判斷該person的下一代<ul></ul>是否已經添加,如果已添加,則不再添加了
     if h[person.id].nil?
       #將下一代對應的<ul>添加到person的</a>標簽后
       generation_hash[person.id].first << "<ul>"
       #將下一代對應的</ul>添加到person的</li>前
       generation_hash[person.id].last.insert 0,"</ul>"
       #標記已經添加下一代的<ul></ul>
       h[person.id] = 1
     end
     
     person.sons.each do |son|
       #將下一代的數據插入到這一代數組的倒數第二個位置上
       #同時用到了遞歸,進行深度遍歷
       generation_hash[person.id].insert(-2, draw_tree(son))
       #這樣就形成了類似以下的格式
       #<li>
       #   <a href=\"\#\">#{person.name}</a>
       #   <ul>
       #       <li>
       #           <a href=\"\#\">#{son.name}</a>
       #       </li>
       #   </ul>
       #</li>
     end
   end
   #最后將generation_hash的values(一個嵌套的數組)進行flatten,然后再join成一個字符串返回
   generation_hash.values.flatten.join("")
 end
  • 在views中調用
<div class='tree'>
  <ul>
    <%=  sanitize draw_tree(Person.find_by(generation:1) %>
  </ul>
</div>
  • 因為家族數據變化不是特別頻繁,所以可以利用cache來緩存家族樹的頁面片段,提高頁面的讀取速度
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、垂直對齊 如果你用CSS,則你會有困惑:我該怎么垂直對齊容器中的元素?現在,利用CSS3的Transform,...
    kiddings閱讀 3,200評論 0 11
  • 1、屬性選擇器:id選擇器 # 通過id 來選擇類名選擇器 . 通過類名來選擇屬性選擇器 ...
    Yuann閱讀 1,657評論 0 7
  • 純CSS3制作二級菜單特效 基礎掌握知識:1.boder-radis圓角的制作 2.linear-gradient...
    Iris_mao閱讀 3,976評論 2 17
  • W3C標準中對css3的transition這是樣描述的:“css的transition允許css的屬性值在一定的...
    青春前行閱讀 1,433評論 0 5
  • 正所謂“三人行必有我師”,這兩天跟北京各路神仙牛人大咖交流請教,可謂腦洞大開,廖廖數語,直指問題本質核心,還帶解決...
    7ce322bedc91閱讀 511評論 0 1