|
| 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