Search results

Drag and Drop

Drag and Drop is supported through two libraries. Those are Draggable and Droppable. Draggable makes DOM to be dragged using mouse or touch gestures and Droppable mark required DOM as droppable zone.

Initializing draggable

You can make any element draggable by passing the element to Draggable constructor. Refer the following code snippet to enable draggable for DOM element.

Source
Preview
index.ts
index.html
import {Draggable} from  '@syncfusion/ej2-base';

 let dragElement: HTMLElement = document.getElementById('element1');
 let draggable:Draggable = new Draggable(dragElement,{clone: false});
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Draggable</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='element1'><p>Draggable Element </p></div>
    </div>
</body>

</html>

Creating droppable zone

You can convert any DOM element as a droppable zone, which accepts the draggable elements. Refer the following code snippet to enable droppable zone.

Source
Preview
index.ts
index.html
import { Droppable} from  '@syncfusion/ej2-base';

 let droppable: Droppable = new Droppable(document.getElementById('droppable'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Draggable</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
            <div id ='droppable'>
                <p class='drop'>
                    <span>Drop Target</span> 
                </p>
            </div>    
    </div>
</body>

</html>

Defining drop action

To define drop action set drop callback function during droppable object creation. You can get details of dropped element through dropped element property in event argument. Refer the following code snippet to use basic drag and drop action.

Source
Preview
index.ts
index.html
import { Draggable, Droppable, DropEventArgs } from '@syncfusion/ej2-base';

let draggable: Draggable = new Draggable(document.getElementById('element1'), {
    clone: false
});
let droppable: Droppable = new Droppable(document.getElementById('droppable'), {
    drop: (e: DropEventArgs) => {
        e.droppedElement.querySelector('.drag-text').textContent = 'Dropped';
    }
});
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Draggable</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='droppable'>
            <p class='drop'><span>Drop Target </span></p></div>
    <div id='element1'><p class='drag-text'>Drag </p></div>

    </div>
</body>

</html>

See Also

Define handle element for Draggable
Restricting Draggable within container
Visual feedback of draggable element
Accepting specific drag element in droppable