zorab47 / active_admin-sortable_tree

Show ActiveAdmin index as a nested tree with drag'n'drop
MIT License
163 stars 127 forks source link

max_levels not being respected #50

Open rwillrich opened 9 years ago

rwillrich commented 9 years ago

I'm using sortable tree in a ActiveAdmin, in Rails 4.2, with the following:

sortable tree: true,
      roots_collection: -> { Taxon.categories.roots },
      max_levels: 2

But when visit the index, I see all levels, which are 3. Looking at the HTML, the data-* attributes were set appropriately:

<ol
    class="ui-sortable"
    data-sortable-type="tree"
    data-sortable-url="/admin/contexts/1/categorias/sort"
    data-max-levels="2">

Is there any problem with the options I passed?

zorab47 commented 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?

rwillrich commented 9 years ago

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?

zorab47 commented 9 years ago

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.

zorab47 commented 9 years ago

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.

rwillrich commented 9 years ago

It's exactly what I need!

zorab47 commented 8 years ago

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:

zorab47 commented 8 years ago

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