Rails 5:来自日期哈希的JS树视图

时间:2017-01-08 07:39:45

标签: javascript ruby-on-rails arrays json ruby

我有模型,其中找到不同的日期,然后按年,月,日分组值:

def self.tree
  dates = MyModel.order("date_trunc('day', datetime_column) DESC")
           .distinct.pluck("date_trunc('day', datetime_column)")
  hash = Hash.new { |h, y| h[y] = Hash.new { |h2, m| h2[m] = {} } }
  dates_by_ymd = dates.each_with_object(hash) do |date, h|
    h[date.year][date.strftime('%B')][date.strftime('%b %d')] = date
  end
end

打印出来的哈希:

{2016=>{"November"=>{"Nov 20"=>Sun, 20 Nov 2016 05:20:00 UTC +00:00}, "December"=>{"Dec 12"=>Mon, 12 Dec 2016 04:05:00 UTC +00:00, "Dec 24"=>Sat, 24 Dec 2016 18:51:33 UTC +00:00, "Dec 30"=>Fri, 30 Dec 2016 06:20:00 UTC +00:00}}, 2017=>{"December"=>{"Dec 12"=>Tue, 12 Dec 2017 15:15:00 UTC +00:00}}}

在控制器中我有这个:

def update_placements
  @placements = #code which finds placements
  @treeview = @placements.tree
  respond_to do |format|
    format.js
  end
end

如何将上面的哈希变成一些不错的javascript树视图?对于example like hereenter image description here

我很欣赏任何我应该注意的提示。谢谢!

加成

此外,我需要能够获取特定日期的记录。基本上它将打开年,月,日,然后填写特定日的记录。我想要一起加载年,月,日,然后每天记录将被单独加载,因为表可能会变得非常大。当天的记录可以找到购买datetime_column

更新

在视图中有这个代码填充树视图:

<div id="tree">
    <script>
       $('#tree').jstree({
           'core' : {
               'data' : [ <%= raw @treeview %> ]
              }
          });
    </script>
</div>

我如何将多年(在我2017年,2017年的例子中)作为根节点?我想摆脱“root”。

enter image description here

JS与data这样:

$('#tree').jstree({
    'core' : {
        'data' : [ { 'text' : 'Child 1' }, 'Child 2' ]
         }
});

会像这样摆脱“节点”:

enter image description here

和“孩子1”,“孩子2”......我需要填充我的2016年,2017年等等。上面的JS中的“文本”就是你的代码中的“数据”。

1 个答案:

答案 0 :(得分:1)

获得dates_ymd后:

{2017=>
  {1=>
    {8=>#<Date: 2017-01-08 ((2457762j,0s,0n),+0s,2299161j)>,
     3=>#<Date: 2017-01-03 ((2457757j,0s,0n),+0s,2299161j)>}},
 2015=>
  {12=>{5=>#<Date: 2015-12-05 ((2457362j,0s,0n),+0s,2299161j)>},
   11=>{7=>#<Date: 2015-11-07 ((2457334j,0s,0n),+0s,2299161j)>}}}

您可以使用此方法:

def to_js_tree(hash = tree, node = :root)
  hash.each_with_object({data: node}){|(k,v), new_hash|
    new_hash[:children] ||= []
    if v.is_a?(Hash)
      new_hash[:children] << to_js_tree(v,k)
    else
      new_hash[:children] << {data: k, :children => [{data: v}]}
    end
  }
end

示例:

pp to_js_tree(dates_by_ymd)
# =>
{:data=>:root,
 :children=>
  [{:data=>2017,
    :children=>
     [{:data=>1,
       :children=>
        [{:data=>8,
          :children=>
           [{:data=>#<Date: 2017-01-08 ((2457762j,0s,0n),+0s,2299161j)>}]},
         {:data=>3,
          :children=>
           [{:data=>#<Date: 2017-01-03 ((2457757j,0s,0n),+0s,2299161j)>}]}]}]},
   {:data=>2015,
    :children=>
     [{:data=>12,
       :children=>
        [{:data=>5,
          :children=>
           [{:data=>#<Date: 2015-12-05 ((2457362j,0s,0n),+0s,2299161j)>}]}]},
      {:data=>11,
       :children=>
        [{:data=>7,
          :children=>
           [{:data=>
              #<Date: 2015-11-07 ((2457334j,0s,0n),+0s,2299161j)>}]}]}]}]}

puts to_js_tree(dates_by_ymd).to_json
#=> {"data":"root","children":[{"data":2017,"children":[{"data":1,"children":[{"data":8,"children":[{"data":"2017-01-08"}]},{"data":3,"children":[{"data":"2017-01-03"}]}]}]},{"data":2015,"children":[{"data":12,"children":[{"data":5,"children":[{"data":"2015-12-05"}]}]},{"data":11,"children":[{"data":7,"children":[{"data":"2015-11-07"}]}]}]}]}

在您的情况下,您可以致电:

@placements.to_js_tree.to_json

如果您已定义

def self.to_js_tree(hash = tree, node = :root)

在您的展示位置模型中。

更新

如果您不希望有多年的子节点作为子节点,但是需要多个树和多年作为根的数组,则可以添加新的to_js_trees方法:

def to_js_trees(hash = tree)
  hash.map{|year, year_tree|
    to_js_tree(year_tree, year)
  }
end

注意:@treeview现在已经是一个数组,因此您只需使用'data' : <%= raw @treeview %>代替'data' : [ <%= raw @treeview %> ]