Skip to content

Commit 6927a8b

Browse files
andrewseguinmmalerba
authored andcommitted
docs(material/table): create example with columns defined dynamically (#22811)
* docs(material/table): create example with columns defined dynamically * docs(material/table): lint (cherry picked from commit 97e4836)
1 parent 74026b6 commit 6927a8b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/components-examples/material/table/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ import {TableHarnessExample} from './table-harness/table-harness-example';
4646
import {TableWithRipplesExample} from './table-with-ripples/table-with-ripples-example';
4747
import {TableColumnStylingExample} from './table-column-styling/table-column-styling-example';
4848
import {TableRowBindingExample} from './table-row-binding/table-row-binding-example';
49+
import {
50+
TableGeneratedColumnsExample
51+
} from './table-generated-columns/table-generated-columns-example';
4952

5053
export {
5154
TableBasicExample, TableFlexBasicExample,
@@ -62,6 +65,7 @@ export {
6265
TableReorderableExample, TableRecycleRowsExample,
6366
TableHarnessExample, TableColumnStylingExample,
6467
TableRowBindingExample, TableWithRipplesExample,
68+
TableGeneratedColumnsExample,
6569
};
6670

6771
const EXAMPLES = [
@@ -79,6 +83,7 @@ const EXAMPLES = [
7983
TableReorderableExample, TableRecycleRowsExample,
8084
TableHarnessExample, TableColumnStylingExample,
8185
TableRowBindingExample, TableWithRipplesExample,
86+
TableGeneratedColumnsExample,
8287
];
8388

8489
@NgModule({
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.demo-table {
2+
width: 100%;
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8 demo-table">
2+
<ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
3+
<mat-header-cell *cdkHeaderCellDef>
4+
{{column.header}}
5+
</mat-header-cell>
6+
<mat-cell *cdkCellDef="let row">
7+
{{column.cell(row)}}
8+
</mat-cell>
9+
</ng-container>
10+
11+
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
12+
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
13+
</table>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {Component} from '@angular/core';
2+
3+
export interface PeriodicElement {
4+
name: string;
5+
position: number;
6+
weight: number;
7+
symbol: string;
8+
}
9+
10+
const ELEMENT_DATA: PeriodicElement[] = [
11+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
12+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
13+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
14+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
15+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
16+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
17+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
18+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
19+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
20+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
21+
];
22+
23+
/**
24+
* @title Table with columns defined using ngFor instead of statically written in the template.
25+
*/
26+
@Component({
27+
selector: 'table-generated-columns-example',
28+
styleUrls: ['table-generated-columns-example.css'],
29+
templateUrl: 'table-generated-columns-example.html',
30+
})
31+
export class TableGeneratedColumnsExample {
32+
columns = [
33+
{
34+
columnDef: 'position',
35+
header: 'No.',
36+
cell: (element: PeriodicElement) => `${element.position}`
37+
},
38+
{
39+
columnDef: 'name',
40+
header: 'Name',
41+
cell: (element: PeriodicElement) => `${element.name}`
42+
},
43+
{
44+
columnDef: 'weight',
45+
header: 'Weight',
46+
cell: (element: PeriodicElement) => `${element.weight}`
47+
},
48+
{
49+
columnDef: 'symbol',
50+
header: 'Symbol',
51+
cell: (element: PeriodicElement) => `${element.symbol}`
52+
}
53+
];
54+
dataSource = ELEMENT_DATA;
55+
displayedColumns = this.columns.map(c => c.columnDef);
56+
}

0 commit comments

Comments
 (0)