Search results

Context menu

The TreeGrid has options to show the context menu when right clicked on it. To enable this feature, you need to define either default or custom item in the contextMenuItems property.

The default items are in the following table.

Items Description
AutoFit Auto fit the current column.
AutoFitAll Auto fit all columns.
Edit Edit the current record.
Delete Delete the current record.
Save Save the edited record.
Cancel Cancel the edited state.
PdfExport Export the treegrid data as Pdf document.
ExcelExport Export the treegrid data as Excel document.
CsvExport Export the treegrid data as CSV document.
SortAscending Sort the current column in ascending order.
SortDescending Sort the current column in descending order.
FirstPage Go to the first page.
PrevPage Go to the previous page.
LastPage Go to the last page.
NextPage Go to the next page.
AddRow Add new row to the treegrid.
tagHelper
default-contextmenu.cs
@{
    var contextItems = new string[]{
        "SortAscending", "SortDescending","Edit", "Delete", "Save", "Cancel",
        "PdfExport", "ExcelExport", "CsvExport", "FirstPage", "PrevPage",
        "LastPage", "NextPage" };
}

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" contextMenuItems="contextItems" allowExcelExport="true" allowPdfExport="true" allowSorting="true" allowPaging="true" allowResizing="true" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-pagesettings pageSize="7"></e-treegrid-pagesettings>
    <e-treegrid-editsettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Row"></e-treegrid-editsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180" textAlign="Left"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" editType="datepickeredit" textAlign="Right" format="yMd" type="date" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" editType="numericedit" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Custom context menu items

The custom context menu items can be added by defining the contextMenuItems as a collection of contextMenuItemModel. Actions for this customized items can be defined in the contextMenuClick event.

In the below sample, we have shown context menu item for parent rows to expand or collapse child rows.

tagHelper
custom-contextmenu.cs
@{
    List<object> contextItems = new List<Object>();
    contextItems.Add(new { text = "Collapse the Row", target = ".e-content", id = "collapserow" });
    contextItems.Add(new { text = "Expand the Row", target = ".e-content", id = "expandrow" });
}

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" contextMenuOpen="onOpen" contextMenuClick="onClick"
              contextMenuItems="contextItems" allowExcelExport="true" allowPdfExport="true" allowSorting="true" allowPaging="true" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-pagesettings pageSize="7"></e-treegrid-pagesettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180" textAlign="Left"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function onOpen(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        var elem = args.event.target;
        var uid = elem.closest('.e-row').getAttribute('data-uid');
        if (ejs.base.isNullOrUndefined(ejs.base.getValue('hasChildRecords', treegrid.grid.getRowObjectFromUID(uid).data))) {
            args.cancel = true;
        } else {
            var flag = ejs.base.getValue('expanded', treegrid.grid.getRowObjectFromUID(uid).data);
            var val = flag ? 'none' : 'block';
            document.querySelectorAll('li#expandrow')[0].setAttribute('style', 'display: ' + val + ';');
            val = !flag ? 'none' : 'block';
            document.querySelectorAll('li#collapserow')[0].setAttribute('style', 'display: ' + val + ';');
        }
    }
    function onClick(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.getColumnByField("TaskId");
        if (args.item.id === "collapserow")
            treegrid.collapseRow(treegrid.getSelectedRows()[0], treegrid.getSelectedRecords()[0]);
        else
            treegrid.expandRow(treegrid.getSelectedRows()[0], treegrid.getSelectedRecords()[0]);
    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

You can hide or show an item in context menu for specific area inside of treegrid by defining the target property.