|
1 | 1 | import {Component} from '@angular/core';
|
2 | 2 | import {DataSource} from '@angular/cdk/table';
|
3 |
| -import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
4 | 3 | import {Observable} from 'rxjs/Observable';
|
5 |
| -import 'rxjs/add/operator/startWith'; |
6 |
| -import 'rxjs/add/observable/merge'; |
7 |
| -import 'rxjs/add/operator/map'; |
| 4 | +import 'rxjs/add/observable/of'; |
8 | 5 |
|
9 |
| -/** |
10 |
| - * @title Basic table |
11 |
| - */ |
12 | 6 | @Component({
|
13 | 7 | selector: 'table-basic-example',
|
14 | 8 | styleUrls: ['table-basic-example.css'],
|
15 | 9 | templateUrl: 'table-basic-example.html',
|
16 | 10 | })
|
17 | 11 | export class TableBasicExample {
|
18 |
| - displayedColumns = ['userId', 'userName', 'progress', 'color']; |
19 |
| - exampleDatabase = new ExampleDatabase(); |
20 |
| - dataSource: ExampleDataSource | null; |
21 |
| - |
22 |
| - ngOnInit() { |
23 |
| - this.dataSource = new ExampleDataSource(this.exampleDatabase); |
24 |
| - } |
| 12 | + displayedColumns = ['position', 'name', 'weight', 'symbol']; |
| 13 | + dataSource = new ExampleDataSource(); |
25 | 14 | }
|
26 | 15 |
|
27 |
| -/** Constants used to fill up our data base. */ |
28 |
| -const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple', |
29 |
| - 'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray']; |
30 |
| -const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack', |
31 |
| - 'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper', |
32 |
| - 'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth']; |
33 |
| - |
34 |
| -export interface UserData { |
35 |
| - id: string; |
| 16 | +export interface Element { |
36 | 17 | name: string;
|
37 |
| - progress: string; |
38 |
| - color: string; |
| 18 | + position: number; |
| 19 | + weight: number; |
| 20 | + symbol: string; |
39 | 21 | }
|
40 | 22 |
|
41 |
| -/** An example database that the data source uses to retrieve data for the table. */ |
42 |
| -export class ExampleDatabase { |
43 |
| - /** Stream that emits whenever the data has been modified. */ |
44 |
| - dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]); |
45 |
| - get data(): UserData[] { return this.dataChange.value; } |
46 |
| - |
47 |
| - constructor() { |
48 |
| - // Fill up the database with 100 users. |
49 |
| - for (let i = 0; i < 100; i++) { this.addUser(); } |
50 |
| - } |
51 |
| - |
52 |
| - /** Adds a new user to the database. */ |
53 |
| - addUser() { |
54 |
| - const copiedData = this.data.slice(); |
55 |
| - copiedData.push(this.createNewUser()); |
56 |
| - this.dataChange.next(copiedData); |
57 |
| - } |
58 |
| - |
59 |
| - /** Builds and returns a new User. */ |
60 |
| - private createNewUser() { |
61 |
| - const name = |
62 |
| - NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' + |
63 |
| - NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.'; |
64 |
| - |
65 |
| - return { |
66 |
| - id: (this.data.length + 1).toString(), |
67 |
| - name: name, |
68 |
| - progress: Math.round(Math.random() * 100).toString(), |
69 |
| - color: COLORS[Math.round(Math.random() * (COLORS.length - 1))] |
70 |
| - }; |
71 |
| - } |
72 |
| -} |
| 23 | +const data: Element[] = [ |
| 24 | + {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'}, |
| 25 | + {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}, |
| 26 | + {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'}, |
| 27 | + {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}, |
| 28 | + {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'}, |
| 29 | + {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'}, |
| 30 | + {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'}, |
| 31 | + {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}, |
| 32 | + {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'}, |
| 33 | + {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'}, |
| 34 | + {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'}, |
| 35 | + {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'}, |
| 36 | + {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'}, |
| 37 | + {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'}, |
| 38 | + {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'}, |
| 39 | + {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'}, |
| 40 | + {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'}, |
| 41 | + {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'}, |
| 42 | + {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'}, |
| 43 | + {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'}, |
| 44 | +]; |
73 | 45 |
|
74 | 46 | /**
|
75 |
| - * Data source to provide what data should be rendered in the table. Note that the data source |
76 |
| - * can retrieve its data in any way. In this case, the data source is provided a reference |
77 |
| - * to a common data base, ExampleDatabase. It is not the data source's responsibility to manage |
78 |
| - * the underlying data. Instead, it only needs to take the data and send the table exactly what |
79 |
| - * should be rendered. |
| 47 | + * Data source to provide what data should be rendered in the table. The observable provided |
| 48 | + * in connect should emit exactly the data that should be rendered by the table. If the data is |
| 49 | + * altered, the observable should emit that new set of data on the stream. In our case here, |
| 50 | + * we return a stream that contains only one set of data that doesn't change. |
80 | 51 | */
|
81 | 52 | export class ExampleDataSource extends DataSource<any> {
|
82 |
| - constructor(private _exampleDatabase: ExampleDatabase) { |
83 |
| - super(); |
84 |
| - } |
85 |
| - |
86 | 53 | /** Connect function called by the table to retrieve one stream containing the data to render. */
|
87 |
| - connect(): Observable<UserData[]> { |
88 |
| - return this._exampleDatabase.dataChange; |
| 54 | + connect(): Observable<Element[]> { |
| 55 | + return Observable.of(data); |
89 | 56 | }
|
90 | 57 |
|
91 | 58 | disconnect() {}
|
|
0 commit comments