feat: Demo example for TemplateRef cellTemplate use
See original GitHub issueI’m submitting a … (check one with “x”)
[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here
Current behavior
Doc is a bit elusive on how to use json column definitions where cellTemplate
s are involved
Expected behavior Here is what the demo code could look like (this is adapted from the first demo)
import { Component, ViewChild, TemplateRef, OnInit } from '@angular/core';
@Component({
selector: 'template-templateref-demo',
template: `
<div>
<h3>
TemplateRef cellTemplate
<small>
<a href="https://github.com/swimlane/ngx-datatable/blob/master/demo/template/templateref.ts" target="_blank">
Source
</a>
</small>
</h3>
<ngx-datatable
class="material"
[rows]="rows"
[columns]="columns"
[columnMode]="'force'"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="'auto'">
</ngx-datatable>
<ng-template #sampleT let-row="row" let-value="value" let-i="index">
templated {{value}}
</ng-template>
</div>
`
})
export class BasicAutoComponent implements OnInit {
@ViewChild('sampleT') public sampleT: TemplateRef<any>;
rows = [];
columns = [
{
prop: 'name',
cellTemplate: this.sampleT
},
{ name: 'Gender' },
{ name: 'Company' }
];
constructor() {
this.fetch((data) => {
this.rows = data;
});
}
ngOnInit() {
this.columns = [
{
prop: 'name',
cellTemplate: this.sampleT
},
{ name: 'Gender' },
{ name: 'Company' }
];
}
fetch(cb) {
const req = new XMLHttpRequest();
req.open('GET', `assets/data/company.json`);
req.onload = () => {
cb(JSON.parse(req.response));
};
req.send();
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Angular ngx-datatable cellTemplate not loading - Stack Overflow
The problem is that you're most probably using a nested template and thus angular is not able to get a reference.
Read more >Advanced Uses of Polymer Templates - Steven Skelton
To create a demo application, suppose that our cellTemplate is actually a template containing more templates which will be conditionally ...
Read more >TemplateRef - Angular
Represents an embedded template that can be used to instantiate embedded views. To instantiate embedded views based on a template, use the ViewContainerRef ......
Read more >Assign cell template in code code rather than in the view
I am using the columGroupProvider class given in the sample code, ... a reference to a template and then access it as type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well, the point is, the docs say column.cellTemplate accepts a
TemplateRef
, but don’t mention how/when to get one, though it’s a pretty common need when declaring your columns as json (e.g. to reuse the definition).Of course this issue, even when closed, might already be enough for someone searching for the solution 😃
Note that something especially tricky for beginners is the need to reapply the TemplateRef in ngOnInit as it doesn’t exist before.