Skip to content

Commit b17ed9d

Browse files
authored
fix(material-experimental/mdc-table): flex tables should stretch cells (#23259)
1 parent 53d2d4b commit b17ed9d

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
import {
5656
TableGeneratedColumnsExample
5757
} from './table-generated-columns/table-generated-columns-example';
58+
import {TableFlexLargeRowExample} from './table-flex-large-row/table-flex-large-row-example';
5859

5960
export {
6061
TableBasicExample,
@@ -65,6 +66,7 @@ export {
6566
TableExpandableRowsExample,
6667
TableFilteringExample,
6768
TableFlexBasicExample,
69+
TableFlexLargeRowExample,
6870
TableFooterRowExample,
6971
TableGeneratedColumnsExample,
7072
TableHarnessExample,
@@ -99,6 +101,7 @@ const EXAMPLES = [
99101
TableExpandableRowsExample,
100102
TableFilteringExample,
101103
TableFlexBasicExample,
104+
TableFlexLargeRowExample,
102105
TableFooterRowExample,
103106
TableGeneratedColumnsExample,
104107
TableHarnessExample,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.mat-mdc-table {
2+
width: 100%;
3+
max-height: 500px;
4+
overflow: auto;
5+
}
6+
7+
.mat-column-name {
8+
height: 100px;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
2+
<!-- Position Column -->
3+
<ng-container matColumnDef="position">
4+
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
5+
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
6+
</ng-container>
7+
8+
<!-- Name Column -->
9+
<ng-container matColumnDef="name">
10+
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
11+
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
12+
</ng-container>
13+
14+
<!-- Weight Column -->
15+
<ng-container matColumnDef="weight">
16+
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
17+
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
18+
</ng-container>
19+
20+
<!-- Symbol Column -->
21+
<ng-container matColumnDef="symbol">
22+
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
23+
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
24+
</ng-container>
25+
26+
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
27+
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
28+
</mat-table>
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 Flex table where one column's cells has a greater height than others.
25+
*/
26+
@Component({
27+
selector: 'table-flex-large-row-example',
28+
styleUrls: ['table-flex-large-row-example.css'],
29+
templateUrl: 'table-flex-large-row-example.html',
30+
})
31+
export class TableFlexLargeRowExample {
32+
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
33+
dataSource = ELEMENT_DATA;
34+
}

src/dev-app/mdc-table/mdc-table-demo.html

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ <h3>Table basic with recycled rows</h3>
77
<h3>Table basic flex</h3>
88
<table-flex-basic-example></table-flex-basic-example>
99

10+
<h3>Table flex with large row</h3>
11+
<table-flex-large-row-example></table-flex-large-row-example>
12+
1013
<h3>Table dynamic columns</h3>
1114
<table-dynamic-columns-example></table-dynamic-columns-example>
1215

src/material-experimental/mdc-table/table.scss

+17
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,20 @@ mat-row.mat-mdc-row, mat-header-row.mat-mdc-header-row, mat-footer-row.mat-mdc-f
4747
.mat-mdc-table .mat-mdc-footer-row:hover {
4848
background-color: inherit;
4949
}
50+
51+
// Flex rows should not set a definite height, but instead stretch to the height of their
52+
// children. Otherwise, the cells grow larger than the row and the layout breaks.
53+
.mat-mdc-table mat-header-row.mat-mdc-header-row,
54+
.mat-mdc-table mat-row.mat-mdc-row,
55+
.mat-mdc-table mat-footer-row.mat-mdc-footer-cell {
56+
height: unset;
57+
}
58+
59+
// Flex cells should stretch to the height of their parent. This was okay for the legacy
60+
// table since the cells were centered and the borders displayed on the rows, but the MDC
61+
// version displays borders on the cells and do not correctly line up with the row bottom.
62+
mat-header-cell.mat-mdc-header-cell,
63+
mat-cell.mat-mdc-cell,
64+
mat-footer-cell.mat-mdc-footer-cell {
65+
align-self: stretch;
66+
}

0 commit comments

Comments
 (0)