|
| 1 | +The `<cdk-table>` is an unopinionated, customizable data-table with a fully-templated API, dynamic |
| 2 | +columns, and an accessible DOM structure. This component acts as the core upon which anyone can |
| 3 | +build their own tailored data-table experience. |
| 4 | + |
| 5 | +The table provides a foundation upon which other features, such as sorting and pagination, can be |
| 6 | +built. Because it enforces no opinions on these matters, developers have full control over the |
| 7 | +interaction patterns associated with the table. |
| 8 | + |
| 9 | +For a Material Design styled table, see the documentation for `<md-table>` which builds on top of |
| 10 | +the CDK data-table. |
| 11 | + |
| 12 | +<!-- example(cdk-table-basic) --> |
| 13 | + |
| 14 | +### Using the CDK data-table |
| 15 | + |
| 16 | +#### Writing your table template |
| 17 | + |
| 18 | +The first step to writing the data-table template is to define the columns. |
| 19 | +A column definition is specified via an `<ng-container>` with the `cdkColumnDef` directive, giving |
| 20 | +column a name. Each column definition then further defines both a header-cell template |
| 21 | +(`cdkHeaderCellDef`) and a data-cell template (`cdkCellDef`). |
| 22 | + |
| 23 | + |
| 24 | +```html |
| 25 | + <ng-container cdkColumnDef="username"> |
| 26 | + <cdk-header-cell *cdkHeaderCellDef> User name </cdk-header-cell> |
| 27 | + <cdk-cell *cdkCellDef="let row"> {{row.a}} </cdk-cell> |
| 28 | + </ng-container> |
| 29 | +``` |
| 30 | + |
| 31 | +The set of columns defined represent the columns that are _available_ to be rendered. The specific |
| 32 | +columns rendered in a given row, and their order, are specified on the row (see below). |
| 33 | + |
| 34 | +Note that `cdkCellDef` exports the row context such that the row data can be referenced in the cell |
| 35 | +template. The directive also exports the same properties as `ngFor` (index, even, odd, first, |
| 36 | +last). |
| 37 | + |
| 38 | +The next step is to define the table's header-row (`cdkHeaderRowDef`) and data-row (`cdkRowDef`). |
| 39 | + |
| 40 | +```html |
| 41 | + <cdk-header-row *cdkHeaderRowDef="['username', 'age', 'title']"></cdk-header-row> |
| 42 | + <cdk-row *cdkRowDef="let row; columns: ['username', 'age', 'title']"></cdk-row> |
| 43 | +``` |
| 44 | + |
| 45 | +These row templates accept the specific columns to be rendered via the name given to the |
| 46 | +`cdkColumnDef`. |
| 47 | + |
| 48 | + |
| 49 | +The `cdkRowDef` also exports row context, which can be used for event and property |
| 50 | +bindings on the row element. Any content placed _inside_ of the header row or data row template |
| 51 | +will be ignored, as the rendered content of the row comes from the cell templates described |
| 52 | +above. |
| 53 | + |
| 54 | + |
| 55 | +##### Example: table with three columns |
| 56 | +```html |
| 57 | +<cdk-table [dataSource]="dataSource"> |
| 58 | + <!-- User name Definition --> |
| 59 | + <ng-container cdkColumnDef="username"> |
| 60 | + <cdk-header-cell *cdkHeaderCellDef> User name </cdk-header-cell> |
| 61 | + <cdk-cell *cdkCellDef="let row"> {{row.username}} </cdk-cell> |
| 62 | + </ng-container> |
| 63 | + |
| 64 | + <!-- Age Definition --> |
| 65 | + <ng-container cdkColumnDef="age"> |
| 66 | + <cdk-header-cell *cdkHeaderCellDef> Age </cdk-header-cell> |
| 67 | + <cdk-cell *cdkCellDef="let row"> {{row.age}} </cdk-cell> |
| 68 | + </ng-container> |
| 69 | + |
| 70 | + <!-- Title Definition --> |
| 71 | + <ng-container cdkColumnDef="title"> |
| 72 | + <cdk-header-cell *cdkHeaderCellDef> Title </cdk-header-cell> |
| 73 | + <cdk-cell *cdkCellDef="let row"> {{row.title}} </cdk-cell> |
| 74 | + </ng-container> |
| 75 | + |
| 76 | + <!-- Header and Row Declarations --> |
| 77 | + <cdk-header-row *cdkHeaderRowDef="['username', 'age', 'title']"></cdk-header-row> |
| 78 | + <cdk-row *cdkRowDef="let row; columns: ['username', 'age', 'title']"></cdk-row> |
| 79 | + </cdk-table> |
| 80 | +``` |
| 81 | + |
| 82 | +The columns given on the row determine which cells are rendered and in which order. Thus, the |
| 83 | +columns can be set via binding to support dynamically changing the columns shown at run-time. |
| 84 | + |
| 85 | + |
| 86 | +It is not required to display all the columns that are defined within the template, |
| 87 | +nor use the same ordering. For example, to display the table with only `age` |
| 88 | +and `username` and in that order, then the row and header definitions would be written as: |
| 89 | + |
| 90 | +```html |
| 91 | + <cdk-row *cdkRowDef="let row; columns: myDisplayedColumns"></cdk-row> |
| 92 | +``` |
| 93 | + |
| 94 | +Event and property bindings can be added directly to the row element. |
| 95 | + |
| 96 | +##### Example: table with event and class binding |
| 97 | +```html |
| 98 | + <cdk-header-row *cdkHeaderRowDef="['age', 'username']" |
| 99 | + (click)=”handleHeaderRowClick(row)”> |
| 100 | + </cdk-header-row> |
| 101 | + |
| 102 | + <cdk-row *cdkRowDef="let row; columns: ['age', 'username']" |
| 103 | + [class.can-vote]=”row.age >= 18” |
| 104 | + (click)=”handleRowClick(row)”> |
| 105 | + </cdk-row> |
| 106 | +``` |
| 107 | + |
| 108 | +#### Connecting the table to a data source |
| 109 | +Data is provided to the table through a `DataSource`. When the table receives a data source, |
| 110 | +it calls the DataSource's connect function which returns an observable that emits an array of data. |
| 111 | +Whenever the data source emits data to this stream, the table will update. |
| 112 | + |
| 113 | +Because the _data source_ provides this stream, it bears the responsibility of triggering table |
| 114 | +updates. This can be based on _anything_: websocket connections, user interaction, model updates, |
| 115 | +time-based intervals, etc. Most commonly, updates will be triggered by user interactions like |
| 116 | +sorting and pagination. |
| 117 | + |
| 118 | +##### `trackBy` |
| 119 | +To improve performance, a trackBy function can be provided to the table similar to Angular’s |
| 120 | +(`ngFor` trackBy)[trackBy]. This informs the table how to uniquely identify rows to track how the |
| 121 | +data changes with each update. |
| 122 | + |
| 123 | +```html |
| 124 | +<cdk-table [dataSource]="dataSource" [trackBy]="myTrackById"> |
| 125 | +``` |
| 126 | + |
| 127 | + |
| 128 | +[trackBy][https://angular.io/api/common/NgForOf#change-propagation] |
0 commit comments