Row template in EJ2 JavaScript Treegrid control

27 Apr 202314 minutes to read

The rowTemplate has an option to customise the look and behavior of the treegrid rows. The rowTemplate property accepts either the template string or HTML element ID.

var treegrid = new ej.treegrid.TreeGrid({
            dataSource: textdata,
            childMapping: "Children",
            treeColumnIndex: 0,
            rowTemplate: '#rowtemplate',
            height: 250,
            width: 'auto',
            rowHeight: 83,
            columns: [
                { field: "EmpID", headerText: "Employee ID", width: "150" },
                { field: "Name", headerText: "Employee Name", width: '150' },
                { field: "Address", headerText: "Employee Details", width: "350" }
            ]
        });
        treegrid.appendTo('#TreeGrid');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 TreeGrid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript TreeGrid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-grids/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-treegrid/styles/material.css" rel="stylesheet">
    
    
    
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
        <script id="rowtemplate" type="text/x-template">

            <tr>
                <td class="border" style='padding-left:18px;' >
                    <div>${EmpID}</div>
                </td>
                <td class="border" style='padding: 10px 0px 0px 20px;'>
                    <div style="font-size:14px;">
                       ${Name}
                        <p style="font-size:9px;">${Designation}</p>
                    </div>
                </td>
                <td class="border">
                    <div>
                        <div style="position:relative;display:inline-block;">
                           <img src="${FullName}.png" alt="${FullName}" />
                        </div>
                        <div style="display:inline-block;">
                            <div style="padding:5px;">${Address}</div>
                            <div style="padding:5px;">${Country}</div>
                            <div style="padding:5px;font-size:12px;">${Contact}</div>
                        </div>
                    </div>
                </td>
        
            </tr>
        </script>
        
    <div id="container">
        <div id="TreeGrid"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

The rowTemplate property accepts only the TR element.

Row template with formatting

If the rowTemplate is used, the value cannot be formatted inside the template using the columns.format property. In that case, a function should be defined globally to format the value and invoke it inside the template.

var instance = new ej.base.Internationalization();
    window.format = function (value) {
        return instance.formatDate(value, { skeleton: 'yMd', type: 'date' });
    };
    var treegrid = new ej.treegrid.TreeGrid({
        dataSource: textdata,
        childMapping: 'Children',
        treeColumnIndex: 0,
        rowTemplate: '#rowtemplate',
        height: 250,
        width: 'auto',
        rowHeight: 83,
        columns: [
            { field: 'EmpID', headerText: 'Employee ID', width: '150' },
            { field: 'Address', headerText: 'Employee Details', width: '350' },
            { field: 'DOB', headerText: 'DOB', width: '150' }
        ]
    });
    treegrid.appendTo('#TreeGrid');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 TreeGrid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript TreeGrid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-grids/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-calendars/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-treegrid/styles/material.css" rel="stylesheet">
    
    
    
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
        <script id="rowtemplate" type="text/x-template">

            <tr>
                <td class="border" style='padding-left:18px;' >
                    <div>${EmpID}</div>
                </td>
                <td class="border">
                    <div>
                        <div style="position:relative;display:inline-block;">
                           <img src="${FullName}.png" alt="${FullName}" />
                        </div>
                        <div style="display:inline-block;">
                            <div style="padding:5px;">${Address}</div>
                            <div style="padding:5px;">${Country}</div>
                            <div style="padding:5px;font-size:12px;">${Contact}</div>
                        </div>
                    </div>
                </td>
                <td class="border" style='padding-left: 20px;'>
                    <div>${format(data.DOB)}</div>
                </td>
        
            </tr>
        </script>
        
    <div id="container">
        <div id="TreeGrid"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Limitations

Row template feature is not compatible with all the features which are available in treegrid and it has limited features support. Here we have listed out the features which are not compatible with row template feature.

  • Filtering
  • Paging
  • Sorting
  • Scrolling
  • Searching
  • Rtl
  • Context Menu
  • State Persistence