Indent and Outdent in ASP.NET MVC Tree Grid Component

21 Dec 20222 minutes to read

The Indent and Outdent feature will help to change the hierarchy level of rows in tree grid. The indent action moves the selected row as the last child of its previous row, whereas the outdent action moves the selected row as a sibling to its parent row.

To use the indent and outdent feature, inject the RowDD module in the Tree Grid. The tree grid toolbar has the built-in items to execute indent and outdent actions. Define this by using the toolbar property.

@(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(130).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(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height(265).SelectedRowIndex(2).Toolbar(new List<string>() { "Indent", "Outdent" }).ChildMapping("Children").TreeColumnIndex(1).Render())
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Indent/Outdent a row programmatically

You can change the hierarchy level of record programmatically using indent and outdent methods.

@Html.EJS().Button("Indent").Content("Indent").Render()
@Html.EJS().Button("Outdent").Content("Outdent").Render()

@(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(130).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(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height(265).ChildMapping("Children").TreeColumnIndex(1).Render())

<script>
    document.getElementById('Indent').addEventListener('click', () => {
        var treegrid = document.getElementById('TreeGrid').ej2_instances[0];
        treegrid.indent(treegrid.getCurrentViewRecords()[2]);
    });
    document.getElementById('Outdent').addEventListener('click', () => {
        var treegrid = document.getElementById('TreeGrid').ej2_instances[0];
        treegrid.outdent(treegrid.getCurrentViewRecords()[2]);
    });
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

NOTE

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 learn how to present and manipulate data.