Sha256: 0c4c29fc1577f09cb64e1eb2296221a6a89009b152a1e815bd3b308178320fe6
Contents?: true
Size: 1.21 KB
Versions: 7
Compression:
Stored size: 1.21 KB
Contents
# Configure for Rails 3.1 module Dynatree class Renderer def initialize(tree, association) @tree = tree @association = association end def render() self.recurse(nil).to_json end def has_selected_children(id) items = @tree.select{ |elem| elem.parent_id == id } if items.empty? false else items.each do |item| if @association.include? item return true end if has_selected_children(item.id) return true end end false end end def recurse(parent_id) ret = [] items = @tree.select{ |elem| elem.parent_id == parent_id } unless items.empty? items.each do |item| ch = {} if @association.include? item ch['select'] = true ch['expand'] = true else if has_selected_children(item.id) ch['expand'] = true end end ch['title'] = item.name ch['key'] = item.id.to_s ch['children'] = self.recurse(item.id) ret << ch end end ret end end end
Version data entries
7 entries across 7 versions & 1 rubygems