Open a dialog on contextmenu item click in Vue Context menu component

25 Apr 202510 minutes to read

This section explains about how to open a dialog on ContextMenu item click. This can be achieved by handling dialog open in select event of the ContextMenu.

Install Syncfusion® Popups packages using below command.

npm install @syncfusion/ej2-vue-popups --save

In the following sample, Dialog will open while clicking Save As... item.

<template>
    <div>
        <div id="target">Right click / Touch hold to open the ContextMenu</div>
        <ejs-dialog id="dialog" content="This file can be saved as PDF" :buttons='buttons' width='200px' height='110px'
            :visible='false'></ejs-dialog>
        <ejs-contextmenu target='#target' :items='menuItems' :select='onSelect'></ejs-contextmenu>
    </div>
</template>

<script setup>

import { ContextMenuComponent as EjsContextmenu } from "@syncfusion/ej2-vue-navigations";
import { DialogComponent as EjsDialog } from "@syncfusion/ej2-vue-popups";
import { enableRipple } from '@syncfusion/ej2-base';

enableRipple(true);

const menuItems = [
    {
        text: 'Back'
    },
    {
        text: 'Forward'
    },
    {
        text: 'Reload'
    },
    {
        separator: true
    },
    {
        text: 'Save As...'
    },
    {
        text: 'Print'
    },
    {
        text: 'Cast'
    }];

const buttons = [{
    buttonModel: {
        isPrimary: true,
        content: 'Submit',
        cssClass: 'e-flat',
    },
    click: function () {
        this.hide();
    }
}];

const onSelect = (args) => {
    if (args.item.text === 'Save As...') {
        if (document.getElementById('dialog_dialog-content')) {
            document.getElementById("dialog").ej2_instances[0].show();
        }
        else {
            document.getElementById("dialog").ej2_instances[0].appendTo('#dialog');
        }
    }
};

</script>

<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

.list {
    display: none;
}

#target {
    border: 1px dashed;
    height: 150px;
    padding: 10px;
    position: relative;
    text-align: justify;
    color: gray;
    user-select: none;
}
</style>
<template>
    <div>
        <div id="target">Right click / Touch hold to open the ContextMenu</div>
        <ejs-dialog id="dialog" content="This file can be saved as PDF" :buttons='buttons' width='200px' height='110px'
            :visible='false'></ejs-dialog>
        <ejs-contextmenu target='#target' :items='menuItems' :select='onSelect'></ejs-contextmenu>
    </div>
</template>

<script>

import { ContextMenuComponent } from "@syncfusion/ej2-vue-navigations";
import { DialogComponent } from "@syncfusion/ej2-vue-popups";
import { enableRipple } from '@syncfusion/ej2-base';

enableRipple(true);

export default {
    name: "App",
    components: {
        "ejs-dialog": DialogComponent,
        "ejs-contextmenu": ContextMenuComponent
    },
    data() {
        return {
            menuItems: [
                {
                    text: 'Back'
                },
                {
                    text: 'Forward'
                },
                {
                    text: 'Reload'
                },
                {
                    separator: true
                },
                {
                    text: 'Save As...'
                },
                {
                    text: 'Print'
                },
                {
                    text: 'Cast'
                }],
            buttons: [{
                buttonModel: {
                    isPrimary: true,
                    content: 'Submit',
                    cssClass: 'e-flat',
                },
                click: function () {
                    this.hide();
                }
            }],
        };
    },
    methods: {
        onSelect: function (args) {
            if (args.item.text === 'Save As...') {
                if (document.getElementById('dialog_dialog-content')) {
                    document.getElementById("dialog").ej2_instances[0].show();
                }
                else {
                    document.getElementById("dialog").ej2_instances[0].appendTo('#dialog');
                }
            }
        }
    }
}
</script>

<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

.list {
    display: none;
}

#target {
    border: 1px dashed;
    height: 150px;
    padding: 10px;
    position: relative;
    text-align: justify;
    color: gray;
    user-select: none;
}
</style>