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来缓存家族树的页面片段,提高页面的读取速度
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 3,147评论 0 11
  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 1,610评论 0 7
  • 纯CSS3制作二级菜单特效 基础掌握知识:1.boder-radis圆角的制作 2.linear-gradient...
    Iris_mao阅读 3,924评论 2 17
  • W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的...
    青春前行阅读 1,395评论 0 5
  • 正所谓“三人行必有我师”,这两天跟北京各路神仙牛人大咖交流请教,可谓脑洞大开,廖廖数语,直指问题本质核心,还带解决...
    7ce322bedc91阅读 484评论 0 1