Scrolling in Tree Grid Control

9 Jun 20257 minutes to read

The scrollbar will be displayed in the treegrid when content exceeds the element width or height. The vertical and horizontal scrollbars will be displayed based on the following criteria:

  • The vertical scrollbar appears when the total height of rows present in the treegrid exceeds its element height.
  • The horizontal scrollbar appears when the sum of columns width exceeds the treegrid element width.
  • The height and width are used to set the treegrid height and width, respectively.

NOTE

The default value for height and width is auto.

Set width and height

To specify the width and height of the scroller in the pixel, set the pixel value to a number.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="315" width="400" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-columns>
     <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
     <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
     <e-treegrid-column field="StartDate" headerText="Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
     <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

Responsive with parent container

Specify the width and height as 100% to make the treegrid element fill its parent container. Setting the height to 100% requires the treegrid parent element to have explicit height.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="100%" width="100%" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-columns>
      <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
      <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
      <e-treegrid-column field="StartDate" headerText="Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
      <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

The Syncfusion ASP.NET Core TreeGrid provides a useful feature to keep the column headers fixed (sticky) while scrolling through large datasets. This ensures that the headers remain visible at all times, enhancing user experience by making it easier to understand the context of the data displayed, especially when dealing with wide or long hierarchical data.

For example, in a project management application, users often need to scroll through a detailed list of tasks and subtasks. When the dataset is large, scrolling down can cause confusion if the column headers scroll out of view, making it difficult to remember what each column represents. By enabling sticky headers, the column headers remain visible even while scrolling, allowing users to easily keep track of the data context.

To enable sticky headers in the TreeGrid, you can simply set the enableStickyHeader property to true. This makes the column headers stick to the top of the TreeGrid container or its parent scrolling container when you scroll vertically.

The following sample demonstrates how to enable or disable the sticky header in the TreeGrid using a Switch and its Change event:

<div style="height: 400px; overflow-y: auto; padding: 0px 10px 0px 10px; border: 1px solid #ddd;">
    <div style="padding: 20px 0px 20px 0px; display: flex">
      <label style="margin: -2px 5px 0px 0px; font-weight: bold;">Enable/Disable Sticky Header</label>
      <ejs-switch id="switch" checked="true" change="toggleStickyHeader"></ejs-switch>
    </div>
    <div style='height: 1000px'>
      <ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" childMapping="Children" treeColumnIndex="1" enableStickyHeader="true" height="100%">
        <e-treegrid-columns>
          <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
          <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
          <e-treegrid-column field="StartDate" headerText="Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
          <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
        </e-treegrid-columns>
      </ejs-treegrid>
    </div>
</div>

<script>
  function toggleStickyHeader(args) {
    var treeGrid = document.getElementById("TreeGrid").ej2_instances[0];
    treeGrid.enableStickyHeader = args.checked ?? false;
  }
</script>
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

Scroll to selected row

You can scroll the treegrid content to the selected row position by using the rowSelected event.

<ejs-numerictextbox id="numeric" change="onChange" min="0" width="200" showSpinButton="false" format="##" placeholder="Enter index to select a row"></ejs-numerictextbox>

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="270" width="100%" childMapping="Children" treeColumnIndex="1" rowSelected="rowSelected">
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText="Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function onChange(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.selectRow(parseInt(this.getText(), 10));
    }
    function rowSelected(args) {
        var rowHeight = this.getRows()[treegrid.getSelectedRowIndexes()[0]].scrollHeight;
        this.getContent().children[0].scrollTop = rowHeight * this.getSelectedRowIndexes()[0];
    }
</script>
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}