Introduction

In this article, we will learn how to drag and drop table columns Angular 10 applications.
Prerequisites
  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Node and NPM installed
  • Bootstrap
Create an Angular project by using the following command.
  1. ng new sdemo
Open this project in Visual Studio Code and install bootstrap by using the following command.
  1. npm install bootstrap --save
Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
  1. @import '~bootstrap/dist/css/bootstrap.min.css';
Now install Table-dragger library using the following command.
  1. npm install table-dragger --save
Now create a new component by using the following command.
  1. ng g c dragtable
Now open dragtable.component.html file and add the following code:
  1. <div>
  2. <div class="row" style="margin-top:10px;margin-bottom: 10px;">
  3. <div class="col-sm-12 btn btn-success">
  4. Drag and drop table Columns in Angular application
  5. </div>
  6. </div>
  7. <div class="container">
  8. <div class="row">
  9. <div class="col-md-12">
  10. <table class="table" id="table">
  11. <thead class="thead-dark">
  12. <tr>
  13. <th>Id</th>
  14. <th>Name</th>
  15. <th>Age</th>
  16. <th>Address</th>
  17. <th>City</th>
  18. <th>Salary</th>
  19. <th>Department</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr *ngFor="let data of rowData">
  24. <td>{{data.Id}}</td>
  25. <td>{{data.Name}}</td>
  26. <td>{{data.Age}}</td>
  27. <td>{{data.Address}}</td>
  28. <td>{{data.City}}</td>
  29. <td>{{data.Salary}}</td>
  30. <td>{{data.Department}}</td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. </div>
  35. </div>
  36. </div>
  37. <br/>
Open dragtable.component.ts file and add the following lines
  1. import { Component, OnInit } from '@angular/core';
  2. import tableDragger from 'table-dragger'
  3. @Component({
  4. selector: 'app-dragtable',
  5. templateUrl: './dragtable.component.html',
  6. styleUrls: ['./dragtable.component.css']
  7. })
  8. export class DragtableComponent implements OnInit {
  9. constructor() { }
  10. public rowData= [
  11. {
  12. Id: "1",
  13. Name: "Sanwar",
  14. Age: "25",
  15. Address: "Jaipur",
  16. City: "Jaipur",
  17. Salary: "500000",
  18. Department: "IT",
  19. },
  20. {
  21. Id: "2",
  22. Name: "Nisha",
  23. Age: "25",
  24. Address: "C-Scheme",
  25. City: "Jaipur",
  26. Salary: "500000",
  27. Department: "IT",
  28. },
  29. {
  30. Id: "3",
  31. Name: "David",
  32. Age: "25",
  33. Address: "C-Scheme",
  34. City: "Jaipur",
  35. Salary: "500000",
  36. Department: "IT",
  37. },
  38. {
  39. Id: "4",
  40. Name: "Sam",
  41. Age: "25",
  42. Address: "C-Scheme",
  43. City: "Jaipur",
  44. Salary: "500000",
  45. Department: "IT",
  46. },
  47. {
  48. Id: "5",
  49. Name: "Jyotsna",
  50. Age: "25",
  51. Address: "C-Scheme",
  52. City: "Mumbai",
  53. Salary: "500000",
  54. Department: "IT",
  55. },
  56. {
  57. Id: "6",
  58. Name: "Sid",
  59. Age: "25",
  60. Address: "C-Scheme",
  61. City: "Bangalore",
  62. Salary: "500000",
  63. Department: "IT",
  64. }
  65. ]
  66. ngOnInit(): void {
  67. var id = document.getElementById('table');
  68. console.log(id)
  69. var dragger = tableDragger(id, {
  70. mode: 'column',
  71. onlyBody: true,
  72. animation: 300
  73. });
  74. dragger.on('drop',function(from, to){
  75. });
  76. }
  77. }
Now open app.component.html file and add the following code:
  1. <app-dragtable></app-dragtable>
Now open app.module.ts file and add the following code.
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { HttpClientModule } from "@angular/common/http";
  4. import { AppComponent } from './app.component';
  5. import { DragtableComponent } from './dragtable/dragtable.component';
  6. @NgModule({
  7. declarations: [
  8. AppComponent,
  9. DragtableComponent
  10. ],
  11. imports: [
  12. BrowserModule,
  13. HttpClientModule
  14. ],
  15. providers: [],
  16. bootstrap: [AppComponent]
  17. })
  18. export class AppModule { }
Now, run the application using the 'npm start' command and check the result.
Now click on header,drag and column and drop on another column.Its reorder columns.
We also drag and drop rows of the table. We need to change mode column to row.

Summary

In this article, we learned how to create drag and drop table columns Angular 10 application.