Skip to content

Nested sortables

Each item can hold its own sortable list. All levels share the same group, so items can be dragged between branches and depths - into the empty list under any item, too. An item is never dropped inside itself.

Item 1.1
Item 2.1
Item 2.2
Item 3.1
Item 3.2
Item 2.3
Item 1.2
Item 1.3
Item 2.4
Item 2.5
Item 1.4

Tree: Item 1.1 [Item 2.1, Item 2.2 [Item 3.1, Item 3.2], Item 2.3], Item 1.2, Item 1.3 [Item 2.4, Item 2.5], Item 1.4

Try it

  • Drag Item 3.1 up into the top level, between Item 1.1 and Item 1.2.
  • Drag Item 1.2 into the empty dashed area under Item 2.1: it becomes a child of 2.1, one level deeper.
  • Drag Item 1.1 over its own children: none of them takes it - an item cannot be dropped inside itself - and its whole branch moves with it wherever it does land.
  • From the keyboard, pick an item up with Space: the arrows carry it into a nested list and out again, in the direction they point.

How it works

  • NestedList is a component of the demo's own: a Bortable whose ItemTemplate renders another NestedList for the item's children. Every level has Group="nested", so any list of the tree takes an item from any other.
  • The lists inside the dragged item's own subtree, and the lists inside the drop preview, refuse it - so an item is never dropped inside itself.
  • Each list handles its own items only: a drag that starts in an inner list is not also a drag of the outer item around it.
  • A nested list takes the pointer only for a drag it could receive. One of another group, of another item type, or with Put="false" leaves the drag to the list around it - which is how the columns of a board still sort while the pointer crosses the card lists inside them.
  • Every item renders a list of its own, empty or not, with padding and a minimum height in this page's stylesheet, so an empty branch still has an area to drop into.
  • Each list is bound one way, with Items, to the item's Children, which the list changes in place. ItemsChanged only tells the page to render the tree line again.

Parameters on this page

ParameterHereWhat it does
Itemsitem.ChildrenThe children of the item the list belongs to, changed in place.
ItemsChangedOnItemsChangedRaised once per drag for each list that changed - here, to show the new tree.
GroupnestedEvery level shares it, so items move between branches and depths.
SwapThreshold0.65The value the SortableJS nested demo uses: the outer part of each item is a dead zone. See swap thresholds.
Idnested-item-1-1, ...Follows the item that owns the list, wherever the item moves.
An unhandled error has occurred. Reload 🗙