|
| 1 | +import {DataSource} from '@angular/cdk/collections'; |
| 2 | +import {Component} from '@angular/core'; |
| 3 | +import {BehaviorSubject, Observable} from 'rxjs'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @title Basic use of `<cdk-table>` (uses display flex) |
| 7 | + */ |
| 8 | +@Component({ |
| 9 | + selector: 'cdk-table-basic-flex-example', |
| 10 | + styleUrls: ['cdk-table-basic-flex-example.css'], |
| 11 | + templateUrl: 'cdk-table-basic-flex-example.html', |
| 12 | +}) |
| 13 | +export class CdkTableBasicFlexExample { |
| 14 | + displayedColumns = ['position', 'name', 'weight', 'symbol']; |
| 15 | + dataSource = new ExampleDataSource(); |
| 16 | +} |
| 17 | + |
| 18 | +export interface PeriodicElement { |
| 19 | + name: string; |
| 20 | + position: number; |
| 21 | + weight: number; |
| 22 | + symbol: string; |
| 23 | +} |
| 24 | + |
| 25 | +const ELEMENT_DATA: PeriodicElement[] = [ |
| 26 | + {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'}, |
| 27 | + {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}, |
| 28 | + {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'}, |
| 29 | + {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}, |
| 30 | + {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'}, |
| 31 | + {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'}, |
| 32 | + {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'}, |
| 33 | + {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}, |
| 34 | + {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'}, |
| 35 | + {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'}, |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Data source to provide what data should be rendered in the table. Note that the data source |
| 40 | + * can retrieve its data in any way. In this case, the data source is provided a reference |
| 41 | + * to a common data base, ExampleDatabase. It is not the data source's responsibility to manage |
| 42 | + * the underlying data. Instead, it only needs to take the data and send the table exactly what |
| 43 | + * should be rendered. |
| 44 | + */ |
| 45 | +export class ExampleDataSource extends DataSource<PeriodicElement> { |
| 46 | + /** Stream of data that is provided to the table. */ |
| 47 | + data: BehaviorSubject<PeriodicElement[]> = new BehaviorSubject<PeriodicElement[]>(ELEMENT_DATA); |
| 48 | + |
| 49 | + /** Connect function called by the table to retrieve one stream containing the data to render. */ |
| 50 | + connect(): Observable<PeriodicElement[]> { |
| 51 | + return this.data; |
| 52 | + } |
| 53 | + |
| 54 | + disconnect() {} |
| 55 | +} |
0 commit comments