1- The ` <cdk-table> ` is an unopinionated, customizable data-table with a fully-templated API, dynamic
1+ The ` CdkTable ` is an unopinionated, customizable data-table with a fully-templated API, dynamic
22columns, and an accessible DOM structure. This component acts as the core upon which anyone can
33build their own tailored data-table experience.
44
@@ -7,7 +7,7 @@ built. Because it enforces no opinions on these matters, developers have full co
77interaction patterns associated with the table.
88
99For a Material Design styled table, see the
10- [ documentation for ` <mat-table> ` ] ( https://material.angular.io/components/table ) which builds on
10+ [ documentation for ` MatTable ` ] ( https://material.angular.io/components/table ) which builds on
1111top of the CDK data-table.
1212
1313<!-- example(cdk-table-basic) -->
@@ -23,8 +23,8 @@ the column a name. Each column definition then further defines both a header-cel
2323
2424``` html
2525<ng-container cdkColumnDef =" username" >
26- <cdk-header-cell *cdkHeaderCellDef > User name </cdk-header-cell >
27- <cdk-cell *cdkCellDef =" let row" > {{row.a}} </cdk-cell >
26+ <th cdk-header-cell *cdkHeaderCellDef > User name </th >
27+ <td cdk-cell *cdkCellDef =" let row" > {{row.a}} </td >
2828</ng-container >
2929```
3030
3838The next step is to define the table's header-row (` cdkHeaderRowDef ` ) and data-row (` cdkRowDef ` ).
3939
4040``` html
41- <cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></cdk-header-row >
42- <cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></cdk-row >
41+ <tr cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></tr >
42+ <tr cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></tr >
4343```
4444
4545These row templates accept the specific columns to be rendered via the name given to the
@@ -53,58 +53,58 @@ above.
5353##### Example: table with three columns
5454
5555``` html
56- <cdk-table [dataSource] =" dataSource" >
56+ <table cdk-table [dataSource] =" dataSource" >
5757 <!-- User name Definition -->
5858 <ng-container cdkColumnDef =" username" >
59- <cdk-header-cell *cdkHeaderCellDef > User name </cdk-header-cell >
60- <cdk-cell *cdkCellDef =" let row" > {{row.username}} </cdk-cell >
59+ <th cdk-header-cell *cdkHeaderCellDef > User name </th >
60+ <td cdk-cell *cdkCellDef =" let row" > {{row.username}} </td >
6161 </ng-container >
6262
6363 <!-- Age Definition -->
6464 <ng-container cdkColumnDef =" age" >
65- <cdk-header-cell *cdkHeaderCellDef > Age </cdk-header-cell >
66- <cdk-cell *cdkCellDef =" let row" > {{row.age}} </cdk-cell >
65+ <th cdk-header-cell *cdkHeaderCellDef > Age </th >
66+ <td cdk-cell *cdkCellDef =" let row" > {{row.age}} </td >
6767 </ng-container >
6868
6969 <!-- Title Definition -->
7070 <ng-container cdkColumnDef =" title" >
71- <cdk-header-cell *cdkHeaderCellDef > Title </cdk-header-cell >
72- <cdk-cell *cdkCellDef =" let row" > {{row.title}} </cdk-cell >
71+ <th cdk-header-cell *cdkHeaderCellDef > Title </th >
72+ <td cdk-cell *cdkCellDef =" let row" > {{row.title}} </td >
7373 </ng-container >
7474
7575 <!-- Header and Row Declarations -->
76- <cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></cdk-header-row >
77- <cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></cdk-row >
78- </cdk- table >
76+ <tr cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></tr >
77+ <tr cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></tr >
78+ </table >
7979```
8080
8181The columns given on the row determine which cells are rendered and in which order. Thus, the
8282columns can be set via binding to support dynamically changing the columns shown at run-time.
8383
8484``` html
85- <cdk-row *cdkRowDef =" let row; columns: myDisplayedColumns" ></cdk-row >
85+ <tr cdk-row *cdkRowDef =" let row; columns: myDisplayedColumns" ></tr >
8686```
8787
8888It is not required to display all the columns that are defined within the template,
8989nor use the same ordering. For example, to display the table with only ` age `
9090and ` username ` and in that order, then the row and header definitions would be written as:
9191
9292``` html
93- <cdk-row *cdkRowDef =" let row; columns: ['age', 'username']" ></cdk-row >
93+ <tr cdk-row *cdkRowDef =" let row; columns: ['age', 'username']" ></tr >
9494```
9595
9696Event and property bindings can be added directly to the row element.
9797
9898##### Example: table with event and class binding
9999``` html
100- <cdk-header-row *cdkHeaderRowDef =" ['age', 'username']"
101- (click) =" handleHeaderRowClick(row)" >
102- </cdk-header-row >
103-
104- <cdk-row *cdkRowDef =" let row; columns: ['age', 'username']"
105- [class.can-vote] =" row.age >= 18"
106- (click) =" handleRowClick(row)" >
107- </cdk-row >
100+ <tr cdk-header-row *cdkHeaderRowDef =" ['age', 'username']"
101+ (click) =" handleHeaderRowClick(row)" >
102+ </tr >
103+
104+ <tr cdk-row *cdkRowDef =" let row; columns: ['age', 'username']"
105+ [class.can-vote] =" row.age >= 18"
106+ (click) =" handleRowClick(row)" >
107+ </tr >
108108```
109109
110110##### Styling columns
@@ -136,5 +136,45 @@ To improve performance, a `trackBy` function can be provided to the table simila
136136table how to uniquely identify rows to track how the data changes with each update.
137137
138138``` html
139- <cdk-table [dataSource] =" dataSource" [trackBy] =" myTrackById" >
139+ <table cdk-table [dataSource] =" dataSource" [trackBy] =" myTrackById" >
140+ ```
141+
142+ ### Alternate HTML to using native table
143+
144+ The CDK table does not require that you use a native HTML table. If you want to have full control
145+ over the style of the table, it may be easier to follow an alternative template approach that does
146+ not use the native table element tags.
147+
148+ This alternative approach replaces the native table element tags with the CDK table directive
149+ selectors. For example, ` <table cdk-table> ` becomes ` <cdk-table> ` ; ` <tr cdk-row ` > becomes
150+ ` <cdk-row> ` . The following shows a previous example using this alternative template:
151+
152+ ``` html
153+ <cdk-table [dataSource] =" dataSource" >
154+ <!-- User name Definition -->
155+ <ng-container cdkColumnDef =" username" >
156+ <cdk-header-cell *cdkHeaderCellDef > User name </cdk-header-cell >
157+ <cdk-cell *cdkCellDef =" let row" > {{row.username}} </cdk-cell >
158+ </ng-container >
159+
160+ <!-- Age Definition -->
161+ <ng-container cdkColumnDef =" age" >
162+ <cdk-header-cell *cdkHeaderCellDef > Age </cdk-header-cell >
163+ <cdk-cell *cdkCellDef =" let row" > {{row.age}} </cdk-cell >
164+ </ng-container >
165+
166+ <!-- Title Definition -->
167+ <ng-container cdkColumnDef =" title" >
168+ <cdk-header-cell *cdkHeaderCellDef > Title </cdk-header-cell >
169+ <cdk-cell *cdkCellDef =" let row" > {{row.title}} </cdk-cell >
170+ </ng-container >
171+
172+ <!-- Header and Row Declarations -->
173+ <cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></cdk-header-row >
174+ <cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></cdk-row >
175+ </cdk-table >
140176```
177+
178+ For an example of how to render the structure as a table, see the
179+ [ documentation for ` <mat-table> ` ] ( https://material.angular.io/components/table ) which includes
180+ the style support for this approach.
0 commit comments