Column Chooser in ASP.NET MVC Tree Grid Component

21 Dec 20222 minutes to read

The column chooser has options to show or hide columns dynamically. It can be enabled by defining the ShowColumnChooser as true.

@(Html.EJS().TreeGrid("ColumnChooser").ShowColumnChooser(true).PageSettings(p => p.PageSize(10))
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).ShowInColumnChooser(false).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).AllowPaging().Toolbar(new List<string>() { "ColumnChooser" })Render()
)
public ActionResult ColumnChooser()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

NOTE

You can hide the column names in column chooser by defining the ShowInColumnChooser as false.

Open column chooser by external button

The Column chooser can be displayed on a page through external button by invoking the openColumnChooser method with X and Y axis positions.

@Html.EJS().Button("opencolumnchooser").Content("Open Column Chooser").IsPrimary(true).Render()

@(Html.EJS().TreeGrid("ColumnChooserTreeGrid").
      .ShowColumnChooser()
      .PageSettings(p => p.PageSize(10))
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).ShowInColumnChooser(false).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).AllowPaging().Render()
)
<script>
    document.getElementById('opencolumnchooser').addEventListener("click", () => {
        var treeGrid = document.getElementById('ColumnChooserTreeGrid').ej2_instances[0];
       treeGrid.columnChooserModule.openColumnChooser(200, 50); // give X and Y axis
    });
</script>
public ActionResult ColumnChooserbutton()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    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.