How to Easily Select and Delete Multiple Items in the EJ 2 Scheduler Control | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

How to Easily Select and Delete Multiple Items in the EJ 2 Scheduler Control

Selecting multiple items and deleting them all is an easy job compared to deleting individual items separately. The former saves a lot of time and effort.

The Syncfusion Essential JS 2 Scheduler control has built-in support to select multiple items. This blog serves as a guide to implement this support easily in your application.

In this blog, I am going to demonstrate the implementation of multiple item selection using a mouse device and deleting them all in a single action.

Initializing Scheduler control

For the first step, we need to initialize the EJ 2 Scheduler control with some local data. You’ll find the data I use in the following code example.

ej.base.enableRipple(true);           
 var date = new Date();
 var curDate = date.getDate();
 var curMonth = date.getMonth();
 var curYear = date.getFullYear();
    var data = [{
                Id: 1,
                Subject: '1',
                StartTime: new Date(curYear, curMonth, curDate, 11, 30),
                EndTime: new Date(curYear, curMonth, curDate, 13, 0),
                ProjectId: 1,
                TaskId: 1

            }, {
                Id: 2,
                Subject: '2',
                StartTime: new Date(curYear, curMonth, curDate, 12, 0),
                EndTime: new Date(curYear, curMonth, curDate, 14, 0),
                ProjectId: 1,
                TaskId: 2
            },
            {
                Id: 3,
                Subject: '3',
                StartTime: new Date(curYear, curMonth, curDate, 13, 30),
                EndTime: new Date(curYear, curMonth, curDate, 15, 0),
                ProjectId: 1,
                TaskId: 1
            },
            {
                Id: 4,
                Subject: '4',
                StartTime: new Date(curYear, curMonth, curDate, 13, 30),
                EndTime: new Date(curYear, curMonth, curDate, 15, 0),
                ProjectId: 2,
                TaskId: 3
            },
            {
                Id: 5,
                Subject: '5',
                StartTime: new Date(curYear, curMonth, curDate, 12, 30),
                EndTime: new Date(curYear, curMonth, curDate, 17, 0),
                ProjectId: 2,
                TaskId: 4
            },
            {
                Id: 6,
                Subject: '6',
                StartTime: new Date(curYear, curMonth, curDate, 11, 30),
                EndTime: new Date(curYear, curMonth, curDate, 14, 0),
                ProjectId: 2,
                TaskId: 5
            }];

Now, initiate the control with the local data. Here, I have initialized a dialog to display the selected items and added a button to delete the selected items.

var scheduleObj = new ej.schedule.Schedule({
                width: '100%', height: '550px',
                selectedDate: new Date(curYear, curMonth, curDate),
                views: ['TimelineDay', 'TimelineWeek', 'TimelineWorkWeek', 'TimelineMonth', 'Agenda'],
                currentView: 'TimelineWeek',
                group: {
                    resources: ['Projects', 'Categories']
                },
                resources: [
                    {
                        field: 'ProjectId', title: 'Choose Project', name: 'Projects',
                        dataSource: [
                            { text: 'PROJECT 1', id: 1, color: '#cb6bb2' },
                            { text: 'PROJECT 2', id: 2, color: '#56ca85' }
                        ],
                        textField: 'text', idField: 'id', colorField: 'color'
                    }, {
                        field: 'TaskId', title: 'Category',
                        name: 'Categories', allowMultiple: true,
                        dataSource: [
                            { text: 'Nancy', id: 1, groupId: 1, color: '#df5286' },
                            { text: 'Steven', id: 2, groupId: 1, color: '#7fa900' },
                            { text: 'Robert', id: 3, groupId: 2, color: '#ea7a57' },
                            { text: 'Smith', id: 4, groupId: 2, color: '#5978ee' },
                            { text: 'Micheal', id: 5, groupId: 2, color: '#df5286' }
                        ],
                        textField: 'text', idField: 'id', groupIDField: 'groupId', colorField: 'color'
                    }
                ],
                eventSettings: {
                    dataSource: data
                }
            });
            scheduleObj.appendTo('#Schedule');
         var dialogObj = new ej.popups.Dialog({
                header: 'Selected Items list',
                target: document.body,
                isModal: true,
                animationSettings: { effect: 'None' },
                showCloseIcon: true,
                width: '500px'
            });
            dialogObj.appendTo('#dialog');
            dialogObj.hide();

            var button = new ej.buttons.Button();
            button.appendTo('#deleteBtn');

The following is the HTML code example, for your reference.

<html>
<head>
    <script src="https://ej2.syncfusion.com/javascript/demos/schedule/timeline-resource-grouping/datasource.js" type="text/javascript"></script>
    <script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
    <link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<style>
            .e-schedule:not(.e-device) .e-agenda-view .e-content-wrap table td:first-child {
                width: 90px;
            }
            .e-schedule .e-agenda-view .e-resource-column {
                width: 100px;
            }
           .multiSelectHelper {
                position: absolute;
                z-index: 100000;
                background-color: transparent;
                border: 1px solid transparent;
            }

                .multiSelectHelper.inAction {
                    background-color: lightgray;
                    border: 1px dashed black;
                    opacity: 0.5;
                }

                .multiSelectHelper.hidden {
                    display: none;
                }
        </style>
</head>
<body>
    <div class="stackblitz-container material">
        <div class="col-lg-12 control-section">
            <div class="content-wrapper">
                <div id="Schedule"> </div>
                <div id="dialog">
                    <div id="content"></div>
                    <button id="deleteBtn">Delete All</button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

For more details about getting started with the EJ 2 Scheduler control, check out our documentation.

Implementing mouse selection

Now we need to implement the mouse selection by using document mouse events such as mouse up, down, and move. I have added multiSelectHelper in the HTML from the previous segment to implement this.

var clicked = false, clickedPoint, movedPoint, finalCollection = [];

            document.onmousedown = function (evt) {
                if (evt.target.classList.contains("e-work-cells") && evt.target.closest("#Schedule")) {
                    clicked = true;
                    clickedPoint = { x: evt.clientX, y: evt.clientY };
                    addMultiSelectHelper();
                }
            };

            document.onmouseup = function (evt) {
                if (clicked) {
                    clicked = false;
                    var helper = document.getElementById("multiSelectHelper");
                    helper.classList.add("hidden");

                    finalCollection = [];
                    var left = parseFloat(helper.style.left);
                    var top = parseFloat(helper.style.top);
                    var right = parseFloat(helper.style.left) + parseFloat(helper.style.width);
                    var bottom = parseFloat(helper.style.top) + parseFloat(helper.style.height);
                    var startCell = scheduleObj.getCellDetails(document.elementFromPoint(left, top));
                    if (startCell)
                        var startRes = scheduleObj.resourceBase.lastResourceLevel[startCell.groupIndex];
                    var endCell = scheduleObj.getCellDetails(document.elementFromPoint(right, bottom));
                    if (endCell)
                        var endRes = scheduleObj.resourceBase.lastResourceLevel[endCell.groupIndex];
                    if (startRes && endRes) {
                        var rangeCollection = scheduleObj.getEvents(startCell.startTime, endCell.endTime);
                        var resCollection = scheduleObj.resourceBase.lastResourceLevel.slice(startRes.groupIndex, (endRes.groupIndex + 1));
                        var content = document.getElementById("content"), child;
                        content.innerHTML = "";
                        for (var i = 0; i < resCollection.length; i++) {
                            var eventCollection = scheduleObj.eventBase.filterEventsByResource(scheduleObj.resourceBase.lastResourceLevel[resCollection[i].groupIndex], rangeCollection);
                            if (eventCollection.length > 0) {
                                for (var index = 0; index < eventCollection.length; index++) {
                                    finalCollection.push(eventCollection[index]);
                                    child = document.createElement("p");
                                    child.innerHTML = eventCollection[index].Subject;
                                    content.appendChild(child);
                                }
                            }
                        }
                        var footer = document.createElement("p");
                        footer.innerHTML = finalCollection.length + " item(s) selected";
                        content.appendChild(footer);
                        if (selectStarted)
                            dialogObj.show();
                    }
                }
                selectStarted = false;
            };

            document.onmousemove = function (evt) {
                if (clicked) {
                    selectStarted = true;
                    movedPoint = { x: evt.clientX, y: evt.clientY };
                    updateMultiSelectHelper();
                }
            };
            //Selection helper
            addMultiSelectHelper = function () {
                var multiHelper = document.getElementById("multiSelectHelper");
                if (!multiHelper) {
                    multiHelper = document.createElement("div");
                    multiHelper.id = "multiSelectHelper";
                    multiHelper.classList.add("multiSelectHelper");
                    document.body.appendChild(multiHelper);
                }
                multiHelper.classList.add("inAction");
                return multiHelper;
            };
            updateMultiSelectHelper = function () {
                var multiHelper = document.getElementById("multiSelectHelper");
                if (multiHelper) {
                    var bounds = getRectBoundsFromPoints(clickedPoint, movedPoint);
                    multiHelper.style.left = bounds.x;
                    multiHelper.style.top = bounds.y;
                    multiHelper.style.width = bounds.width;
                    multiHelper.style.height = bounds.height;
                    multiHelper.classList.remove("hidden");
                }
            };
            getRectBoundsFromPoints = function (startPoint, endPoint) {
                var bounds = { "x": 0, "y": 0, "width": 0, "height": 0 };
                bounds.width = Math.abs(startPoint.x - endPoint.x);
                bounds.height = Math.abs(startPoint.y - endPoint.y);
                if (startPoint.x <= endPoint.x && startPoint.y <= endPoint.y) {
                    bounds.x = startPoint.x;
                    bounds.y = startPoint.y;
                }
                else if (startPoint.x <= endPoint.x && startPoint.y >= endPoint.y) {
                    bounds.x = startPoint.x;
                    bounds.y = startPoint.y - bounds.height;
                }
                else if (startPoint.x >= endPoint.x && startPoint.y <= endPoint.y) {
                    bounds.x = startPoint.x - bounds.width;
                    bounds.y = startPoint.y;
                }
                else if (startPoint.x >= endPoint.x && startPoint.y >= endPoint.y) {
                    bounds.x = startPoint.x - bounds.width;
                    bounds.y = startPoint.y - bounds.height;
                }
                return bounds;
            };

In the previous listing, I add a multiselect helper (div element) on the mouse down action and store the clicked x and y positions. On mouse move, I update the helper’s position and size with respect to the mouse pointer.

Finally, when the user releases the mouse button, I hide the helper and pass the range to the Scheduler API to select all the items within that range. We need to find the start and end cells and their indexes to select them. The output of multiselection using the mouse is shown in the following screenshot.MultiSelection made using mouse in Essential JS 2 Scheduler

Display and delete selected items

Once we have the selected items, we’ll display all of them in a dialog with a Delete All button. On mouse up, append the list of items into the dialog content. Then, if the user clicks the Delete All button, remove the selected items from the data and reassign the updated data to the Scheduler control.

The following code example illustrates this.

button.element.onclick = function () {
                var data = scheduleObj.eventSettings.dataSource, updatedCollection = [], elementFound;
                for (var i = 0; i < data.length; i++) {
                    elementFound = false;
                    for (var j = 0; j < finalCollection.length; j++) {
                        if (finalCollection[j].Id == data[i].Id) {
                            elementFound = true;
                            break;
                        }                            
                    }
                    if (!elementFound)
                        updatedCollection.push(data[i]);
                }
                scheduleObj.eventSettings.dataSource = updatedCollection;
                scheduleObj.dataBind();
                dialogObj.hide();
            }

Here is the output of the dialog with the selected items.Listing the multiple items selected

It is pretty easy to achieve multiple selection in our EJ 2 Scheduler control using a mouse. From here, we can apply styles and update properties for multiple items.

Conclusion

I hope you now have a clear idea about implementing multiselection in the Essential JS 2 Scheduler control. The HTML file created in the blog is available in this  location for your reference. You can always download our free evaluation to see all our controls in action.

If you have any questions or require clarification about these features, please let us know in the comments below. You can also contact us through our support forumDirect-Trac, or feedback portal. We are happy to assist you!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed