question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: Demo example for TemplateRef cellTemplate use

See original GitHub issue

I’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 cellTemplates 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:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
eddy-geekcommented, Mar 30, 2017

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 😃

3reactions
eddy-geekcommented, Mar 28, 2017

Note that something especially tricky for beginners is the need to reapply the TemplateRef in ngOnInit as it doesn’t exist before.

Read more comments on GitHub >

github_iconTop 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 >
Bountysource
feat : Demo example for TemplateRef cellTemplate use.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found