Scrolling in Tree Grid Control

21 Dec 20224 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();
}

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();
}