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.

[Table] Add example with dynamic columns

See original GitHub issue

Bug, feature request, or proposal:

Proposal

What is the expected behavior?

It’d be nice to have example(s) on how to use md-table with dynamic columns.

What is the current behavior?

Currently all the examples are with hard coded columns, like this:

<!-- ID Column -->
<ng-container cdkColumnDef="userId">
  <md-header-cell *cdkHeaderCellDef md-sort-header> ID </md-header-cell>
  <md-cell *cdkCellDef="let row"> {{row.id}} </md-cell>
</ng-container>

<!-- Progress Column -->
<ng-container cdkColumnDef="progress">
  <md-header-cell *cdkHeaderCellDef md-sort-header> Progress </md-header-cell>
  <md-cell *cdkCellDef="let row"> {{row.progress}}% </md-cell>
</ng-container>

@andrewseguin

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:49
  • Comments:59 (11 by maintainers)

github_iconTop GitHub Comments

164reactions
willshowellcommented, Oct 5, 2017

For those interested, here was my approach

https://plnkr.co/edit/UntMipJO7lQVFCCSq3sZ?p=preview

<!-- Generic column definition -->
<ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
  <md-header-cell *cdkHeaderCellDef>{{ column.header }}</md-header-cell>
  <md-cell *cdkCellDef="let row">{{ column.cell(row) }}</md-cell>
</ng-container>
/** Table columns */
columns = [
  { columnDef: 'userId',    header: 'ID',       cell: (row: UserData) => `${row.id}`        },
  { columnDef: 'userName',  header: 'Name',     cell: (row: UserData) => `${row.name}`      },
  { columnDef: 'progress',  header: 'Progress', cell: (row: UserData) => `${row.progress}%` }
];

/** Column definitions in order */
displayedColumns = this.columns.map(x => x.columnDef);

EDIT: Here is a (hopefully) evergreen stackblitz with the exact same approach

https://stackblitz.com/edit/material2-beta11-vmwjpe

17reactions
DaDavecommented, May 22, 2018

This should work: <div *ngFor="let displayedColumn of displayedColumns; let columnIndex = index"> <ng-container *ngIf="displayedColumn !='actions'" matColumnDef="{{displayedColumn}}"> <mat-header-cell *matHeaderCellDef mat-sort-header>{{displayedColumn}}</mat-header-cell> <mat-cell *matCellDef="let element "> {{element[displayedColumn]}}</mat-cell> </ng-container> </div> <ng-container matColumnDef="actions"> <mat-header-cell *matHeaderCellDef >Actions</mat-header-cell> <mat-cell *matCellDef="let element "> <button mat-raised-button> Edit </button> </mat-cell> </ng-container>

Read more comments on GitHub >

github_iconTop Results From Across the Web

CREATE TABLE with Dynamic Column - sql - Stack Overflow
Another option is to create a table with NULL columns. To do this in SQL, you can do the following: CREATE TABLE tblPerson...
Read more >
Dynamic Columns - MariaDB Knowledge Base
Dynamic columns allow one to store different sets of columns for each row in a table. It works by storing a set of...
Read more >
Table Records with dynamic columns | OutSystems
Hi guys, I need to have table records where the columns are dynamic and I have no more ideas on how to do...
Read more >
How to Create Dynamic Tables from Editable Columns in ...
Call the function on component mount to get the inventory data to populate the table. To dynamically create the rows, iterate through the...
Read more >
Table dynamically changing the columns displayed - StackBlitz
Table dynamically changing the columns displayed. ... Add column </button>. <button mat-raised-button (click) ... <tr mat-row *matRowDef="let row; columns:.
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