Drag Drop between Custom Angular Components

Created: 07 November 2017

Angular components by default don't have a drag drop functionality built-in. If you need to drag drop objects within the same component or different ones, you need to built it from scratch. IntegralUI DragDrop Service provides a way to create custom drag drop operations for different kinds of Angular components.

In this example, there are three angular components: two lists and a block component. You can drag an item from the first list to the block or to the second list and vice versa. During this process, the IntegralUI DragDrop Service works as mediator between components. You can store and retrieve the dragged object using methods provided by the service. In more advanced scenarios, you can

How to Create Custom Angular Component

In this example, you need two components: a list and a block. Both components will have an add and remove methods used during drag drop operations and called from root application component. The main difference here is that the list has uses an array to store the objects that are dropped, while the block uses a variable. The components are defined as:

In current state, these components don't have a drag drop functionality. They can only store objects and display them in their view.

How to Add Drag Drop functionality to Angular Components

In order to use the IntegralUI DragDrop Service, you need to add it as provider to the root application component. This makes sure that a single instance of this service is used across all child components. As a result, the data stored by the service in one component, is retrievable by another component.

To handle drag drop operations, you can use standard HTML5 events. Each component should have handlers for dragstart and dragover events. When you drag an object initially, the dragstart event is fired . In your event handler, you need to call the setData method of the DragDrop Service to update the stored object. Also, you can store a reference to the component from which the drag operations starts. This is used later, when you need to remove the dragged object from the source component and add it to the target component, in other words move an object from one component to another.

Here are the handlers for dragstart event for list and block component. They are very similar:

The handler for the dragover event is used to set the target component over which the dragged object is currently hovering. By setting the targetCtrl of the data stored in the DragDrop Service, you can later use it to add the object to the target component. This is all handled from the application root component.

The difference here between the list and block components is that the list also can have as a target an item and also the list itself, while the block act as a drop target in whole.

In addition, during hovering over the list, it is good to visually display the position at which the dragged object can drop. For this purpose, a simple change to item padding is used. To make the example simple, the object can drop only below the target item.

How to Handle Drop Operation from App Root Component

Until this point you can start drag and drop and handle the target over which object hovers. Now, you need to process the drop of the dragged objects. For this purpose, the best is to handle it from the application root component, and call the add/remove methods for the source and target components.

To handle drops over target component, you need to add a handler to the standard HTML5 drop event. You don't have to add an event handler to each child component, you can handle it from the root component. Any drop events in a child also bubble-up to the parent component.

During drag drop operation using the DragDrop Service, you have stored the object that is dragged but also the source component from which the object is dragged and the target component over which the object is dropped. You can use this references to call the add/remove methods and complete the move operation.

As it is shown in above code, the operation is completed only when item is removed at first from the source, and then add it to the target component.

Conclusion

Drag Drop functionality by default is not present in Angular components. If you need to add a drag drop to your components, you can use the IntegralUI DragDrop Service to handle the data. By setting this service as a provider on global level to the application root component, you can add drag drop functionality to any child component. Each component during drag drop, can act as a source or a target , which is determined by the data stored in the service.

Newsletter

Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.

Name:

Email: *

*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.