- Home
- Forum
- Angular - EJ 2
- How to use template property of grid Column dynamically in typescript
How to use template property of grid Column dynamically in typescript
Hi guys,
I'm using the sample provide by https://ej2.syncfusion.com/angular/demos/#/material/grid/over-view
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.defineColumns();
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' }
];
this.gridInstance.refreshHeader();
}
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?
SIGN IN To post a reply.
3 Replies
TS
Thavasianand Sankaranarayanan
Syncfusion Team
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.
|
[app.component.html]
<ejs-grid #grid [dataSource]='data' height='350'>
</ejs-grid>
<ng-template #template let-data>
<div class='empimg'><span>{{data.CustomerName}}</span></div>
</ng-template>
---------------------------------------------------------------------------------------------------------------------------
[app.component.ts]
export class AppComponent {
public data: Object[] = [];
@ViewChild('grid')
public gridInstance : GridComponent ;
@ViewChild('template')
public toolbarTemplate: any;
ngOnInit(): void {
this.data = orderDetails;
this.defineColumns();
}
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.
Regards,
Thavasianand S.
JM
Jairo Martins Marques
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.
Regards,
Jairo Marques
TS
Thavasianand Sankaranarayanan
Syncfusion Team
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.
Regards,
Thavasianand S.
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
JM Jairo Martins Marques
- Jun 9, 2019 12:08 PM UTC
- Jun 11, 2019 11:57 AM UTC