Angular 可排序列表与拖放 | ng-hub-ui-sortable

Angular 可排序列表和拖放工具,适用于数组、FormArray 集成、Signals 工作流和互联列表。

交互式示例

探索实时示例,了解该库在常见 Angular 用例中的表现。

简单数组

Sortable - Simple Arrays

Make any array sortable with a single directive. Every layout below shares the same model, so each drag updates them all.

Button groups
Cards
Ankara
Ankara description
Moscow
Moscow description
Munich
Munich description
Paris
Paris description
Washington
Washington description
Navigation
Current model
[ "Ankara", "Moscow", "Munich", "Paris", "Washington" ]

代码

FormArray 集成

Sortable - Reactive FormArray

Bind directly to a reactive FormArray. Edit the inputs and drag to reorder — the form value always reflects the current order.

Current FormArray value:

[ "Ankara", "Moscow", "Munich", "Paris", "Washington" ]

代码

自定义选项

Sortable - Options

Pass any SortableJS options through the directive: filter non-draggable items, hook into events and tune autoscroll.

Filtered (disabled) items
  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
[ "1", "2", "3", "4", "5" ]
Events
Updated 0 times
  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
Autoscroll

Drag near the edges of the list to trigger autoscroll.

  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
  • Element 6
  • Element 7
  • Element 8
  • Element 9
  • Element 10
  • Element 11
  • Element 12
  • Element 13
  • Element 14
  • Element 15
  • Element 16
  • Element 17
  • Element 18
  • Element 19
  • Element 20
  • Element 21
  • Element 22
  • Element 23
  • Element 24
  • Element 25
  • Element 26
  • Element 27
  • Element 28
  • Element 29
  • Element 30

代码

多个列表

Sortable - Multiple Lists

Connect lists with the group option to transfer items across containers. Build kanban boards, clone factories and constrained drop targets.

Transfer between lists
  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
[ "1", "2", "3", "4", "5" ]
  • Element 6
  • Element 7
  • Element 8
  • Element 9
  • Element 10
[ "6", "7", "8", "9", "10" ]
Clone items

List 1 is a clone factory; list 2 is its target.

  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
  • Element 6
  • Element 7
  • Element 8
  • Element 9
  • Element 10
Constrained groups
1. Cannot accept items
  • Element 1
  • Element 2
  • Element 3
  • Element 4
  • Element 5
2. Normal list
  • Element 6
  • Element 7
  • Element 8
  • Element 9
  • Element 10
3. Clones children
  • Element 11
  • Element 12
4. Only #1 can drop here
  • Element 13

代码

布局构建器

Sortable - Layout Builder

Build hierarchical, nested drag-and-drop layouts with grouped containers for generics, tabs and widgets.

Build advanced visual layout builders with nested drag and drop in Angular. This powerful example demonstrates nested sortable lists with drag handles and component slots, perfect for creating page builders and dashboard designers.

Create hierarchical structures with drag-and-drop functionality—ideal for layout builders, dashboard organizers, and visual editors. Supports multi-level nesting with full control over handles and constraints.

Demo: Generic, Tabs & Widgets

Generic A
Widget 1
Widget 2
Tabs Container
Generic B
Widget 3
Generic C
Generic D
Widget 4
Generic E
Move log

No events yet.

代码

Signals

Sortable - Signals

Bind a writable signal as the sortable model. Reordering updates the signal value instantly, keeping reactive state in sync.

  • Angularv18
  • Reactv19
  • Vuev3
  • Sveltev5

Current signal value:

[ "Angular", "React", "Vue", "Svelte" ]

代码

手动模式

Sortable - Manual Mode

Disable automatic array updates and apply each reorder yourself with the moveItemInArray and transferArrayItem helpers.

Single List - Manual Reordering

Drag and drop tasks to reorder them. Notice how we manually update the array using the moveItemInArray helper function.

Task List
  • Design UI mockups
  • Implement authentication
  • Write unit tests
  • Deploy to production
Current State:
[
  {
    "id": 1,
    "title": "Design UI mockups",
    "completed": false
  },
  {
    "id": 2,
    "title": "Implement authentication",
    "completed": false
  },
  {
    "id": 3,
    "title": "Write unit tests",
    "completed": false
  },
  {
    "id": 4,
    "title": "Deploy to production",
    "completed": false
  }
]

Multiple Lists - Kanban Board Style

Drag tasks between different status columns. The transferArrayItem helper makes it easy to move items between arrays while maintaining full control.

To Do 3
  • Review pull requests
    Priority: high
  • Update documentation
    Priority: medium
  • Refactor legacy code
    Priority: low
In Progress 2
  • Fix bug #123
    Priority: high
  • Add new feature
    Priority: medium
Done 1
  • Initial setup
    Priority: high

Operations Log

No operations yet. Start dragging items to see the log!

Code Example

Component Handler
import { moveItemInArray, transferArrayItem } from 'ng-hub-ui-sortable';

onTasksUpdate(event: SortableEvent): void {
  // Manually update the array with full control
  moveItemInArray(this.tasks, event.oldIndex, event.newIndex);
  
  // You can add validation, API calls, etc. here
  console.log('Task reordered!');
}

onListAdd(event: SortableEvent, targetList: any[], listName: string): void {
  const sourceList = this.getListByElement(event.from);
  
  // Transfer item between arrays
  transferArrayItem(sourceList, targetList, event.oldIndex, event.newIndex);
  
  // Perfect place for API calls to persist the change
  this.apiService.updateTaskStatus(item.id, listName);
}

Why Use Manual Control?

Validation

Validate changes before updating your data model. Prevent invalid operations or apply business rules.

API Integration

Make API calls to persist changes immediately. Handle success/error responses appropriately.

Undo/Redo

Implement undo/redo functionality by maintaining operation history before committing changes.

Immutability

Use immutable data patterns with OnPush change detection for optimal performance.

代码