Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max_levels not being respected #50

Open
rwillrich opened this issue May 21, 2015 · 7 comments
Open

max_levels not being respected #50

rwillrich opened this issue May 21, 2015 · 7 comments

Comments

@rwillrich
Copy link

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
Copy link
Owner

zorab47 commented May 24, 2015

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
Copy link
Author

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
Copy link
Owner

zorab47 commented May 30, 2015

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
Copy link
Owner

zorab47 commented May 30, 2015

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
Copy link
Author

It's exactly what I need!

@zorab47
Copy link
Owner

zorab47 commented Apr 6, 2016

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... 💡

@zorab47
Copy link
Owner

zorab47 commented Apr 6, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants