Introduction
Sometimes you need to drag and drop data from one table to another table in angular. In this article, I will show how we can drag and drop data between two tables in angular. Here, I am using HTML 5 Drag and Drop APIs to perform drag and drop action in angular.
I have created two tables, one is “Driver Schedule” which has driver schedule information and the other is “Trailer Info” which has trailer related information. Here the requirement is that I need to drag “Trailer No” from Trailer Info table and drop it into “Trailer No” column in Driver Schedule. And similarly, once you assign the trailer (using drag and drop from Trailer Info table to Driver Schedule) then you need to un-assign the trailer by performing drag and drop from Driver Schedule to Trailer Info.
Background
To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.
There are a number of different events to attach to for monitoring the entire drag and drop process.
- dragstart
The dragstart event is fired when the user starts dragging an element or text selection. - drop
The drop event is fired when an element or text selection is dropped on a valid drop target. - dragleave
The dragleave event is fired when a dragged element or text selection leaves a valid drop target. - dragover
The dragover event is fired when an element or text selection is being dragged over a valid drop target . - dragend
The dragend event is fired when a drag operation is being ended (by releasing a mouse button or hitting the escape key).
Installing Angular CLI
If you haven’t installed Angular CLI, you can do that by running the below command.
npm install -g @angular/cli
Generating a new project
We can use the below command to create a new project.
ng new DragAndDropDemo
Now that we have created our application, let’s run it and see if it is working or not.
- ng serve --open (if you need to open the browser by the app)
- ng serve (if you want to manually open the browser).
- You can always use 'ng s' as well
The command will build your application and run it in the browser.
Create JSON Data Source for two tables
I have created two JSON files to bind the data in Driver Schedule and Trailer Info table.
schedule.json
Add JSON file with name “schedule.json” in the application. Add below JSON data code in this file.
{
"data": [{
"id": 1,
"scheduleId": "Sch001",
"driverName": "Anand",
"origin": "Mumbai",
"destination": "Delhi",
"trailerNumber": "N/A"
}, {
"id": 2,
"scheduleId": "Sch002",
"driverName": "Rustam",
"origin": "Mumbai",
"destination": "Pune",
"trailerNumber": "N/A"
}, {
"id": 3,
"scheduleId": "Sch003",
"driverName": "Ramesh",
"origin": "Delhi",
"destination": "Chennai",
"trailerNumber": "N/A"
}, {
"id": 4,
"scheduleId": "Sch004",
"driverName": "Anuj",
"origin": "Mumbai",
"destination": "Chennai",
"trailerNumber": "N/A"
}, {
"id": 5,
"scheduleId": "Sch005",
"driverName": "Kishor",
"origin": "Mumbai",
"destination": "Goa",
"trailerNumber": "N/A"
}, {
"id": 6,
"scheduleId": "Sch006",
"driverName": "Suresh",
"origin": "Mumbai",
"destination": "Nashik",
"trailerNumber": "N/A"
}, {
"id": 7,
"scheduleId": "Sch007",
"driverName": "Mahesh",
"origin": "Delhi",
"destination": "Luckmow",
"trailerNumber": "N/A"
}, {
"id": 8,
"scheduleId": "Sch008",
"driverName": "Chandan",
"origin": "Mumbai",
"destination": "Jaipur",
"trailerNumber": "N/A"
}, {
"id": 9,
"scheduleId": "Sch009",
"driverName": "Narendra",
"origin": "Mumbai",
"destination": "Banglore",
"trailerNumber": "N/A"
}, {
"id": 10,
"scheduleId": "Sch010",
"driverName": "Jitendra",
"origin": "Banglore",
"destination": "Manglore",
"trailerNumber": "N/A"
}]
}
trailer.json
Add JSON file with name “trailer.json” in the application. Add below JSON data code in this file.
{
"data": [{
"trailerNumber": "Trl001",
"origin": "Mumbai",
"destination": "Delhi"
}, {
"trailerNumber": "Trl002",
"origin": "Mumbai",
"destination": "Pune"
}, {
"trailerNumber": "Trl003",
"origin": "Delhi",
"destination": "Chennai"
}, {
"trailerNumber": "Trl004",
"origin": "Mumbai",
"destination": "Chennai"
}, {
"trailerNumber": "Trl005",
"origin": "Mumbai",
"destination": "Goa"
}, {
"trailerNumber": "Trl006",
"origin": "Mumbai",
"destination": "Nashik"
}, {
"trailerNumber": "Trl007",
"origin": "Delhi",
"destination": "Luckmow"
}]
}

Join the conversation! Your thoughts help the community grow.