Row Template in ASP.NET MVC Tree Grid Component

10 Jan 20239 minutes to read

The RowTemplate has an option to customize the look and behavior of the treegrid rows. The RowTemplate property accepts either the Template string or HTML element ID.

@Html.EJS().TreeGrid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
       {
           col.Field("EmpID").HeaderText("Employee ID").Width(150).TextAlign(TextAlign.Right).Add();
           col.Field("Name").HeaderText("Employee Name").Width(150).Add();
           col.Field("Address").HeaderText("Employee Details").Width(350).Add();
       }).Height(250).ChildMapping("Children").TreeColumnIndex(0).RowHeight(83).RowTemplate("#rowtemplate").Render()

    <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="../Content/images/treegrid/${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>
    <style type="text/css">

        .border {
            border-color: #e0e0e0;
            border: 1px solid #e0e0e0;
            border-width: 1px 0 0 0;
        }

        img {
            width: 60px;
            height: 60px;
            vertical-align: baseline;
            border-radius: 50px;
            margin-left: 20px;
            background-repeat: no-repeat;
        }
    </style>
public IActionResult RowTemplate()
        {
            ViewBag.datasource = TreeData.GetTemplateData();
            return View();
        }

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.

@Html.EJS().TreeGrid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
       {
           col.Field("EmpID").HeaderText("Employee ID").Width(150).Add();
           col.Field("Address").HeaderText("Employee Details").Width(350).Add();
           col.Field("DOB").HeaderText("DOB").Width(150).Add();
       }).Height(250).ChildMapping("Children").TreeColumnIndex(0).RowHeight(83).RowTemplate("#rowtemplate").Render()

    <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="../Content/images/treegrid/${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>${DOB.toLocaleDateString()}</div>
            </td>
        </tr>
    </script>
    <style type="text/css">

        .border {
            border-color: #e0e0e0;
            border: 1px solid #e0e0e0;
            border-width: 1px 0 0 0;
        }

        img {
            width: 60px;
            height: 60px;
            vertical-align: baseline;
            border-radius: 50px;
            margin-left: 20px;
            background-repeat: no-repeat;
        }
    </style>
public IActionResult RowTemplate()
        {
            ViewBag.datasource = TreeData.GetTemplateData();
            return View();
        }

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

NOTE

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