Skip to content

Commit a043bec

Browse files
andrewseguintinayuangao
authored andcommitted
docs(table): simplify first basic example (#6177)
* docs(table): simplify first basic example * update docs * export Element
1 parent d6f1a8d commit a043bec

File tree

3 files changed

+51
-93
lines changed

3 files changed

+51
-93
lines changed
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
/* Structure */
21
.example-container {
32
display: flex;
43
flex-direction: column;
4+
max-height: 500px;
55
min-width: 300px;
66
}
77

8-
.example-header {
9-
min-height: 64px;
10-
display: flex;
11-
align-items: center;
12-
padding-left: 24px;
13-
font-size: 20px;
14-
}
15-
168
.mat-table {
179
overflow: auto;
18-
max-height: 500px;
1910
}

src/material-examples/table-basic/table-basic-example.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44
<!--- Note that these columns can be defined in any order.
55
The actual rendered columns are set as a property on the row definition" -->
66

7-
<!-- ID Column -->
8-
<ng-container mdColumnDef="userId">
9-
<md-header-cell *mdHeaderCellDef> ID </md-header-cell>
10-
<md-cell *mdCellDef="let row"> {{row.id}} </md-cell>
11-
</ng-container>
12-
13-
<!-- Progress Column -->
14-
<ng-container mdColumnDef="progress">
15-
<md-header-cell *mdHeaderCellDef> Progress </md-header-cell>
16-
<md-cell *mdCellDef="let row"> {{row.progress}}% </md-cell>
7+
<!-- Position Column -->
8+
<ng-container mdColumnDef="position">
9+
<md-header-cell *mdHeaderCellDef> No. </md-header-cell>
10+
<md-cell *mdCellDef="let element"> {{element.position}} </md-cell>
1711
</ng-container>
1812

1913
<!-- Name Column -->
20-
<ng-container mdColumnDef="userName">
14+
<ng-container mdColumnDef="name">
2115
<md-header-cell *mdHeaderCellDef> Name </md-header-cell>
22-
<md-cell *mdCellDef="let row"> {{row.name}} </md-cell>
16+
<md-cell *mdCellDef="let element"> {{element.name}} </md-cell>
17+
</ng-container>
18+
19+
<!-- Weight Column -->
20+
<ng-container mdColumnDef="weight">
21+
<md-header-cell *mdHeaderCellDef> Weight </md-header-cell>
22+
<md-cell *mdCellDef="let element"> {{element.weight}} </md-cell>
2323
</ng-container>
2424

2525
<!-- Color Column -->
26-
<ng-container mdColumnDef="color">
27-
<md-header-cell *mdHeaderCellDef>Color</md-header-cell>
28-
<md-cell *mdCellDef="let row" [style.color]="row.color"> {{row.color}} </md-cell>
26+
<ng-container mdColumnDef="symbol">
27+
<md-header-cell *mdHeaderCellDef> Symbol </md-header-cell>
28+
<md-cell *mdCellDef="let element"> {{element.symbol}} </md-cell>
2929
</ng-container>
3030

3131
<md-header-row *mdHeaderRowDef="displayedColumns"></md-header-row>

src/material-examples/table-basic/table-basic-example.ts

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,58 @@
11
import {Component} from '@angular/core';
22
import {DataSource} from '@angular/cdk/table';
3-
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
43
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';
85

9-
/**
10-
* @title Basic table
11-
*/
126
@Component({
137
selector: 'table-basic-example',
148
styleUrls: ['table-basic-example.css'],
159
templateUrl: 'table-basic-example.html',
1610
})
1711
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();
2514
}
2615

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 {
3617
name: string;
37-
progress: string;
38-
color: string;
18+
position: number;
19+
weight: number;
20+
symbol: string;
3921
}
4022

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+
];
7345

7446
/**
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.
8051
*/
8152
export class ExampleDataSource extends DataSource<any> {
82-
constructor(private _exampleDatabase: ExampleDatabase) {
83-
super();
84-
}
85-
8653
/** 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);
8956
}
9057

9158
disconnect() {}

0 commit comments

Comments
 (0)