Skip to content

Commit 133c5e8

Browse files
HermanWKeurismmalerba
authored andcommitted
docs(material/table): table multiple row example #27621 (#29421)
* docs(material/table): table multiple row example Added an example of how to use the multiTemplateDataRows template to the docs. Fixes #27621 * ci: fix formatting issues --------- Co-authored-by: Miles Malerba <[email protected]> (cherry picked from commit 4a86a65)
1 parent c79ec26 commit 133c5e8

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {TableFilteringExample} from './table-filtering/table-filtering-example';
66
export {TableFooterRowExample} from './table-footer-row/table-footer-row-example';
77
export {TableHttpExample} from './table-http/table-http-example';
88
export {TableMultipleHeaderFooterExample} from './table-multiple-header-footer/table-multiple-header-footer-example';
9+
export {TableMultipleRowTemplateExample} from './table-multiple-row-template/table-multiple-row-template-example';
910
export {TableOverviewExample} from './table-overview/table-overview-example';
1011
export {TablePaginationExample} from './table-pagination/table-pagination-example';
1112
export {TableRowContextExample} from './table-row-context/table-row-context-example';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
table {
2+
width: 100%;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<div class="mat-elevation-z8">
2+
<table mat-table [dataSource]="dataSource" multiTemplateDataRows>
3+
<!-- Position Column -->
4+
<ng-container matColumnDef="position">
5+
<th mat-header-cell *matHeaderCellDef>No.</th>
6+
<td mat-cell *matCellDef="let element">{{element.position}}</td>
7+
</ng-container>
8+
9+
<!-- Name Column -->
10+
<ng-container matColumnDef="name">
11+
<th mat-header-cell *matHeaderCellDef>Name</th>
12+
<td mat-cell *matCellDef="let element">{{element.name}}</td>
13+
</ng-container>
14+
15+
<!-- Weight Column -->
16+
<ng-container matColumnDef="weight">
17+
<th mat-header-cell *matHeaderCellDef>Weight</th>
18+
<td mat-cell *matCellDef="let element">{{element.weight}}</td>
19+
</ng-container>
20+
21+
<!-- Symbol Column -->
22+
<ng-container matColumnDef="symbol">
23+
<th mat-header-cell *matHeaderCellDef>Symbol</th>
24+
<td mat-cell *matCellDef="let element">{{element.symbol}}</td>
25+
</ng-container>
26+
27+
<!-- Secondary Column -->
28+
<ng-container matColumnDef="secondary">
29+
<td mat-cell [attr.colspan]="displayedColumns.length" *matCellDef="let element">
30+
Secondary row for the element {{element.name}}
31+
</td>
32+
</ng-container>
33+
34+
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
35+
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
36+
<tr mat-row *matRowDef="let row; columns: ['secondary'];"></tr>
37+
</table>
38+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {Component} from '@angular/core';
2+
import {MatTableDataSource, MatTableModule} from '@angular/material/table';
3+
4+
/**
5+
* @title Table with multiple row template
6+
*/
7+
@Component({
8+
selector: 'table-multiple-row-template-example',
9+
styleUrls: ['table-multiple-row-template-example.css'],
10+
templateUrl: 'table-multiple-row-template-example.html',
11+
standalone: true,
12+
imports: [MatTableModule],
13+
})
14+
export class TableMultipleRowTemplateExample {
15+
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
16+
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
17+
}
18+
19+
export interface PeriodicElement {
20+
name: string;
21+
position: number;
22+
weight: number;
23+
symbol: string;
24+
}
25+
26+
const ELEMENT_DATA: PeriodicElement[] = [
27+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
28+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
29+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
30+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
31+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
32+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
33+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
34+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
35+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
36+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
37+
{position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
38+
{position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
39+
{position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
40+
{position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
41+
{position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
42+
{position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
43+
{position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
44+
{position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
45+
{position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
46+
{position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
47+
];

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ <h3>Table with sticky footer</h3>
7070
<h3>Table with sticky header</h3>
7171
<table-sticky-header-example></table-sticky-header-example>
7272

73+
<h3>Table with multiple rows per data item</h3>
74+
<table-multiple-row-template-example></table-multiple-row-template-example>
75+
7376
<h3>Table with mat-text-column</h3>
7477
<table-text-column-example></table-text-column-example>
7578

src/dev-app/table/table-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
TableHarnessExample,
2828
TableHttpExample,
2929
TableMultipleHeaderFooterExample,
30+
TableMultipleRowTemplateExample,
3031
TableOverviewExample,
3132
TablePaginationExample,
3233
TableRecycleRowsExample,
@@ -63,6 +64,7 @@ import {ChangeDetectionStrategy, Component} from '@angular/core';
6364
TableFooterRowExample,
6465
TableHttpExample,
6566
TableMultipleHeaderFooterExample,
67+
TableMultipleRowTemplateExample,
6668
TableOverviewExample,
6769
TablePaginationExample,
6870
TableRowContextExample,

src/material/table/table.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ to resolve this.
374374

375375
When using the `multiTemplateDataRows` directive to support multiple rows for each data object, the context of `*matRowDef` is the same except that the `index` value is replaced by `dataIndex` and `renderIndex`.
376376

377+
<!--- example(table-multiple-row-template) -->
378+
377379
### Accessibility
378380

379381
By default, `MatTable` applies `role="table"`, assuming the table's contains primarily static

0 commit comments

Comments
 (0)