Context Menu

10 Oct 201723 minutes to read

Scheduler provides default context menu options for both appointments as well as work cells. It also allows to define additional custom context menu options.

The options that are available under contextMenuSettings are as follows,

  • enable - Enables/disables the context menu option in Scheduler.
  • menuItems - Contains the sub-menu collections related to both the appointment and cells.

Default Menu Options

The menu items contains two separate collection based on the appointment and cells.

Appointment

The appointment collection includes the following options.

Open Appointment (default)
Delete Appointment (default)
Print Appointment
Categorize

Cells

The default options available within the cell collection includes -

New Appointment
New Recurring Appointment
Today
Go to date
Settings (View, TimeMode, Work Hours)

The following code snippet shows how to enable the context menu settings in Scheduler and to make use of it with default available options.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-context-menu-settings.bind="ContextMenu" e-appointment-settings.bind="AppointmentList"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        constructor() {
            this.ContextMenu = {
                enable: true,
                menuItems: {
                    appointment: [
                        { id: "open", text: "Open Appointment" },
                        { id: "delete", text: "Delete Appointment" }
                    ],
                    cells: [
                        { id: "new", text: "New Appointment" },
                        { id: "recurrence", text: "New Recurring Appointment" },
                        { id: "today", text: "Today" },
                        { id: "goToDate", text: "Go to date" },
                        { id: "settings", text: "Settings" },
                        { id: "view", text: "View", parentId: "settings" },
                        { id: "timeMode", text: "TimeMode", parentId: "settings" },
                        { id: "view_Day", text: "Day", parentId: "view" },
                        { id: "view_Week", text: "Week", parentId: "view" },
                        { id: "view_Workweek", text: "Workweek", parentId: "view" },
                        { id: "view_Month", text: "Month", parentId: "view" },
                        { id: "timeMode_Hour12", text: "12 Hours", parentId: "timeMode" },
                        { id: "timeMode_Hour24", text: "24 Hours", parentId: "timeMode" },
                        { id: "workhours", text: "Work Hours", parentId: "settings" }
                    ]
                }
            };
            this.AppointmentList = {
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30)
                }]
            };
        }
    }

    NOTE

    In agenda view, only the appointment menu items shows up in the context menu options. For default menu items, the id must be defined the same as mentioned in the above code example – as we have processed the menus based on this id within our source.

    Custom Menu Options

    Apart from the default available options, it is also possible to add custom menu options to the context-menu of both the appointment and cell collection.

    The following code example depicts how to add the custom menu items to the appointment and cell collection of the context menu settings.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-context-menu-settings.bind="ContextMenu" e-appointment-settings.bind="AppointmentList"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        constructor() {
            this.ContextMenu = {
                enable: true,
                menuItems: {
                    appointment: [
                        { id: "open", text: "Open Appointment" },
                        { id: "delete", text: "Delete Appointment" },
                        { id: "option1", text: "User Option 1" }
                    ],
                    cells: [
                        { id: "celloption1", text: "Custom Option 1" }
                    ]
                }
            };
            this.AppointmentList = {
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30)
                }]
            };
        }
    }

    NOTE

    The id given for the custom menu items must be unique in both the appointment and cell collection.

    Handling Menu Actions

    To define specific actions for a click made on the custom menu items, the client-side event menuItemClick can be used as depicted in the below code example.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-context-menu-settings.bind="ContextMenu" e-appointment-settings.bind="AppointmentList" e-on-menu-item-click.trigger="onMenuItemClick($event)"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        onMenuItemClick(event) {
            let args = event.detail;
            //args.events contains information of the clicked menu item.
            if (args.events.ID == "option1")
                alert("Custom menu clicked");
        }
        constructor() {
            this.ContextMenu = {
                enable: true,
                menuItems: {
                    appointment: [{
                        id: "open",
                        text: "Open Appointment"
                    }, {
                        id: "delete",
                        text: "Delete Appointment"
                    }, {
                        id: "option1",
                        text: "User Option 1"
                    }]
                }
            };
            this.AppointmentList = {
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30)
                }]
            };
        }
    }

    Also, it is possible to predict the target on which the right click is made, either on the cells or appointments with the use of the event beforeContextMenuOpen. The below code example shows how to block the display of context menu, when right clicked on the cells by setting args.cancel as true.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-context-menu-settings.bind="ContextMenu" e-appointment-settings.bind="AppointmentList" e-on-before-context-menu-open.trigger="onBeforeContextMenuOpen($event)"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        onBeforeContextMenuOpen(event) {
            let args = event.detail;
            //args.events.target – target information to depict either cell/appointment
            if ($(args.events.target).hasClass("e-workcells") || $(args.events.target).hasClass("e-monthcells"))
                args.cancel = true;
        }
        constructor() {
            this.ContextMenu = {
                enable: true,
                menuItems: {
                    appointment: [{
                        id: "open",
                        text: "Open Appointment"
                    }, {
                        id: "delete",
                        text: "Delete Appointment"
                    }, {
                        id: "option1",
                        text: "User Option 1"
                    }]
                }
            };
            this.AppointmentList = {
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30)
                }]
            };
        }
    }

    Adding Categorize Option

    To include the default categorize option within the context menu, it is necessary to enable the categorizeSettings property as shown in the below code example.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-context-menu-settings.bind="ContextMenu" e-appointment-settings.bind="AppointmentList"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        constructor() {
            this.ContextMenu = {
                enable: true,
                menuItems: {
                    appointment: [{
                        id: "open",
                        text: "Open Appointment"
                    }, {
                        id: "delete",
                        text: "Delete Appointment"
                    }, {
                        id: "categorize",
                        text: "Categorize"
                    }],
                }
            };
            this.AppointmentList = {
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30)
                }]
            };
        }
    }

    NOTE

    The categorize option must be added only to the appointment collection, which displays on right clicking the appointments.

    Remote Data Binding for Categorize

    In Schedule, we can also binding the categorize datasource using remote data. The below code example shows how to bind the datasource to categorizeSettings.

  • HTML
  • <template>
        <div>
            <ej-schedule id="Schedule1" e-current-date="11/07/2015" e-categorize-settings.bind="CategorizeData" e-appointment-settings.bind="AppointmentList"></ej-schedule>
        </div>
    </template>
  • JAVASCRIPT
  • export class Schedule {
        constructor() {
            let dataManager = ej.DataManager({
                url: "Home/GetCategorizeData",
                adaptor: new ej.UrlAdaptor()
            });
            this.CategorizeData = {
                enable: true,
                allowMultiple: true,
                dataSource: dataManager,
                id: "Id",
                fontColor: "FontColor",
                color: "Color",
                text: "Text"
            };
            this.AppointmentList = {
                categorize: "Categorize",
                dataSource: [{
                    Id: 100,
                    Subject: "Research on Sky Miracles",
                    StartTime: new Date(2015, 11, 2, 9, 00),
                    EndTime: new Date(2015, 11, 2, 10, 30),
                    Categorize: "1"
                }]
            };
        }
    }

    The server-side controller code to handle the CRUD operations are as follows.

  • C#
  • public JsonResult GetCategorizeData()
    {
        // ScheduleDataDataContext is a LINQ-to-SQL data class name that is defined in the .dbml file to access the tables from the database
        IEnumerable data = new ScheduleDataDataContext().CategoryData;
        return Json(data, JsonRequestBehavior.AllowGet);
    }