Command Column Editing in ASP.NET MVC Tree Grid Component

21 Dec 20225 minutes to read

The command column provides an option to add CRUD action buttons in a column. This can be defined by the Commands property of Column API.

The available built-in command buttons are:

Command Button Actions
Edit Edit the current row.
Delete Delete the current row.
Save Update the edited row.
Cancel Cancel the edited state.
@using Syncfusion.EJ2.Grids


@{
    List<object> commands = new List<object>();
    commands.Add(new { type = "Edit", buttonOption = new { iconCss = "e-icons e-edit", cssClass = "e-flat" } });
    commands.Add(new { type = "Delete", buttonOption = new { iconCss = "e-icons e-delete", cssClass = "e-flat" } });
    commands.Add(new { type = "Save", buttonOption = new { iconCss = "e-icons e-update", cssClass = "e-flat" } });
    commands.Add(new { type = "Cancel", buttonOption = new { iconCss = "e-icons e-cancel-icon", cssClass = "e-flat" } });
}


@(Html.EJS().TreeGrid("TreeGrid").DataSource((IEnumerable<object>)ViewBag.datasource)
       .EditSettings(edit =>
        {
          edit.AllowAdding(true);
          edit.AllowDeleting(true);
          edit.AllowEditing(true);
          edit.ShowDeleteConfirmDialog(true);
          edit.Mode(Syncfusion.EJ2.TreeGrid.EditMode.Row);

        })
        .Columns(col =>
         {
           col.Field("TaskId").HeaderText("Task ID").IsPrimaryKey(true).Width(120)
              .TextAlign(TextAlign.Right).Add();
           col.Field("TaskName").HeaderText("Task Name").Add();
           col.Field("StartDate").HeaderText("Start Date").Width(150).Format("yMd")
              .EditType("datepickeredit").TextAlign(TextAlign.Right).Add();
           col.Field("Duration").HeaderText("Duration").Width("110").EditType("numericedit")
              .Edit(new { @params = new { format = "n" } }).TextAlign(TextAlign.Right).Add();
           col.HeaderText("Manage Records").Width("160").Commands(commands).Add();

         }).Height(400).ChildMapping("Children").TreeColumnIndex(1).Render()
)
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.dataSource = tree;
    return View();
}

Custom command

The custom command buttons can be added in a column by using the Commands property of Column API and the action for the custom buttons can be defined in the ButtonOption.Click event.

@using Syncfusion.EJ2.Grids


@{
    List<object> commands = new List<object>();
    commands.Add(new { type = "taskstatus", buttonOption = new { content = "Details", cssClass = "e-flat e-details" } }); // custom
}

<script>
    function load() {
        this.columns[4].commands[0].buttonOption.click = function (args) {     //click event for custom command button
            var treegrid = document.getElementById('TreeGrid').ej2_instances[0];  // treegrid instance
            var rowObj = treegrid.grid.getRowObjectFromUID(ej.base.closest(args.target, '.e-row').getAttribute('data-uid'));
            alert(JSON.stringify(rowObj.data));  // display row data
        }
    }
</script>

@(Html.EJS().TreeGrid("TreeGrid").DataSource((IEnumerable<object>)ViewBag.datasource)
       .EditSettings(edit =>
        {
          edit.AllowAdding(true);
          edit.AllowDeleting(true);
          edit.AllowEditing(true);
          edit.ShowDeleteConfirmDialog(true);
          edit.Mode(Syncfusion.EJ2.TreeGrid.EditMode.Row);

        })
        .Columns(col =>
         {
           col.Field("TaskId").HeaderText("Task ID").IsPrimaryKey(true).Width(120)
              .TextAlign(TextAlign.Right).Add();
           col.Field("TaskName").HeaderText("Task Name").Add();
           col.Field("StartDate").HeaderText("Start Date").Width(150).Format("yMd")
              .EditType("datepickeredit").TextAlign(TextAlign.Right).Add();
           col.Field("Duration").HeaderText("Duration").Width("110").EditType("numericedit")
              .Edit(new { @params = new { format = "n" } }).TextAlign(TextAlign.Right).Add();
           col.HeaderText("Manage Records").Width("160").Commands(commands).Add();

         }).Height(400).ChildMapping("Children").TreeColumnIndex(1).Load("load").Render()
)
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.dataSource = tree;
    return View();
}

NOTE

You can refer to our ASP.NET MVC Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET MVC Tree Grid example to knows how to present and manipulate data.