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.

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.
razor
default-contextmenu.cs
@(Html.EJS().TreeGrid("TreeGrid").AllowPaging().AllowSorting().AllowExcelExport()
      .AllowPdfExport()
      .AllowResizing()
      .EditSettings(edit => edit.AllowAdding(true).AllowDeleting(true).AllowEditing(true).Mode(Syncfusion.EJ2.TreeGrid.EditMode.Row))
      .PageSettings(page => page.PageSize(7))
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .ContextMenuItems(
          new List<object>() { "SortAscending",
              "SortDescending", "Edit", "Delete",
              "Save", "Cancel", "PdfExport", "ExcelExport", "CsvExport",
              "FirstPage", "PrevPage", "LastPage", "NextPage"
          })
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).IsPrimaryKey(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").EditType("datepickeredit")
                  .TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(90).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).EditType("numericedit")
                  .TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).ChildMapping("Children").TreeColumnIndex(1).Render())
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.

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

@(Html.EJS().TreeGrid("TreeGrid")
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
        col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(90).Add();
        col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).ChildMapping("Children").ContextMenuItems(ContextMenuitems).ContextMenuOpen("ContextMenuOpen").ContextMenuClick("ContextMenuClick")
         .TreeColumnIndex(1).Render())

<script>
    function ContextMenuOpen(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        var elem = args.event.target;
        var uid = elem.closest('.e-row').getAttribute('data-uid');
        if (ej.base.isNullOrUndefined(ej.base.getValue('hasChildRecords', treegrid.grid.getRowObjectFromUID(uid).data))) {
            args.cancel = true;
        } else {
            var flag = ej.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 ContextMenuClick(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.