Skip to content

Commit b124325

Browse files
authored
docs(material/table): add docs on row/column styles and bindings (#22778)
* docs(material/table): add docs on row/column styles and bindings * docs(material/table): fix CSS classname to match demo- * docs(material/table): review * docs(material/table): review2 * docs(material/table): use set
1 parent a48f736 commit b124325

File tree

8 files changed

+210
-2
lines changed

8 files changed

+210
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import {TableWrappedExample, WrapperTable} from './table-wrapped/table-wrapped-e
4242
import {TableReorderableExample} from './table-reorderable/table-reorderable-example';
4343
import {TableRecycleRowsExample} from './table-recycle-rows/table-recycle-rows-example';
4444
import {TableHarnessExample} from './table-harness/table-harness-example';
45+
import {TableColumnStylingExample} from './table-column-styling/table-column-styling-example';
46+
import {TableRowBindingExample} from './table-row-binding/table-row-binding-example';
4547

4648
export {
4749
TableBasicExample, TableFlexBasicExample,
@@ -56,7 +58,8 @@ export {
5658
TableTextColumnExample, TableTextColumnAdvancedExample,
5759
TableWrappedExample, WrapperTable,
5860
TableReorderableExample, TableRecycleRowsExample,
59-
TableHarnessExample,
61+
TableHarnessExample, TableColumnStylingExample,
62+
TableRowBindingExample
6063
};
6164

6265
const EXAMPLES = [
@@ -72,7 +75,8 @@ const EXAMPLES = [
7275
TableTextColumnExample, TableTextColumnAdvancedExample,
7376
TableWrappedExample, WrapperTable,
7477
TableReorderableExample, TableRecycleRowsExample,
75-
TableHarnessExample,
78+
TableHarnessExample, TableColumnStylingExample,
79+
TableRowBindingExample
7680
];
7781

7882
@NgModule({
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.demo-table {
2+
width: 100%;
3+
}
4+
5+
.mat-column-demo-position {
6+
width: 32px;
7+
border-right: 1px solid currentColor;
8+
padding-right: 24px;
9+
text-align: center;
10+
}
11+
12+
.mat-column-demo-name {
13+
padding-left: 16px;
14+
font-size: 20px;
15+
}
16+
17+
.mat-column-demo-weight {
18+
font-style: italic;
19+
}
20+
21+
.mat-column-demo-symbol {
22+
width: 32px;
23+
text-align: center;
24+
font-weight: bold;
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8 demo-table">
2+
<!-- Position Column -->
3+
<ng-container matColumnDef="demo-position">
4+
<th mat-header-cell *matHeaderCellDef> No. </th>
5+
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
6+
</ng-container>
7+
8+
<!-- Name Column -->
9+
<ng-container matColumnDef="demo-name">
10+
<th mat-header-cell *matHeaderCellDef> Name </th>
11+
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
12+
</ng-container>
13+
14+
<!-- Weight Column -->
15+
<ng-container matColumnDef="demo-weight">
16+
<th mat-header-cell *matHeaderCellDef> Weight </th>
17+
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
18+
</ng-container>
19+
20+
<!-- Symbol Column -->
21+
<ng-container matColumnDef="demo-symbol">
22+
<th mat-header-cell *matHeaderCellDef> Symbol </th>
23+
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
24+
</ng-container>
25+
26+
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
27+
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
28+
</table>
29+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 Styling columns using their auto-generated column names
25+
*/
26+
@Component({
27+
selector: 'table-column-styling-example',
28+
styleUrls: ['table-column-styling-example.css'],
29+
templateUrl: 'table-column-styling-example.html',
30+
})
31+
export class TableColumnStylingExample {
32+
displayedColumns: string[] = ['demo-position', 'demo-name', 'demo-weight', 'demo-symbol'];
33+
dataSource = ELEMENT_DATA;
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.demo-table {
2+
width: 100%;
3+
}
4+
5+
.mat-row .mat-cell {
6+
border-bottom: 1px solid transparent;
7+
border-top: 1px solid transparent;
8+
cursor: pointer;
9+
}
10+
11+
.mat-row:hover .mat-cell {
12+
border-color: currentColor;
13+
}
14+
15+
.demo-row-is-clicked {
16+
font-weight: bold;
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8 demo-table">
2+
<!-- Position Column -->
3+
<ng-container matColumnDef="position">
4+
<th mat-header-cell *matHeaderCellDef>No.</th>
5+
<td mat-cell *matCellDef="let element">{{element.position}}</td>
6+
</ng-container>
7+
8+
<!-- Name Column -->
9+
<ng-container matColumnDef="name">
10+
<th mat-header-cell *matHeaderCellDef>Name</th>
11+
<td mat-cell *matCellDef="let element">{{element.name}}</td>
12+
</ng-container>
13+
14+
<!-- Weight Column -->
15+
<ng-container matColumnDef="weight">
16+
<th mat-header-cell *matHeaderCellDef>Weight</th>
17+
<td mat-cell *matCellDef="let element">{{element.weight}}</td>
18+
</ng-container>
19+
20+
<!-- Symbol Column -->
21+
<ng-container matColumnDef="symbol">
22+
<th mat-header-cell *matHeaderCellDef>Symbol</th>
23+
<td mat-cell *matCellDef="let element">{{element.symbol}}</td>
24+
</ng-container>
25+
26+
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
27+
<tr
28+
mat-row
29+
(click)="clickedRows.add(row)"
30+
[class.demo-row-is-clicked]="clickedRows.has(row)"
31+
*matRowDef="let row; columns: displayedColumns;"
32+
></tr>
33+
</table>
34+
35+
<div>
36+
<h3>
37+
Click Log
38+
</h3>
39+
</div>
40+
41+
<div *ngIf="!clickedRows.size">
42+
Clicked rows will be logged here
43+
</div>
44+
45+
<ul>
46+
<li *ngFor="let clickedRow of clickedRows">
47+
Clicked on {{clickedRow.name}}
48+
</li>
49+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 Binding event handlers and properties to the table rows.
25+
*/
26+
@Component({
27+
selector: 'table-row-binding-example',
28+
styleUrls: ['table-row-binding-example.css'],
29+
templateUrl: 'table-row-binding-example.html',
30+
})
31+
export class TableRowBindingExample {
32+
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
33+
dataSource = ELEMENT_DATA;
34+
clickedRows = new Set<PeriodicElement>();
35+
}

src/material/table/table.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ functionality by extending a different base class, the `DataSource` base class c
124124
implemented instead (`MyCustomDataSource extends SomeOtherBaseClass implements DataSource`) to
125125
respect Typescript's restriction to only implement one base class.
126126

127+
### Styling Columns
128+
129+
Each table cell has an automatically generated class based on which column it appears in. The format for this
130+
generated class is `mat-column-NAME`. For example, cells in a column named "symbol" can be targeted with the
131+
selector `.mat-column-symbol`.
132+
133+
<!-- example(table-column-styling) -->
134+
135+
### Row Templates
136+
137+
Event handlers and property binding on the row templates will be applied to each row rendered by the table. For example,
138+
adding a `(click)` handler to the row template will cause each individual row to call the handler when clicked.
139+
140+
<!-- example(table-row-binding) -->
141+
127142
### Features
128143

129144
The `MatTable` is focused on a single responsibility: efficiently render rows of data in a

0 commit comments

Comments
 (0)