Columns in ASP.NET CORE Tree Grid Component

21 Dec 202318 minutes to read

The column definitions are used as the dataSource schema in the TreeGrid. This plays a vital role in rendering column values in the required format. The treegrid operations such as sorting, filtering and searching etc. are performed based on column definitions. The field property of e-treegrid-columns tag helper is necessary to map the data source values in TreeGrid columns.

NOTE

  1. If the column field is not specified in the dataSource, the column values will be empty.

    2. If the field name contains “dot” operator, it is considered as complex binding.

treeColumnIndex property denotes the column that is used to expand and collapse child rows.

Format

To format cell values based on specific culture, use the format property of e-treegrid-column tag helper. The TreeGrid uses Internalization library to format the number values.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" childMapping="Children" height="315" treeColumnIndex="1">
    <e-treegrid-columns>
        <e-treegrid-column field="OrderID" headerText="Order ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="OrderName" headerText="Order Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="Price" headerText="Price" textAlign="Right" width="90" format="c2" type="number"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetFormatData();
    ViewBag.data = tree;
    return View();
}

NOTE

By default, the number and date values are formatted in en-US locale.

Number formatting

The number or integer values can be formatted using the below format strings.

Format Description Remarks
N Denotes numeric type. The numeric format is followed by integer value as N2, N3. etc which denotes the number of precision to be allowed.
C Denotes currency type. The currency format is followed by integer value as C2, C3. etc which denotes the number of precision to be allowed.
P Denotes percentage type The percentage format expects the input value to be in the range of 0 to 1. For example the cell value 0.2 is formatted as 20%. The percentage format is followed by integer value as P2, P3. etc which denotes the number of precision to be allowed.

Refer to the link to know more about Number formatting.

Date formatting

You can format date values either using built-in date format string or custom format string.

For built-in date format you can specify format property as string (Example: yMd). Refer to the link to know more about Date formatting.

You can also use custom format string to format the date values. Some of the custom formats and the formatted date values are given in the below table.

Format Formatted value
{ type:’date’, format:’dd/MM/yyyy’ } 04/07/1996
{ type:’date’, format:’dd.MM.yyyy’ } 04.07.1996
{ type:’date’, skeleton:’short’ } 7/4/96
{ type: ‘dateTime’, format: ‘dd/MM/yyyy hh:mm a’ } 04/07/1996 12:00 AM
{ type: ‘dateTime’, format: ‘MM/dd/yyyy hh:mm:ss a’ } 07/04/1996 12:00:00 AM
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" load="onLoad" childMapping="subTasks" height="315" treeColumnIndex="1">
    <e-treegrid-columns>
        <e-treegrid-column field="orderID" headerText="Order ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="orderName" headerText="Order Name" width="220"></e-treegrid-column>
        <e-treegrid-column field="orderDate" headerText="Order Date" textAlign="Right" width="160" ></e-treegrid-column>
        <e-treegrid-column field="price" headerText="Price" textAlign="Right" width="90" format="C2" type="number"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function onLoad() {
        var treegridObj = document.getElementById("TreeGrid").ej2_instances[0];
        for (i = 0; i < treegridObj.columns.length; i++) {
            if (treegridObj.columns[i].field == "orderDate") {
                treegridObj.columns[i].format = { type: 'dateTime', format: 'dd/MM/yyyy' };
            }
        }

    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetFormatData();
    ViewBag.data = tree;
    return View();
}

Lock columns

You can lock columns by using column.lockColumn property. The locked columns will be moved to the first position. Also you can’t reorder its position.

In the below example, Duration column is locked and its reordering functionality is disabled.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" childMapping="Children" treeColumnIndex="1" allowReordering="true" height="315">
    <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" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80" lockColumn="true"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Column type

Column type can be specified using the type property of e-treegrid-column tag helper. It specifies the type of data the column binds.

If the format is defined for a column, the column uses type to select the appropriate format option (number or date).

TreeGrid column supports the following types:

  • string
  • number
  • boolean
  • date
  • datetime

NOTE

If the type is not defined, it will be determined from the first record of the dataSource.

Checkbox column

To render checkboxes in existing column, you need to set [showCheckbox] property of e-treegrid-column as true.

It is also possible to select the rows hierarchically using checkboxes in TreeGrid by enabling [autoCheckHierarchy] property. When we check on any parent record checkbox then the child record checkboxes will get checked.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" height="400" autoCheckHierarchy="true" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="100"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" showCheckbox="true" width="190"></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();
}

Controlling Tree Grid actions

You can enable or disable treegrid action for a particular column by setting the allowFiltering, and allowSorting properties of e-treegrid-column tag helper.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" allowFiltering="true" allowSorting="true" childMapping="Children" treeColumnIndex="1" allowPaging="true">
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" allowSorting="false" 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" allowFiltering="false" textAlign="Right" format="yMd" 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.data = tree;
    return View();
}

Show/hide columns by external button

You can show or hide treegrid columns dynamically using external buttons by invoking the showColumns or hideColumns method.

<ejs-button id="hide" content="Hide Columns"></ejs-button>
<ejs-button id="show" content="showColumns"></ejs-button>

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" allowFiltering="true" allowSorting="true" childMapping="Children" treeColumnIndex="1" allowPaging="true">
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" allowSorting="false" 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" allowFiltering="false" textAlign="Right" format="yMd" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    document.getElementById('show').onclick = () => {
        var treeGridObj = document.getElementById("TreeGrid").ej2_instances[0];
        treeGridObj.showColumns(['Task ID', 'Duration']); //show by HeaderText
    };

    document.getElementById('hide').onclick = () => {
        var treeGridObj = document.getElementById("TreeGrid").ej2_instances[0];
        treeGridObj.hideColumns(['Task ID', 'Duration']); //hide by HeaderText
    };
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.data = tree;
    return View();
}

ValueAccessor

The valueAccessor is used to access/manipulate the value of display data. You can achieve custom value formatting by using the valueAccessor.

@{
    Object orderFormatter = "orderFormatter";
    Object currencyFormatter = "currencyFormatter";
}

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" childMapping="subTasks" height="315" treeColumnIndex="1">
    <e-treegrid-columns>
        <e-treegrid-column field="orderID" headerText="Order ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="orderName" headerText="Order Name" width="180" valueAccessor="orderFormatter"></e-treegrid-column>
        <e-treegrid-column field="orderDate" headerText="Order Date" format="yMd" textAlign="Right" width="160" ></e-treegrid-column>
        <e-treegrid-column field="price" headerText="Price" textAlign="Right" width="90"  valueAccessor="currencyFormatter"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function currencyFormatter(field, data, column) {
        return '€' + data['price'];
    }

    function orderFormatter(field, data, column) {
        return data[field] + '-' + data['category'];
    }

</script>
public IActionResult Index()
{
    var tree = TreeData.GetFormatData();
    ViewBag.data = tree;
    return View();
}

Display array type columns

You can bind an array of objects in a column by using the valueAccessor property.
In this example, the name field has an array of two objects, FirstName and LastName. These two objects are joined and bound to a column using the valueAccessor.

@{
    Object valueAccessfn = "orderFormatter";
}
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" 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="Name" headerText="Assignee" valueAccessor="valueAccessfn" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function orderFormatter(field, data, column) {
        return data[field].map(function(args) {
        return args.FirstName || args.LastName }).join(' ');
    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.data = tree;
    return View();
}

Expression column

You can achieve the expression column by using the valueAccessor property.

@{
    Object totalPrice = "totalPrice";
}

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" childMapping="subTasks" height="315" treeColumnIndex="1">
    <e-treegrid-columns>
        <e-treegrid-column field="orderID" headerText="Order ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="orderName" headerText="Order Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="units" headerText="Units" textAlign="Right" width="120"></e-treegrid-column>
        <e-treegrid-column field="unitPrice" headerText="Unit Price" textAlign="Right" width="120"></e-treegrid-column>
        <e-treegrid-column field="price" headerText="Total Price" textAlign="Right" width="90" valueAccessor="totalPrice"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function totalPrice(field, data, column) {
        return data.units * data.unitPrice;
    };
</script>
public IActionResult Index()
{
    var tree = TreeData.GetFormatData();
    ViewBag.data = tree;
    return View();
}

How to render boolean values as checkbox

To render boolean values as checkbox in columns, you need to set displayAsCheckBox property as true.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.data" height="315" 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="Approved" headerText="Approved" displayAsCheckBox="true" textAlign="Center" width="180"></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.data = tree;
    return View();
}

NOTE

You can refer to our ASP.NET Core Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET Core Tree Grid example ASP.NET Core Tree Grid example to knows how to present and manipulate data.