Now, you must be wondering what Drag and Drop Lists are, right? Well, that’s natural!

Let us assume a case where you have two lists - one for the list of items you have not done yet; i.e., the pending list, and the other one for the list of items you have successfully completed, i.e., the done list. Now, it’s natural that you move an item into the list of completed things as soon as you complete it. Moreover, you always have your priorities for the things which are there on your pending list. While using these dropdown lists, you can rearrange your priorities anytime easily.

In simple words, Drag n Drop lists give you the facility to move your items from one list to another as well as rearrange items of the same list. Well, before starting the actual implementation of it, I would like to show you one demo of what we will be implementing at the end of this article.

So, let’s now start implementation. The first step is to create a new app, install Angular Material into your app, and then run it.

Here, I am adding the screenshot of the process that the above-mentioned Material command will start.

Create Drag n Drop List In Angular Using Angular Material

Angular Material will add the BrowserAnimationsModule to your app.module.ts. Angular Material is a library developed by the Angular Team for building high-quality UI Components in Angular that are based on Google’s Material.

Earlier, to install Angular Material on your computer, you had to go through a tedious long process. Now, it is replaced by one single command.

Create Drag n Drop List In Angular Using Angular Material

So, let’s move to the next part. Open your newly created Angular application. Add this code into your app.module.ts file.

  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { AppComponent } from './app.component';
  4. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
  5. import { DragDropModule } from '@angular/cdk/drag-drop';
  6. @NgModule({
  7. declarations: [
  8. AppComponent
  9. ],
  10. imports: [
  11. BrowserModule,
  12. BrowserAnimationsModule,
  13. DragDropModule
  14. ],
  15. providers: [],
  16. bootstrap: [AppComponent]
  17. })
  18. export class AppModule { }

Now, let's learn a little about CDK. CDK gives the developers more tools to build awesome components for the web. Also, it allows us to use features of Angular Material without adopting the Material Design Visual Language.

The next step is to add the following code to your app.component.html.

  1. <div class="container">
  2. <h2>To do</h2>
  3. <div cdkDropList #todoList="cdkDropList" [cdkDropListData]="todo"
  4. [cdkDropListConnectedTo]="doneList" class="list" (cdkDropListDropped)="drop($event)">
  5. <div class="list-item" *ngFor="let item of todo" cdkDrag>{{item}}</div>
  6. </div>
  7. </div>
  8. <div class="container">
  9. <h2>Done</h2>
  10. <div cdkDropList #doneList="cdkDropList" [cdkDropListData]="done"
  11. [cdkDropListConnectedTo]="todoList" class="list" (cdkDropListDropped)="drop($event)">
  12. <div class="list-item" *ngFor="let item of done" cdkDrag>{{item}}</div>
  13. </div>
  14. </div>

Next, add the following code to your app.component.ts.

  1. import { Component } from '@angular/core';
  2. import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
  3. @Component({
  4. selector: 'app-root',
  5. templateUrl: './app.component.html',
  6. styleUrls: ['./app.component.css']
  7. })
  8. export class AppComponent
  9. {
  10. todo = [
  11. 'Get to work',
  12. 'Pick up groceries',
  13. 'Go home',
  14. 'Fall asleep'
  15. ];
  16. done = [
  17. 'Get up',
  18. 'Brush teeth',
  19. 'Take a shower',
  20. 'Check e-mail',
  21. 'Walk dog'
  22. ];
  23. drop(event: CdkDragDrop<string[]>) {
  24. if (event.previousContainer === event.container) {
  25. moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  26. } else {
  27. transferArrayItem(event.previousContainer.data,
  28. event.container.data,
  29. event.previousIndex,
  30. event.currentIndex);
  31. }
  32. }
  33. }

  1. When we want to rearrange the items within one list only
  2. When we want to move the item from one list to other.

So, that’s the only condition I am checking inside my function.

If the previous container of the item is the same as the current one, then we want to moveItemInArray of one list and if it is not the case, then we want to transferItem from one list to the other one.

So, now it should work as per the demo I have shown you earlier.

Thank you!