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
2
2
columns, and an accessible DOM structure. This component acts as the core upon which anyone can
3
3
build their own tailored data-table experience.
4
4
@@ -7,7 +7,7 @@ built. Because it enforces no opinions on these matters, developers have full co
7
7
interaction patterns associated with the table.
8
8
9
9
For 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
11
11
top of the CDK data-table.
12
12
13
13
<!-- example(cdk-table-basic) -->
@@ -23,8 +23,8 @@ the column a name. Each column definition then further defines both a header-cel
23
23
24
24
``` html
25
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 >
26
+ <th cdk-header-cell *cdkHeaderCellDef > User name </th >
27
+ <td cdk-cell *cdkCellDef =" let row" > {{row.a}} </td >
28
28
</ng-container >
29
29
```
30
30
38
38
The next step is to define the table's header-row (` cdkHeaderRowDef ` ) and data-row (` cdkRowDef ` ).
39
39
40
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 >
41
+ <tr cdk-header-row *cdkHeaderRowDef =" ['username', 'age', 'title']" ></tr >
42
+ <tr cdk-row *cdkRowDef =" let row; columns: ['username', 'age', 'title']" ></tr >
43
43
```
44
44
45
45
These row templates accept the specific columns to be rendered via the name given to the
@@ -53,58 +53,58 @@ above.
53
53
##### Example: table with three columns
54
54
55
55
``` html
56
- <cdk-table [dataSource] =" dataSource" >
56
+ <table cdk-table [dataSource] =" dataSource" >
57
57
<!-- User name Definition -->
58
58
<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 >
61
61
</ng-container >
62
62
63
63
<!-- Age Definition -->
64
64
<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 >
67
67
</ng-container >
68
68
69
69
<!-- Title Definition -->
70
70
<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 >
73
73
</ng-container >
74
74
75
75
<!-- 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 >
79
79
```
80
80
81
81
The columns given on the row determine which cells are rendered and in which order. Thus, the
82
82
columns can be set via binding to support dynamically changing the columns shown at run-time.
83
83
84
84
``` html
85
- <cdk-row *cdkRowDef =" let row; columns: myDisplayedColumns" ></cdk-row >
85
+ <tr cdk-row *cdkRowDef =" let row; columns: myDisplayedColumns" ></tr >
86
86
```
87
87
88
88
It is not required to display all the columns that are defined within the template,
89
89
nor use the same ordering. For example, to display the table with only ` age `
90
90
and ` username ` and in that order, then the row and header definitions would be written as:
91
91
92
92
``` html
93
- <cdk-row *cdkRowDef =" let row; columns: ['age', 'username']" ></cdk-row >
93
+ <tr cdk-row *cdkRowDef =" let row; columns: ['age', 'username']" ></tr >
94
94
```
95
95
96
96
Event and property bindings can be added directly to the row element.
97
97
98
98
##### Example: table with event and class binding
99
99
``` 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 >
108
108
```
109
109
110
110
##### Styling columns
@@ -136,5 +136,45 @@ To improve performance, a `trackBy` function can be provided to the table simila
136
136
table how to uniquely identify rows to track how the data changes with each update.
137
137
138
138
``` 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 >
140
176
```
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