"The @angular/cdk/drag-drop module provides you with a way to easily and declaratively create drag-and-drop interfaces, with support for free dragging, sorting within a list, transferring items between lists, animations, touch devices, custom drag handles, previews, and placeholders, in addition to horizontal lists and locking along an axis."
Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.
Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the --prod flag for a production build.
💻 Code Examples
Function to move numbered blocks from 1 list to another.
drop(event: CdkDragDrop<number[]>){
console.log(event.previousContainer.data);// initially returns array (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]console.log(event.container.data);// initially returns []if(event.previousContainer!==event.container){// check to see if moved across liststransferArrayItem(event.previousContainer.data,event.container.data,event.previousIndex,event.currentIndex);}else{// move within the same array and change indexmoveItemInArray(this.numbers,event.previousIndex,event.currentIndex);}}
🆒 Features
Drag and drop functionality - from one list to the other (with certain restrictions - e.g. you cannot move an active item back to the new items list).