|
this.childGrid = {
dataSource: orderDatas,
queryString: "EmployeeID",
----
columns: [
----
{
field: "ShipName",
headerText: "Ship Name",
// render the template element as htmlstring
template: "<div><a rel='nofollow' href='https://ej2.syncfusion.com/documentation/grid/'> ${ShipName}</a></div>",
width: 150
}
]
}; |
|
[app.component.html]
// render the child-Grid’s column template elements inside the ng-template
<ng-template #childtemplate let-data>
<div>
</div>
</ng-template>
[app.component.ts]
import {Component, OnInit, ViewEncapsulation, ViewContainerRef, ViewChild, TemplateRef, Inject } from "@angular/core";
----
@Component({
selector: "app-root",
templateUrl: "app.component.html",
providers: [DetailRowService]
})
export class AppComponent {
constructor(
@Inject(ViewContainerRef) private viewContainerRef?: ViewContainerRef
) {}
@ViewChild("grid", { static: true })
public grid: GridComponent;
@ViewChild("childtemplate", { static: true })
public childtemplate: any;
public parentData: Object[];
public childGrid: any;
ngOnInit(): void {
this.parentData = employeeData;
this.childGrid = {
dataSource: orderDatas,
queryString: "EmployeeID",
allowPaging: true,
pageSettings: { pageSize: 6, pageCount: 5 },
load: function() {
this.registeredTemplate = {}; //set registertemplate value as empty in load event
},
columns: [
{
field: "OrderID",
headerText: "Order ID",
textAlign: "Right",
width: 120
},
{
field: "ShipCity",
headerText: "Ship City",
template: this.childtemplate, // render ng template in the column
width: 120
}
]
};
}
ngAfterViewInit() {
this.childtemplate.elementRef.nativeElement._viewContainerRef = this.viewContainerRef;
}
} |
|
export class AppComponent {
-----
ngOnInit(): void {
this.childGrid = {
----
columns: [
---
{
field: "ShipName",
headerText: "Ship Name",
template:
"<div><a rel='nofollow' href='https://ej2.syncfusion.com/documentation/grid/' onClick='myfunction(event)'> ${ShipName}</a></div>",
width: 150
}
]
};
}
}
(window as any).myfunction = function(args) {
console.log(args);
};
|
|
[app.component.html]
<ng-template #childtemplate let-data>
<div>
<a rel='nofollow' href="https://ej2.syncfusion.com/documentation/grid/" (click)="hyperclick($event)">{{data.ShipCity}}</a>
</div>
</ng-template>
[app.component.ts]
export class AppComponent {
-----
hyperclick(args) {
console.log(this);
console.log(args);
}
}
|
The link is NOT showing UP for some reason with ng-templateIt is NOT able to get reference to childtemplateTypeError: Cannot read property 'elementRef' of undefinedat SprintsComponent.push../src/app/project-charts/sprints/sprints.component.ts.SprintsComponent.ngAfterViewInit (sprints.component.ts:805)at callProviderLifecycles (core.js:21414)at callElementProvidersLifecycles (core.js:21388)at callLifecycleHooksChildrenFirst (core.js:21378)at checkAndUpdateView (core.js:29448)at callViewAction (core.js:29680)at execEmbeddedViewsAction (core.js:29643)at checkAndUpdateView (core.js:29440)at callViewAction (core.js:29680)at execComponentViewsAction (core.js:29622)<ng-template #childtemplate let-data><div><a rel='nofollow' rel='nofollow' href="https://ej2.syncfusion.com/documentation/grid/" (click)="hyperclick($event)">{{data.itemName}}a>div>ng-template>import { Component,TemplateRef, Inject, AfterViewInit, ViewContainerRef, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';import { EditSettingsModel, CellEditArgs, ToolbarItems, IEditCell, CommandModel, ContextMenuItemModel, FilterSettingsModel, SaveEventArgs, GridComponent, Column } from '@syncfusion/ej2-angular-grids';import { PageSettingsModel, CommandClickEventArgs } from '@syncfusion/ej2-angular-grids';import { ProjectChartsService } from '../project-charts.service';import { LeftNavService } from 'src/app/onboarding/left-nav.service';import { TabComponent } from '@syncfusion/ej2-angular-navigations';import { ClickEventArgs, EventArgs } from '@syncfusion/ej2-angular-navigations';import { DialogComponent } from '@syncfusion/ej2-angular-popups';import { EmitType } from '@syncfusion/ej2-base';import { GroupSettingsModel,parentsUntil,DetailRowService } from '@syncfusion/ej2-angular-grids';import { AnimationModel, RangeColorModel, ProgressBar } from '@syncfusion/ej2-progressbar';import { ProgressTheme, ILoadedEventArgs } from '@syncfusion/ej2-progressbar';import { FontModel } from '@syncfusion/ej2-angular-charts';import { Router } from '@angular/router';@Component({selector: 'app-sprints',templateUrl: './sprints.component.html',styleUrls: ['./sprints.component.css'],providers: [DetailRowService]})export class SprintsComponent implements OnInit, AfterViewInit {@ViewChild("grid", { static: false }) grid: GridComponent;@ViewChild('element', { static: true }) element;@ViewChild('tabsElement', { static: false }) tabInstance: TabComponent;@ViewChild('ejDialog', { static: false }) ejDialog: DialogComponent;@ViewChild('progress', { static: false }) public progress: ProgressBar;@ViewChild("childtemplate", { static: true }) public childtemplate: any;hyperclick(args) {console.log(this);console.log(args);}ngAfterViewInit() {this.childtemplate.elementRef.nativeElement._viewContainerRef = this.viewContainerRef;}getParentGridData() {this.chartSvc.getAllSprints(this.loginUser.syndicateDetailId, this.projectId).subscribe((data: any) => {this.childdata = [];if (data && data.sprint) {data.sprint.forEach((item) => {item.childs.forEach((child) => {child.queryId = child.parentSprintId;this.childdata.push(child);});});this.aSprints = data.sprint;this.childGridDataBind();}});}childGridDataBind() {this.childGrid = {dataSource: this.childdata,// id: "DestGrid",queryString: "queryId",allowPaging: false,pageSettings: { pageSize: 6, pageCount: 5 },load: function() {this.registeredTemplate = {}; //set registertemplate value as empty in load event},columns: [// {// field: "parentItemId",// headerText: "ID",// isPrimaryKey: true,// width: 120// },{field: "itemNumber",headerText: "ID",isPrimaryKey: false,width: 120},{field: "itemName",headerText: "Name",textAlign: "left",template:this.childtemplate,// "",// "",width: 120},{field: "nodeType",headerText: "Node Type",textAlign: "left",width: 120},{field: "itemSTartDate",headerText: "Start Date",textAlign: "left",width: 120},{field: "itemEndDate",headerText: "End Date",textAlign: "left",width: 120}]};}
|
[app.component.html]
// render the Grid using ng if condition
<ejs-grid *ngIf="childdatasuccess" #grid id="Grid" [dataSource]="parentData" [childGrid]="childGrid"
[allowPaging]="true">
<e-columns>
<e-column field="EmployeeID" headerText="Employee ID" width="120" textAlign="Right"></e-column>
<e-column field="FirstName" headerText="Name" width="140"></e-column>
<e-column field="Title" headerText="Title" width="170"></e-column>
</e-columns>
</ejs-grid>
// childTemplate1
<ng-template #childtemplate1 let-data>
<div>
<a rel='nofollow' href="https://ej2.syncfusion.com/documentation/grid/" (click)="hyperclick($event)">{{data.ShipCity}}</a>
</div>
</ng-template>
// childTemplate2
<ng-template #childtemplate2 let-data>
<div>
<a rel='nofollow' href="https://ej2.syncfusion.com/documentation/grid/" (click)="hyperclick($event)">{{data.ShipName}}</a>
</div>
</ng-template>
[app.component.ts]
export class AppComponent {
----
@ViewChild("grid", { static: true })
public grid: GridComponent;
@ViewChild("childtemplate1", { static: true })
public childtemplate1: any;
@ViewChild("childtemplate2", { static: true })
public childtemplate2: any;
public childdatasuccess: boolean = false;
----
ngOnInit(): void {
this.parentData = employeeData;
this.getParentGridData(); // call this method to get the data for child Grid
}
getParentGridData() {
let state = { skip: 0, take: 100 };
// execute the service
this.service.getData(state).subscribe((data: any) => {
this.childdata = [];
// get the child Grid data
this.childdata = data.result;
this.childGridDataBind(); // this method called to create the child Grid
});
}
ngAfterViewInit() {
this.childtemplate1.elementRef.nativeElement._viewContainerRef = this.viewContainerRef;
this.childtemplate2.elementRef.nativeElement._viewContainerRef = this.viewContainerRef;
}
hyperclick(args) {
console.log(this);
console.log(args);
}
childGridDataBind() {
// define the child Grid
this.childGrid = {
dataSource: orderDatas,
queryString: "EmployeeID",
allowPaging: true,
pageSettings: { pageSize: 6, pageCount: 5 },
load: function() {
this.registeredTemplate = {}; //set registertemplate value as empty in load event
},
columns: [
{
field: "OrderID",
headerText: "Order ID",
textAlign: "Right",
width: 120
},
{
field: "ShipCity",
headerText: "Ship City",
template: this.childtemplate1,
width: 120
},
{
field: "ShipName",
headerText: "Ship Name",
template: this.childtemplate2,
width: 150
}
]
};
this.childdatasuccess = true; // make the ngIf condition as true after defining the childGrid… now the Grid will be rendered
}
}
|
<e-column field="shippingDetails" headerText="Shipping">
<ng-template #template let-data>
<app-shipping-detail [details]="data.shippingDetails">app-shipping-detail>
ng-template>
e-column>