rails顯示家族樹(組織結(jié)構(gòu)圖)

  • model
    一個(gè)person可以有多個(gè)son撒桨,但只有一個(gè)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來顯示族譜(組織結(jié)構(gòu))圖。原代碼請點(diǎn)我
   * {
       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;
   }
  • 根據(jù)上面的model結(jié)構(gòu)和css的樣式框杜,將person的數(shù)據(jù)進(jìn)行如下的處理:
    app/helpers/people_helper.rb
 def draw_tree(person)
   #定義一個(gè)hash浦楣,用來存儲(chǔ)每一個(gè)person的數(shù)據(jù)
   generation_hash ||= {}
   #定義一個(gè)hash,用來記錄某個(gè)person的下一代的<ul></ul>是否已經(jīng)添加
   h ||= {}
   
   #每一個(gè)person對應(yīng)的數(shù)據(jù)為一個(gè)數(shù)組咪辱,用數(shù)組是因?yàn)橐屜乱淮臄?shù)據(jù)插入到該person的<li></li>中
   generation_hash[person.id] = ["<li><a href=\"\#\">#{person.name}</a>","</li>"]

   #如果person有下一代振劳,那么則進(jìn)行以下操作
   unless person.sons.empty?
     #判斷該person的下一代<ul></ul>是否已經(jīng)添加,如果已添加油狂,則不再添加了
     if h[person.id].nil?
       #將下一代對應(yīng)的<ul>添加到person的</a>標(biāo)簽后
       generation_hash[person.id].first << "<ul>"
       #將下一代對應(yīng)的</ul>添加到person的</li>前
       generation_hash[person.id].last.insert 0,"</ul>"
       #標(biāo)記已經(jīng)添加下一代的<ul></ul>
       h[person.id] = 1
     end
     
     person.sons.each do |son|
       #將下一代的數(shù)據(jù)插入到這一代數(shù)組的倒數(shù)第二個(gè)位置上
       #同時(shí)用到了遞歸历恐,進(jìn)行深度遍歷
       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(一個(gè)嵌套的數(shù)組)進(jìn)行flatten,然后再join成一個(gè)字符串返回
   generation_hash.values.flatten.join("")
 end
  • 在views中調(diào)用
<div class='tree'>
  <ul>
    <%=  sanitize draw_tree(Person.find_by(generation:1) %>
  </ul>
</div>
  • 因?yàn)榧易鍞?shù)據(jù)變化不是特別頻繁专筷,所以可以利用cache來緩存家族樹的頁面片段弱贼,提高頁面的讀取速度
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市磷蛹,隨后出現(xiàn)的幾起案子吮旅,更是在濱河造成了極大的恐慌,老刑警劉巖味咳,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件庇勃,死亡現(xiàn)場離奇詭異,居然都是意外死亡槽驶,警方通過查閱死者的電腦和手機(jī)责嚷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來掂铐,“玉大人罕拂,你說我怎么就攤上這事揍异。” “怎么了聂受?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵蒿秦,是天一觀的道長。 經(jīng)常有香客問我蛋济,道長棍鳖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任碗旅,我火速辦了婚禮渡处,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘祟辟。我一直安慰自己医瘫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布旧困。 她就那樣靜靜地躺著醇份,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吼具。 梳的紋絲不亂的頭發(fā)上僚纷,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音拗盒,去河邊找鬼怖竭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛陡蝇,可吹牛的內(nèi)容都是我干的痊臭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼登夫,長吁一口氣:“原來是場噩夢啊……” “哼广匙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起恼策,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤艇潭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后戏蔑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鲁纠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年总棵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片改含。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡情龄,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骤视,我是刑警寧澤鞍爱,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站专酗,受9級(jí)特大地震影響睹逃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜祷肯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一沉填、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧佑笋,春花似錦翼闹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蜀备,卻和暖如春关摇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琼掠。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國打工拒垃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓷蛙。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓悼瓮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親艰猬。 傳聞我的和親對象是個(gè)殘疾皇子横堡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容