Drag-and-Drop in Webix UI

Not so long ago, the drag-and-drop functionality was used in web apps quite rarely. With HTML5 coming on the scene, native drag-and-drop has become easily accessible and popular. The functionality that required lots of coding efforts before, now can be added with a few lines of HTML code. While developing the Webix library, we tried to keep all features and advantages of native drag-and-drop. At the same time, we’ve added new capabilities that might be useful in web app development.

Component Level Drag-and-Drop

While HTML5 drag-and-drop is focused on an HTML element, Webix drag-and-drop concentrates on the UI components. For example, try to drag items between the tree and the grid in the demo below:

All Webix UI data-components support drag-and-drop functionality. With a single line of code, you can enable drag-and-drop for such components as List, DataTable, Tree, TreeTable, DataView.

webix.ui({
view:"list",
drag:true});

If you have two components with enabled drag-and-drop on the page, it’s already enough to be able to drag elements between these components. There are no limitations for dragging rows from Grid into Tree or from TreeTable into List.

Such unrestricted drag-and-drop is hardly useful. That’s why Webix allows specifying roles of the components using the same drag property.

webix.ui({
view:"tree",
drag:"source"});

webix.ui({
view:"datatable",
drag:"target"});

Such initialization enables drag-and-drop only from Tree to DataTable (from the component marked as drag:”source” to the one marked as drag:”target” ).
The other drag-and-drop use case is reordering of items (rows, tree nodes, list items, etc). Webix provides a ready-to-use model for reordering, as well:

webix.ui({
view:"list",
drag:"order"});

The component with enabled reordering mode allows drag-and-drop only within itself. For example, in case of grid, the user can reorder rows using drag-and-drop but can’t drag the rows outside the grid.

HTML Level Drag-and-Drop

Despite the fact that Webix is component oriented, Library supports drag-and-drop for custom HTML elements, as well. It provides an extra flexibility, as you utilize the Drag-and-Drop with your own HTML content or with some third party library.

Check the demo below and try to drag any grid’s row to the input:

To make any element on the page drop accessible, it’s enough to write something like:

The above code snippet will allow the end user to drop any element from a Webix component into the HTML input. In real life app, we need to define which property of the dragged object will be shown in the input, so we need a bit more of code. Something similar to the following:

Now try to drag any of the blocks into the datatable on the demo below. Row in the grid will be updated with content of the dragged box.

A

B

C

Native HTML5 Drag-and-Drop

While all the above samples are using custom drag-and-drop implementation, it’s possible to make a bridge between HTML5 and native drag-and-drop.
The following code is necessary to create a draggable HTML5 element:

attaching dragover handler to enable HTML5 dropping in the grid’s area

attaching drop handler to define how the data of dragged element need to be used in the grid

As a result, a custom HTML element can be dragged and dropped into the Webix datatable. In a similar way, we can combine any existing drag-and-drop solution, based on HTML5, and Drag-and-Drop functionality of Webix.

Conclusion

As you can see, there are a lot of different ways to use drag-and-drop in Webix components, and all of them are easy to implement. The above samples show the basics of drag-and-drop implementations. However, this functionality contains far more events and configuration options. If needed, any drag-and-drop aspect is easily redefined. You can learn more about this from documentation.

Due to the Webix’s flexibility, you can stick with Webix drag-and-drop implementation, use native HTML5 drag-and-drop, or combine them both for ultimate user experience.

Written by

Maksim Kozhukh

Pragmatic software developer with more than 12 years of web development experience. Prefers to work with Javascript and HTML. Maksim has worked with lots of different technologies from nearly forgotten ColdFustion to the bleeding edge of NodeJS and Go.