Search results

Print

To print the TreeGrid, use the print method from treegrid instance. The print option can be displayed on the toolbar by adding the Print toolbar item.

tagHelper
print.cs
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.dataSource" height="265" childMapping="Children" treeColumnIndex="1" toolbar="@(new List<string>() { "Print" })">
    <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 = TreeData.GetDefaultData();
    ViewBag.dataSource = tree;
    return View();
}

Page setup

Some of the print options cannot be configured through JavaScript code. So, you have to customize the layout, paper size, and margin options using the browser page setup dialog. Please refer to the following links to know more about the browser page setup:

To print the treegrid from an external button, invoke the print method.

tagHelper
print-button.cs
<ejs-button id="print" content="Print"></ejs-button>

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="265" 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>
	
<script>
    document.getElementById('print').addEventListener('click', () => {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.print();
    });
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

By default, the treegrid prints all the pages. To print the current page alone, set the printMode to CurrentPage.

tagHelper
current-page.cs
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="220" childMapping="Children" allowPaging="true" treeColumnIndex="1" toolbar="@(new List<string>() { "Print" })" printMode="CurrentPage">
  <e-treegrid-pagesettings pageSize="8"></e-treegrid-pagesettings>
    <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="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

By default, the browser uses A4 as page size option to print pages and to adapt the size of the page the browser print preview will auto-hide the overflowed contents. Hence treegrid with large number of columns will cut off to adapt the print page.

To show large number of columns when printing, adjust the scale option from print option panel based on your content size.

Scale Option Setting

Show or Hide columns while Printing

You can show a hidden column or hide a visible column while printing the treegrid using toolbarClick and printComplete events.

In toolbarClick event, based on args.item.text as print. We can show or hide columns by setting visible property of e-treegrid-column tag helper to true or false respectively.

In printComplete event, We have reversed the state back to the previous state.

In the below example, we have Duration as a hidden column in the treegrid. While printing, we have changed Duration to visible column and StartDate as hidden column.

tagHelper
show-hide.cs
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="265" childMapping="Children" treeColumnIndex="1" toolbar="@(new List<string>() { "Print"})" toolbarClick="toolbarClick" printComplete="printComplete">
    <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="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80" visible="false"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
		
<script>
    function toolbarClick(args) {
       if (args.item.text === 'Print') {
            var cols = this.grid.columns;
            for (var i = 0; i < cols.length; i++) {
                if (cols[i].field == "Duration") {
                    cols[i].visible = true;
                }
                else if (cols[i].field == "StartDate") {
                    cols[i].visible = false;
                }
            }
        }
    }

    function printComplete(args) {
        var cols = this.grid.columns;
        for (var i = 0; i < cols.length; i++) {
            if (cols[i].field == "Duration") {
                cols[i].visible = false;
            }
            else if (cols[i].field == "StartDate") {
                cols[i].visible = true;
            }
        }
    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Limitations of Printing Large Data

When treegrid contains large number of data, printing all the data at once is not a best option for the browser performance. Because to render all the DOM elements in one page will produce performance issues in the browser. It leads to browser slow down or browser hang.

If printing of all the data is still needed, we suggest to Export the treegrid to Excel or CSV or Pdf and then print it from another non-web based application.