Scrolling

21 Dec 20223 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.

@(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(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(120).Add();
        col.Field("Duration").HeaderText("Duration").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height(315).Width(400).ChildMapping("Children").TreeColumnIndex(1).Render())
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.

@(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(180).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(120).Add();
         col.Field("Duration").HeaderText("Duration").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height("100%").Width("100%").ChildMapping("Children").TreeColumnIndex(1).Render()
)
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.

@Html.EJS().NumericTextBox("numeric").Format("##").Min(0).Width(200).ShowSpinButton(false).Placeholder("Enter index to select a row").Change("onChange").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(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(120).Add();
        col.Field("Duration").HeaderText("Duration").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height(270).Width("100%").ChildMapping("Children").TreeColumnIndex(1).RowSelected("rowSelected").Render()
)

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