Search results

Drag and drop in JavaScript (ES5) Kanban control

09 Jun 2021 / 8 minutes to read

All cards can be dragged and dropped across the columns or within the columns or swimlane row or kanban to an external source and vice versa.

The following drag and drop types are available in the Kanban board.

  • Internal drag and drop
  • Column drag and drop
  • Swimlane drag and drop
  • External drag and drop
  • Kanban to Kanban
  • Kanban to External source and vice versa.

Dropped card position varies based on the sortSettings property.

Internal drag and drop

Allows the user to drag and drop the cards within the kanban board. Based on this, we can categorize into two ways.

  • Column drag and drop
  • Swimlane drag and drop

Column drag and drop

By default, all cards can be dragged and dropped across the columns and within the columns. You cannot drag and drop the cards when disabling the allowDragAndDrop property.

You can prevent the drag or drop behavior of the particular column by disabling the allowDrag or allowDrop property. You can also control the flow of transition cards between the columns by using the transitionColumns property.

In the following example, disable the drag and drop behavior on the Kanban board.

Source
Preview
index.js
index.html
Copied to clipboard
var kanbanObj = new ej.kanban.Kanban({
    dataSource: kanbanData,
    keyField: 'Status',
    columns: [
        { headerText: 'Backlog', keyField: 'Open' },
        { headerText: 'In Progress', keyField: 'InProgress' },
        { headerText: 'Testing', keyField: 'Testing' },
        { headerText: 'Done', keyField: 'Close' }
    ],
    cardSettings: {
        contentField: 'Summary',
        headerField: 'Id'
    },
    allowDragAndDrop: false
});
kanbanObj.appendTo('#Kanban');
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
    <title>Across Swimlane Drag and Drop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Swimlane drag and drop">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-layouts/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-kanban/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="content-wrapper">
            <div id="Kanban"></div>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Swimlane drag and drop

By default, Swimlane allows drag and drop across the columns within the swimlane row. Kanban does not allow dragging the cards across the swimlane rows.

Enabling the dragAndDrop property allows you to drag the cards across the swimlane rows, which is specified inside the swimlaneSettings property.

Source
Preview
index.js
index.html
Copied to clipboard
var kanbanObj = new ej.kanban.Kanban({
    dataSource: kanbanData,
    keyField: 'Status',
    columns: [
        { headerText: 'Backlog', keyField: 'Open' },
        { headerText: 'In Progress', keyField: 'InProgress' },
        { headerText: 'Testing', keyField: 'Testing' },
        { headerText: 'Done', keyField: 'Close' }
    ],
    cardSettings: {
        contentField: 'Summary',
        headerField: 'Id'
    },
    swimlaneSettings: {
        keyField: 'Assignee',
        allowDragAndDrop: true
    }
});
kanbanObj.appendTo('#Kanban');
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
    <title>Across Swimlane Drag and Drop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Swimlane drag and drop">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-layouts/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-kanban/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="content-wrapper">
            <div id="Kanban"></div>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

External drag and drop

Allows the user to drag and drop the cards from one kanban to another kanban or Kanban to an external source and vice versa.

Kanban to kanban

Drag and drop the card from one kanban to another kanban and vice versa. This can be achieved by specifying the externalDropId property which is used to specify the id of the dropped kanban element and the dragStop event which is used to delete the card on dragged Kanban and add the card on dropped Kanban using the deleteCard and addCard public methods.

Before adding a card to dropped kanban, you can manually change the card data headerField when the same card data headerField is dropped to another Kanban.

In the following example, Drag the card from one Kanban and drop it into another kanban using the dragStop event. In this event, remove the card from the dragged Kanban by using the deleteCard public method and add the card to the dropped Kanban by using the addCard public method.

Source
Preview
index.js
index.html
Copied to clipboard
var kanbanOptionsA = {
    dataSource: kanbanAData,
    keyField: 'Status',
    columns: [
        { headerText: 'Backlog', keyField: 'Open' },
        { headerText: 'Done', keyField: 'Close' }
    ],
    externalDropId: ['#kanbanB'],
    cardSettings: {
        contentField: 'Summary',
        headerField: 'Id'
    },
    showEmptyColumn: true,
    dragStop: function (args) {
        if (ej.base.closest(args.event.target, '#kanbanB')) {
            kanbanObjA.deleteCard(args.data);
            args.data.forEach(function (card, i) {
                var index = kanbanObjB.kanbanData.findIndex(function (colData) {
                    return colData[kanbanObjB.cardSettings.headerField] === card[kanbanObjB.cardSettings.headerField];
                });
                if (index !== -1) {
                    card[kanbanObjB.cardSettings.headerField] = Math.max.apply(Math, kanbanObjB.kanbanData.map(function (obj) { return parseInt(obj[kanbanObjB.cardSettings.headerField], 10); })) + ++i;
                }
            });
            kanbanObjB.addCard(args.data, args.dropIndex);
            args.cancel = true;
        }
    }
};
var kanbanObjA = new ej.kanban.Kanban(kanbanOptionsA);
kanbanObjA.appendTo('#kanbanA');
var kanbanOptionsB = {
    dataSource: kanbanBData,
    keyField: 'Status',
    columns: [
        { headerText: 'Backlog', keyField: 'Open' },
        { headerText: 'Done', keyField: 'Close' }
    ],
    cardSettings: {
        contentField: 'Summary',
        headerField: 'Id'
    },
    externalDropId: ['#kanbanA'],
    showEmptyColumn: true,
    dragStop: function (args) {
        if (ej.base.closest(args.event.target, '#kanbanA')) {
            kanbanObjB.deleteCard(args.data);
            args.data.forEach(function (card, i) {
                var index = kanbanObjA.kanbanData.findIndex(function (colData) {
                    return colData[kanbanObjA.cardSettings.headerField] === card[kanbanObjA.cardSettings.headerField];
                });
                if (index !== -1) {
                    card[kanbanObjA.cardSettings.headerField] = Math.max.apply(Math, kanbanObjA.kanbanData.map(function (obj) { return parseInt(obj[kanbanObjA.cardSettings.headerField], 10); })) + ++i;
                }
            });
            kanbanObjA.addCard(args.data, args.dropIndex);
            args.cancel = true;
        }
    }
};
var kanbanObjB = new ej.kanban.Kanban(kanbanOptionsB);
kanbanObjB.appendTo('#kanbanB');
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
    <title>Kanban to Kanban Drag and Drop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Kanban to Kanban drag and drop">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-layouts/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/ej2-kanban/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-6">
                    <h4>Kanban A</h4>
                    <div id="kanbanA"></div>
                </div>
                <div class="col-sm-6">
                    <h4>Kanban B</h4>
                    <div id="kanbanB"></div>
                </div>
            </div>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Treeview to Kanban

Drag the card from the Kanban board and drop it to the Treeview component and vice versa.

In the following sample, remove the data from the Kanban board using the deleteCard public method and add to the Treeview component using the addNodes public method at Kanban dragStop event when dragging the card and dropping it to the Treeview component. Remove the data from Treeview using the removeNodes public method and add to Kanban board using the openDialog public method when dragging the list from the Treeview component and dropping it to the kanban board.

Source
Preview
index.js
index.html
Copied to clipboard
var kanbanOptions = {
        dataSource: kanbanData,
        keyField: 'Status',
        columns: [
            { headerText: 'Backlog', keyField: 'Open' },
            { headerText: 'Done', keyField: 'Close' }
        ],
        externalDropId: ['#TreeView'],
        cardSettings: {
            contentField: 'Summary',
            headerField: 'Id'
        },
        showEmptyColumn: true,
        dragStop: function (args) {
            if (ej.base.closest(args.event.target, '#TreeView')) {
                kanbanObj.deleteCard(args.data);
                treeObj.addNodes(args.data);
                args.cancel = true;
            }
        }
    };
    var kanbanObj = new ej.kanban.Kanban(kanbanOptions);
    kanbanObj.appendTo('#Kanban');
    var treeObj = new ej.navigations.TreeView({
        fields: { dataSource: treeViewData, id: 'Id', text: 'Status' },
        allowDragAndDrop: true,
        nodeDragStop: onTreeDragStop,
        nodeTemplate: '#treeTemplate'
    });
    treeObj.appendTo('#TreeView');
    function onTreeDragStop(args) {
        if (ej.base.closest(args.event.target, '#Kanban')) {
            var treeData = treeObj.fields.dataSource;
            var filteredData = treeData.filter(function (item) { return item.Id === parseInt(args.draggedNodeData.id, 10); });
            treeObj.removeNodes([args.draggedNodeData.id]);
            kanbanObj.openDialog('Add', filteredData[0]);
            args.cancel = true;
        }
    }
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
    <title>Kanban to TreeView Drag and Drop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Kanban to TreeView drag and drop">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
        <div id="container">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-6">
                    <h4>Kanban</h4>
                    <div id="Kanban"></div>
                </div>
                <div class="col-sm-6">
                    <h4>TreeView</h4>
                    <div id="TreeView" style="border: 1px solid #e0e0e0"></div>
                </div>
            </div>
        </div>
    </div>
    <script id="treeTemplate" type="text/x-template">
        <div id="treelist">
            <div id="treeviewlist">${Id} - ${Status}</div>
        </div>
    </script>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Schedule to Kanban

Drag the card from the Kanban board and drop it to the Schedule component and vice versa.

In the following sample, remove the data from the Kanban board using the deleteCard public method and add to the schedule component using the addNodes public method at Kanban dragStop event when dragging the card and dropping it to the Treeview component. Remove the data from Treeview using the removeNodes public method and add to Kanban board using the addCard public method when dragging the list from the Treeview component and dropping it to the kanban board.

Source
Preview
index.js
index.html
Copied to clipboard
var kanbanOptions = {
        dataSource: kanbanData,
        keyField: 'DepartmentName',
        columns: [
            { headerText: 'GENERAL', keyField: 'GENERAL' }
        ],
        externalDropId: ['#Schedule'],
        cardSettings: {
            contentField: 'Name',
            headerField: 'Id',
            selectionType: 'Multiple'
        },
        showEmptyColumn: true,
        dragStop: function (args) {
            if (ej.base.closest(args.event.target, '#Schedule')) {
                kanbanObj.deleteCard(args.data);
                scheduleObj.openEditor(args.data[0], 'Add', true);
                args.cancel = true;
            }
        }
    };
    var kanbanObj = new ej.kanban.Kanban(kanbanOptions);
    kanbanObj.appendTo('#Kanban');
    ej.schedule.Schedule.Inject(ej.schedule.TimelineViews, ej.schedule.TimelineMonth, ej.schedule.Resize, ej.schedule.DragAndDrop);
    var scheduleObj = new ej.schedule.Schedule({
        width: '100%',
        height: '650px',
        selectedDate: new Date(2018, 7, 1),
        currentView: 'TimelineDay',
        eventDragArea: "body",
        cssClass: 'schedule-drag-drop',
        workHours: {
            start: '08:00',
            end: '18:00'
        },
        views: [
            { option: 'TimelineDay' },
            { option: 'TimelineMonth' }
        ],
        group: {
            enableCompactView: false,
            resources: ['Departments', 'Consultants']
        },
        resources: [
            {
                field: 'DepartmentID', title: 'Department',
                name: 'Departments', allowMultiple: false,
                dataSource: [
                    { Text: 'GENERAL', Id: 1, Color: '#bbdc00' },
                    { Text: 'DENTAL', Id: 2, Color: '#9e5fff' }
                ],
                textField: 'Text', idField: 'Id', colorField: 'Color'
            },
            {
                field: 'ConsultantID', title: 'Consultant',
                name: 'Consultants', allowMultiple: false,
                dataSource: [
                    { Text: 'Alice', Id: 1, GroupId: 1, Color: '#bbdc00', Designation: 'Cardiologist' },
                    { Text: 'Nancy', Id: 2, GroupId: 2, Color: '#9e5fff', Designation: 'Orthodontist' },
                    { Text: 'Robert', Id: 3, GroupId: 1, Color: '#bbdc00', Designation: 'Optometrist' },
                    { Text: 'Robson', Id: 4, GroupId: 2, Color: '#9e5fff', Designation: 'Periodontist' },
                    { Text: 'Laura', Id: 5, GroupId: 1, Color: '#bbdc00', Designation: 'Orthopedic' },
                    { Text: 'Margaret', Id: 6, GroupId: 2, Color: '#9e5fff', Designation: 'Endodontist' }
                ],
                textField: 'Text', idField: 'Id', groupIDField: 'GroupId', colorField: 'Color'
            }
        ],
        eventSettings: {
            dataSource: scheduleData,
            fields: {
                subject: { title: 'Patient Name', name: 'Name' },
                startTime: { title: 'From', name: 'StartTime' },
                endTime: { title: 'To', name: 'EndTime' },
                description: { title: 'Reason', name: 'Description' }
            }
        },
        dragStop: scheduleDragStop
    });
    scheduleObj.appendTo('#Schedule');
    function scheduleDragStop(args) {
        if (ej.base.closest(args.event.target, '#Kanban')) {
            scheduleObj.deleteEvent(args.data.Id);
            ej.base.removeClass([scheduleObj.element.querySelector('.e-selected-cell')], 'e-selected-cell');
            kanbanObj.openDialog('Add', args.data);
            args.cancel = true;
        }
    }
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
    <title>Kanban to Schedule Drag and Drop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Kanban to Schedule drag and drop">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-6">
                    <h4>Kanban</h4>
                    <div id="Kanban"></div>
                </div>
                <div class="col-sm-6">
                    <h4>Schedule</h4>
                    <div id="Schedule"></div>
                </div>
            </div>
        </div>
    </div>
    <script id="resource-template" type="text/x-template">
        <div class="template-wrap">
            <div class="specialist-category"> 
                ${getConsultantImage(data)}
                <div class="specialist-name">${getConsultantName(data)}</div>
                <div class="specialist-designation">${getConsultantDesignation(data)}</div>
            </div>
        </div>
    </script>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>