Skip to content

Commit 020ed61

Browse files
andrewseguinjosephperrott
authored andcommitted
docs(table): add selection example (#8161)
* Add comment * newline * triple equals
1 parent 2667c01 commit 020ed61

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.example-container {
2+
display: flex;
3+
flex-direction: column;
4+
max-height: 500px;
5+
min-width: 300px;
6+
}
7+
8+
.mat-table {
9+
overflow: auto;
10+
max-height: 500px;
11+
}
12+
13+
.mat-column-select {
14+
overflow: initial;
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<div class="example-container mat-elevation-z8">
2+
<mat-table #table [dataSource]="dataSource">
3+
4+
<!-- Checkbox Column -->
5+
<ng-container matColumnDef="select">
6+
<mat-header-cell *matHeaderCellDef>
7+
<mat-checkbox (change)="$event ? masterToggle() : null"
8+
[checked]="selection.hasValue() && isAllSelected()"
9+
[indeterminate]="selection.hasValue() && !isAllSelected()">
10+
</mat-checkbox>
11+
</mat-header-cell>
12+
<mat-cell *matCellDef="let row">
13+
<mat-checkbox (click)="$event.stopPropagation()"
14+
(change)="$event ? selection.toggle(row) : null"
15+
[checked]="selection.isSelected(row)">
16+
</mat-checkbox>
17+
</mat-cell>
18+
</ng-container>
19+
20+
<!-- Position Column -->
21+
<ng-container matColumnDef="position">
22+
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
23+
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
24+
</ng-container>
25+
26+
<!-- Name Column -->
27+
<ng-container matColumnDef="name">
28+
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
29+
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
30+
</ng-container>
31+
32+
<!-- Weight Column -->
33+
<ng-container matColumnDef="weight">
34+
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
35+
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
36+
</ng-container>
37+
38+
<!-- Color Column -->
39+
<ng-container matColumnDef="symbol">
40+
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
41+
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
42+
</ng-container>
43+
44+
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
45+
<mat-row *matRowDef="let row; columns: displayedColumns;"
46+
(click)="selection.toggle(row)">
47+
</mat-row>
48+
</mat-table>
49+
</div>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {Component} from '@angular/core';
2+
import {MatTableDataSource} from '@angular/material';
3+
import {SelectionModel} from '@angular/cdk/collections';
4+
5+
/**
6+
* @title Table with selection
7+
*/
8+
@Component({
9+
selector: 'table-selection-example',
10+
styleUrls: ['table-selection-example.css'],
11+
templateUrl: 'table-selection-example.html',
12+
})
13+
export class TableSelectionExample {
14+
displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
15+
dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
16+
selection = new SelectionModel<Element>(true, []);
17+
18+
/** Whether the number of selected elements matches the total number of rows. */
19+
isAllSelected() {
20+
const numSelected = this.selection.selected.length;
21+
const numRows = this.dataSource.data.length;
22+
return numSelected === numRows;
23+
}
24+
25+
/** Selects all rows if they are not all selected; otherwise clear selection. */
26+
masterToggle() {
27+
this.isAllSelected() ?
28+
this.selection.clear() :
29+
this.dataSource.data.forEach(row => this.selection.select(row));
30+
}
31+
}
32+
33+
export interface Element {
34+
name: string;
35+
position: number;
36+
weight: number;
37+
symbol: string;
38+
}
39+
40+
const ELEMENT_DATA: Element[] = [
41+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
42+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
43+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
44+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
45+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
46+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
47+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
48+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
49+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
50+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
51+
{position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
52+
{position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
53+
{position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
54+
{position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
55+
{position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
56+
{position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
57+
{position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
58+
{position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
59+
{position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
60+
{position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
61+
];

0 commit comments

Comments
 (0)