Open rwillrich opened 9 years ago
I think max levels is use purely when nesting tree nodes together as a method of limiting the user from building an overly deep tree. See nestedSortable's doc for a better explanation.
The max levels configuration option isn't used when rendering the ol
or li
elements (see the IndexAsSortable view). That means the entire tree is output regardless of the max_levels
configuration.
What is it you want to do?
I simply wanted to limit the depth of items being rendered.
In this case, I just want to make 2 levels sortable.
Is there any other way to make this?
Unfortunately not without modifying the IndexAsSortable view. I'm reviewing the implementation now to see if there is a way to expose the logic behind tree traversal/rendering which would achieve your goal.
What is your thought on the following API to control child rendering?
ActiveAdmin.register Category do
sortable(
tree: true,
render_child?: -> (child) { child.depth < 2 }
)
index as: :sortable do
label :name
actions
end
end
Here each child node is passed to the render_child?
block to determine whether or not it (and its sub-tree) should be rendered.
It's exactly what I need!
Another downside to conditionally rendering individual children is that child sorting can become inconsistent for excluded children. Due to the way sorting is performed, which is done by sending the current displayed sort order to the server for persistence, if a child is missing it will become incorrectly sorted compared to its siblings.
An alternative would be to check if a node's children should be rendered instead of a specific child. This avoids the issue of not rendering all of a child's siblings.
Just more thoughts on the subject... :bulb:
An update to the proposed API would be to check if a node's children should be rendered...
ActiveAdmin.register Category do
sortable(
tree: true,
render_children_of?: -> (node) { node.depth < 2 }
)
index as: :sortable do
label :name
actions
end
end
I'm using sortable tree in a ActiveAdmin, in Rails 4.2, with the following:
But when visit the index, I see all levels, which are 3. Looking at the HTML, the data-* attributes were set appropriately:
Is there any problem with the options I passed?