We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to use template property of grid Column dynamically in typescript

Thread ID:





145138 Jun 9,2019 12:08 PM UTC Jun 11,2019 11:57 AM UTC Angular - EJ 2 3
Tags: Grid
Jairo Martins Marques
Asked On June 9, 2019 12:08 PM UTC

Hi guys,

I did the following modifications to create my columns dynamically, so:

I remove all <e-column> from my html that now have only the <ejs-grid> tag.
<ejs-grid #grid id='grid' [dataSource]='data' rowHeight='38' [allowSelection]='true' [allowSorting]='true' height='600'
(dataBound)='onDataBound($event)' [enableHover]='false' [selectionSettings]='selectionSettings' [filterSettings]='filterSettings'
[allowSorting]='true' [allowFiltering]='true' [enableVirtualization]='true'>

I changed the ngOnInit to generate the columns dynamically

public ngOnInit(): void {
this.data = getData(1000);
this.filterSettings = { type: "Menu" };
this.filter = { type: "CheckBox" };
this.stTime = performance.now();
this.gridInstance.on('data-ready', function () {
this.dReady = true;
this.selectionSettings = {persistSelection: true, type: "Multiple", checkboxOnly: true };

     defineColumns() : void {
this.gridInstance.columns = [
{ field: "EmployeeID", visible: false, headerText: "Employee ID", width: '130', isPrimaryKey: true },
{ field: 'Employees',
          template: "<div><div class='empimg'><span *ngIf=\"data.EmployeeImg === 'usermale'\" class='e-userimg sf-icon-Male'></span><span *ngIf=\"data.EmployeeImg === 'userfemale'\" class='e-userimg sf-icon-Female'></span></div><span id='Emptext'>\{\{data.Employees\}\}</span></div>",
headerText: 'Employee Name', filter: { type: "CheckBox" }, clipMode: 'EllipsisWithTooltip', width: '200' },
{ field: 'Designation', headerText: 'Designation', filter: { type: "CheckBox" }, clipMode: 'EllipsisWithTooltip', width: '170' },
{ field: 'Mail', headerText: 'Mail', filter: { type: "Menu" }, width: '230' },
{ field: 'Location', headerText: 'Location', filter: { type: "CheckBox" }, width: '140' },
{ field: 'Status', filter: { type: "CheckBox" }, headerText: 'Status', width: '150' },
{ field: 'Trustworthiness', filter: { type: "CheckBox" }, headerText: 'Trustworthiness', width:'200' },
{ field: 'Rating', allowFiltering: false, headerText: 'Rating', width: '160' },
{ field: 'Software', headerText: 'Software', allowSorting: false, allowFiltering: false, width:'180' },
{ field: 'CurrentSalary', headerText: 'Current Salary', filter: { type: "Menu" }, format: 'C2', width: '160' },
{ field: 'Address', headerText: 'Address', filter: { type: "Menu" }, clipMode: 'EllipsisWithTooltip', width: '240' }

Everything works as expected, except the template property. I didn't found any example nor documentation about how to use it, so what i'm doing wrong?

Thavasianand Sankaranarayanan [Syncfusion]
Replied On June 10, 2019 02:09 PM UTC

Hi Jairo, 

Greetings from Syncfusion support. 

We can achieve your requirement by set the template inside a ng-template tag in the html page and refer those template in the typescript page. 

Refer the below code example. 


<ejs-grid #grid [dataSource]='data' height='350'> 

    <ng-template #template let-data> 
    <div class='empimg'><span>{{data.CustomerName}}</span></div> 

export class AppComponent { 
    public data: Object[] = []; 
    public gridInstance : GridComponent ;  
    public toolbarTemplate: any; 
    ngOnInit(): void { 
        this.data = orderDetails; 
    defineColumns() : void { 
       this.gridInstance.columns = 
            headerText: 'Employee Image', textAlign: 'Center', 
            template: this.toolbarTemplate, width: 150 
        { field: 'ShipCountry', headerText: 'Ship Country', width: 120 }, 
        { field: 'ShipCity', headerText: 'Ship City', width: 170 } 

We have prepared a simple sample in the following stackblitz link. 

Thavasianand S. 

Jairo Martins Marques
Replied On June 10, 2019 02:13 PM UTC

Hi Thavasianand S,

thank you for your promptly reply.

Any chance to use only Typescript code? I'd like to get all my grid templates from a database.


Jairo Marques

Thavasianand Sankaranarayanan [Syncfusion]
Replied On June 11, 2019 11:57 AM UTC

Hi Jairo, 

We can render the template using the typescript code but we cannot do data operations as we have in our previous sample. But we can render the element as we give in the template property in typescript. Because in angular we need to render the template inside the ng-template tag alone this is the default one.  

We have prepared a simple sample in the following stackblitz link with rendering a element. 

Thavasianand S. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon