Thanks so much Sergio. You are really great!
That totally worked!
Here is what I came up with, and perhaps this will help someone else.
def active_scaffold_grouped_options(column, select_options, optgroup)
new_select_options = []
Category.roots.each do |r|
ary = []
found_children = false
r.children.each do |c1|
if c1.children.present?
found_children = true
c1.children.map {|child| ary << ["#{child.parent.name} ->
#{child.name}", child.id]}
else
ary << ["#{c1.name}", c1.id ]
end
end
new_select_options << [r.name, ary]
end
return new_select_options #, [@record.categories.pluck(:id)]
end
The only problem was that the catagories went 2x deep and the optgroup
only can go 1x as far as I know.
If someone else knows better here is the a method that creates the
options using a recursive method:
def active_scaffold_grouped_options(column, select_options, optgroup)
new_select_options = []
#Category.walk_tree { |cat, level| puts "CAT #{cat.name} LEVEL #{level}" }
Category.roots.each do |r|
new_select_options << child_array(r)
end
binding.pry
return new_select_options #, [@record.categories.pluck(:id)]
end
# IF MULTIDIM WORKED WITH OPTGROUP
def child_array(r, level=0)
puts "DEPTH #{level}"
return [r.name, r.children.map{ |c|
c.children.present? ? child_array(c, level+1)
: level > 0 ? ["#{c.parent.name} -> #{c.name}", c.id] :
[c.name, c.id] } ]
end
On Fri, May 29, 2015 at 2:43 AM, Sergio Cambra <[email protected]> wrote:
> ActiveScaffold supports enabling groups in select defining :optgroup in
> columsn options:
>
>
>
> config.columns[:column_name].options[:optgroup] = :column_to_group
>
>
>
> When optgroup option is set, active_scaffold_grouped_options(column,
> select_options, optgroup) will be called to return options for select
> grouped by optgroup column.
>
>
>
> As you need to group using tree structure instead of grouping by column, you
> could override active_scaffold_grouped_options(column, select_options,
> optgroup) helper method to return options as a tree. You only need to enable
> calling active_scaffold_grouped_options setting :optgroup to true for
> example:
>
>
>
> config.columns[:column_name].options[:optgroup] = true
>
>
>
> El Jueves, 28 de mayo de 2015 10:59:15 loveand light escribió:
>
>> HI,
>
>>
>
>> Does anyone know a good way to display the options in an acts_as_tree
>> list?
>
>> I am trying to display categories that go 3 layers deep looks like;
>
>>
>
>> Option 1
>
>> Option 1.1
>
>> Option 1.2
>
>> Option 1.3
>
>> Option 2
>
>> Option 2.1
>
>> Option 2.2
>
>> Option 2.2.1
>
>>
>
>> I am currently using the form_ui :chosen which is wonderful for other
>> lists
>
>> except with acts_as_tree need to display the listings like the above for
>
>> selection for a has_many relationship.
>
>>
>
>> Any advice would be totally appreciated!
>
>> Thanks in advance.
>
>> Mark
>
>
--
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails Gem" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/activescaffold.
For more options, visit https://groups.google.com/d/optout.