Drag Drop Events with Custom Conditions in Tree Grid AngularJS

Created: 09 April 2015

Drag&Drop operations in AngularJS Tree Grid component are accompanied with several events which can be handled in your application code. By adding event handlers in your code, you can set custom conditions from which a different operation can be executed. In code below some of these events are presented with conditions that alter the default drag drop operation.

TreeGrid directive is part of IntegralUI Studio for Weba suite of UI Components for development of web apps

In above demonstration the Tree Grid consists of a group of customers, each with several placed orders. By default, when drag drop operation is allowed, each row can be moved and placed in different position in tree hierarchy. It can be placed either above, below or as a child of a target row. Although in general this functionality is desired, in some cases based on application requirements, we may need to change this and create custom drag drop operation.

Note By dragging a specific order you may notice that mouse cursor changes, and a tooltip is shown whenever a drop position is allowed.

In our example, orders from different customers are prevented from moving. You can only reorder orders within the same customer, but you can't move a specific order from one customer to another. You can still change the order of customers, in whole with their orders. Additonally, an order cannot become a child of another order. This is all handled by simple conditions set in event handlers for drag drop events.

As it can be seen from code above, in dragOver event handler we are checking whether dragged row belongs to the same parent row as currently targeted row. If they belong to a different parent row, the drop operation is cancelled by returning a false value.

Furthermore, to prevent a row to become a child of another row, we are using the e.dropPos field to check whether the drop position is set to 0 (which means dragged row will be placed as child row or targeted row). If this condition is fulfilled, the drop operation is also cancelled.

A handler for dragDrop event prevents rows which represents orders (in our example), from dropping in Tree Grid space. By checking the value of drop position, if it is equal to -1, this means that there is no targeting row, and the dragged row is currently over Tree Grid space. If this is true, then the drop operation is cancelled by returning a false value. This condition applies only for orders, and not for customers. Customers can still drop over Tree Grid space.

As a result, we have created a custom drag drop operation that allows reordering of customers and orders, without disrupting relations among our data. All orders will remain within the same customer.

Tab content switching

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.