Context menu in EJ2 JavaScript Schedule control

27 Apr 202311 minutes to read

You can display context menu on work cells and appointments of Scheduler by making use of the ContextMenu control manually from the application end. In the following code example, context menu control is being added from sample end and set its target as Scheduler.

On Scheduler cells, you can display the menu items such as New Event, New Recurring Event and Today option. For appointments, you can display its related options such as Edit Event and Delete Event. The default event window can be opened for appointment creation and editing using the openEditor method of Scheduler.

The deletion of appointments can be done by using the deleteEvent public method. Also, the selectedDate property can be used to navigate between different dates.

You can also display custom menu options on Scheduler cells and appointments. Context menu will open on tap-hold in responsive mode.

var scheduleObj = new ej.schedule.Schedule({
    width: '100%',
    height: '550px',
    selectedDate: new Date(2018, 1, 15),
    eventSettings: {
    dataSource: scheduleData
    },
    allowDragAndDrop: false,
    allowResizing: false
});
scheduleObj.appendTo('#Schedule');

var menuObj, selectedTarget;

var menuItems = [{
    text: 'New Event',
    iconCss: 'e-icons new',
    id: 'Add'
    }, {
    text: 'New Recurring Event',
    iconCss: 'e-icons recurrence',
    id: 'AddRecurrence'
    }, {
    text: 'Today',
    iconCss: 'e-icons today',
    id: 'Today'
    }, {
    text: 'Edit Event',
    iconCss: 'e-icons edit',
    id: 'Save'
    }, {
    text: 'Edit Event',
    id: 'EditRecurrenceEvent',
    iconCss: 'e-icons edit',
    items: [{
    text: 'Edit Occurrence',
    id: 'EditOccurrence'
    }, {
    text: 'Edit Series',
    id: 'EditSeries'
    }]
    }, {
    text: 'Delete Event',
    iconCss: 'e-icons delete',
    id: 'Delete'
    }, {
    text: 'Delete Event',
    id: 'DeleteRecurrenceEvent',
    iconCss: 'e-icons delete',
    items: [{
    text: 'Delete Occurrence',
    id: 'DeleteOccurrence'
    }, {
    text: 'Delete Series',
    id: 'DeleteSeries'
    }]
}];
menuObj = new ej.navigations.ContextMenu({
    target: '.e-schedule',
    items: menuItems,
    beforeOpen: onContextMenuBeforeOpen,
    select: onMenuItemSelect,
    cssClass: 'schedule-context-menu'
});
menuObj.appendTo('#ContextMenu');

function onContextMenuBeforeOpen(args) {
    var newEventElement = document.querySelector('.e-new-event');
    if (newEventElement) {
        ej.base.remove(newEventElement);
        ej.base.removeClass([document.querySelector('.e-selected-cell')], 'e-selected-cell');
    }
    scheduleObj.closeQuickInfoPopup();
    var targetElement = args.event.target;
    if (ej.base.closest(targetElement, '.e-contextmenu')) {
        return;
    }
    selectedTarget = ej.base.closest(targetElement, '.e-appointment,.e-work-cells,' +
    '.e-vertical-view .e-date-header-wrap .e-all-day-cells,.e-vertical-view .e-date-header-wrap .e-header-cells');
    if (ej.base.isNullOrUndefined(selectedTarget)) {
        args.cancel = true;
        return;
    }
    if (selectedTarget.classList.contains('e-appointment')) {
        var eventObj = scheduleObj.getEventDetails(selectedTarget);
        if (eventObj.RecurrenceRule) {
            menuObj.showItems(['EditRecurrenceEvent', 'DeleteRecurrenceEvent'], true);
            menuObj.hideItems(['Add', 'AddRecurrence', 'Today', 'Save', 'Delete'], true);
        } else {
            menuObj.showItems(['Save', 'Delete'], true);
            menuObj.hideItems(['Add', 'AddRecurrence', 'Today', 'EditRecurrenceEvent', 'DeleteRecurrenceEvent'], true);
        }
        return;
    }
    menuObj.hideItems(['Save', 'Delete', 'EditRecurrenceEvent', 'DeleteRecurrenceEvent'], true);
    menuObj.showItems(['Add', 'AddRecurrence', 'Today'], true);
}

function onMenuItemSelect(args) {
    var selectedMenuItem = args.item.id;
    var eventObj;
    if (selectedTarget.classList.contains('e-appointment')) {
        eventObj = scheduleObj.getEventDetails(selectedTarget);
    }
    switch (selectedMenuItem) {
        case 'Today':
            scheduleObj.selectedDate = new Date();
            break;
        case 'Add':
        case 'AddRecurrence':
            var selectedCells = scheduleObj.getSelectedElements();
            var activeCellsData = scheduleObj.getCellDetails(selectedCells.length > 0 ? selectedCells : selectedTarget);
            if (selectedMenuItem === 'Add') {
                scheduleObj.openEditor(activeCellsData, 'Add');
            } else {
                scheduleObj.openEditor(activeCellsData, 'Add', null, 1);
            }
            break;
        case 'Save':
        case 'EditOccurrence':
        case 'EditSeries':
            if (selectedMenuItem === 'EditSeries') {
                eventObj = new ej.data.DataManager(scheduleObj.eventsData).executeLocal(new ej.data.Query().where(scheduleObj.eventFields.id, 'equal', eventObj[scheduleObj.eventFields.recurrenceID]))[0];
            }
            scheduleObj.openEditor(eventObj, selectedMenuItem);
            break;
        case 'Delete':
            scheduleObj.deleteEvent(eventObj);
            break;
        case 'DeleteOccurrence':
        case 'DeleteSeries':
            scheduleObj.deleteEvent(eventObj, selectedMenuItem);
            break;
    }
}
<!DOCTYPE html><html lang="en"><head>
    <title>Schedule Typescript Component</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Schedule Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet"> 
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-schedule/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
    
    <div id="container">
        <div class="content-wrapper">
            <div id="Schedule">
            </div>
            <ul id="ContextMenu"></ul>
        </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>

You can refer to our JavaScript Scheduler feature tour page for its groundbreaking feature representations. You can also explore our JavaScript Scheduler example to knows how to present and manipulate data.