Column template in TypeScript Grid control

18 Nov 20185 minutes to read

Grid control provides a template option that allows you to display custom elements in a column instead of the field value. This can be useful when you need to display images, buttons, or other custom content within a column.

When using template columns, they are primarily meant for rendering custom content and may not provide built-in support for grid actions like sorting, filtering, editing. It is must to define the field property of the column to perform any grid actions.

Render image in a column

To render an image in a grid column, you need to define a template for the column using the template property. The template property expects the HTML element or a function that returns the HTML element.

The following example demonstrates how to define a template for the Employee Image field that displays an image element. The template property is set to the HTML element that contains an image tag. You have utilized the src and alt attributes to an image tag.

The template option allows to define any HTML content within a column.

The Grid control provides support for rendering hyperlink columns and performing routing on click using the template property. This feature is useful when displaying data that requires a link to another page or website.

The following example demonstrates, how to render hyperlink column in the Grid using the template property of the column. To define a template for the column, you can use the template with the a tag to create the hyperlink.

The window.open() method is a built-in JavaScript function that opens a new browser window or tab with the specified URL.

Render other controls in a column

The column template has options to render a custom control in a grid column instead of a field value.

Render LineChart control in a column

In the following example, we have rendered the Sparkline Chart control in the Grid column by defining the template property.

Render ColorPicker control in a column

In the following code, we rendered the ColorPicker control in the Grid column by defining the template property.

Render DropDownList control in a column

Render Chip control in a column

The Grid control provides support for rendering Chips control in a column using the template property. This feature is useful when displaying data that requires a chip control to be rendered in a column.

In the following code, we rendered the Chips control in the Grid First Name column by defining the template property.

Render ProgressBar control in a column

In the following code, the Progress Bar control render in the Grid Freight column by defining the template property.

Render RadioButton in a column

Using condition template

The conditional column template allows you to display template elements based on specific conditions.

The following example demonstrates how to use the template property with the template element and add the condition to render the checkbox based on the value of the Discontinued field. The Discontinued field will render a checkbox in each row for which the value of the Discontinued field is true.

  <script id="template" type="text/x-template">
            <div class="template_checkbox">
                ${if(Discontinued)}
                <input type="checkbox" checked> ${else}
                <input type="checkbox"> ${/if}
            </div>
        </script>

You can use any template element or custom control instead of the checkbox in the conditional template based on your requirement.

How to get the row object by clicking on the template element

The Grid control allows you to retrieve the row object of the selected record when clicking on a template element. This feature can be useful when you need to perform custom actions based on the selected record.

In the following code, the button element is rendered in the Employee Data column and click event binding is used to call the showDetails method when the template element is clicked. The showDetails method is passed the data object as an argument, which allows you to access the selected row object and display it in the dialog popup.

Use custom helper inside the template

The Syncfusion® Grid allows you to use custom helpers inside the template property of a column. This feature allows you to create complex templates that can incorporate additional helper functions that are not available through the default template syntax.

To use the custom helper function inside a column template, you must first add the function to the template’s context.

The following example demonstrates how to use a custom helper function inside the template property, using the template element for the Freight column.

Custom helpers can only be used inside the template property of a column.

Dynamically adding template column

The Syncfusion® Grid control allows you to dynamically add template columns at runtime. This capability is particularly useful when the structure of the grid needs to be modified based on individual interactions or other dynamic conditions.

Dynamically adding template columns involves creating and inserting columns with custom templates after the grid has been initialized. This approach provides flexibility in presenting data in a highly customizable manner.

Enhancing Grid performance by enabling or disabling Aria Labels

By default, the TypeScript Grid adds custom aria-label attributes to template cells by combining the cell value, the “is template cell” identifier, and the column header name. These attributes help screen readers provide meaningful context.

If your application doesn’t require screen reader support and includes multiple template columns, Aria labels may impact performance. To improve rendering, you can disable them for all template columns by setting the enableAriaLabel property to false in the templateOptions of those columns. If accessibility is needed, set it to true to retain Aria labels.

The example below enables Aria labels for the Employee Image column and disables them for the First Name column in the TypeScript Grid.