Quick start
This page builds a sortable list of 1,000 tasks. <vdnd-sortable-list> combines the drop target, the virtual scroller and the drop placeholder, so it is the right starting point for most apps.
The code
What each part does
vdndGroup="tasks"names the group that draggables and drop targets belong to. Items can only move between drop targets in the same group. A group is required even for a single list; without one, dragging is disabled.- The item template is declared inside the
vdndGroupelement. Angular templates resolve dependency injection where they are declared, so this is how each draggable finds its group. [vdndDraggable]="task.id"makes each row draggable. The ID must equal whatitemIdFnreturns for that item, and it must sit on the template's root element.<vdnd-sortable-list>renders the visible rows.itemHeightis the row height in pixels andcontainerHeightis the height of the scroll area.(drop)fires when an item is dropped. The library never changes your data.reorderItems()applies the move to thetaskssignal.<vdnd-drag-preview />renders the element that follows the pointer. Place it once, anywhere in the template.
Every draggable can be focused with Tab. Space picks it up, the arrow keys move it, and Space or Enter drops it. See Accessibility.
Styles
The library does not style rows or the drop placeholder, so the code above renders plain text rows and an invisible placeholder. Add at least this CSS:
With fixed heights, every row must be exactly itemHeight pixels tall, borders and padding included. Otherwise the rows on screen drift away from the positions the list calculates, and items drop in the wrong place.
For rows that differ in height, see Dynamic item heights. Styling lists every class the library adds.
Next steps
- Core concepts: what groups, draggables and droppables are, and the rules that keep them working
- Multiple lists: move items between lists
- Choosing a component: when to use something other than
<vdnd-sortable-list>