From ed1004878a69c2c3e64905fae8303c1e592384e9 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Mon, 31 Jul 2017 13:59:02 -0700 Subject: [PATCH 1/3] docs(table): simplify first basic example --- .../table-basic/table-basic-example.css | 11 +-- .../table-basic/table-basic-example.html | 34 +++---- .../table-basic/table-basic-example.ts | 94 ++++++------------- 3 files changed, 49 insertions(+), 90 deletions(-) diff --git a/src/material-examples/table-basic/table-basic-example.css b/src/material-examples/table-basic/table-basic-example.css index 5b9032d196b3..31bd3b658d8b 100644 --- a/src/material-examples/table-basic/table-basic-example.css +++ b/src/material-examples/table-basic/table-basic-example.css @@ -1,19 +1,10 @@ -/* Structure */ .example-container { display: flex; flex-direction: column; + max-height: 500px; min-width: 300px; } -.example-header { - min-height: 64px; - display: flex; - align-items: center; - padding-left: 24px; - font-size: 20px; -} - .mat-table { overflow: auto; - max-height: 500px; } diff --git a/src/material-examples/table-basic/table-basic-example.html b/src/material-examples/table-basic/table-basic-example.html index c6702956d43e..66dd020c6d55 100644 --- a/src/material-examples/table-basic/table-basic-example.html +++ b/src/material-examples/table-basic/table-basic-example.html @@ -4,31 +4,31 @@ - - - ID - {{row.id}} + + + No. + {{element.position}} - - - Progress - {{row.progress}}% + + + Name + {{element.name}} - - - Name - {{row.name}} + + + Weight + {{element.weight}} - - Color - {{row.color}} + + Symbol + {{element.symbol}} - - + + diff --git a/src/material-examples/table-basic/table-basic-example.ts b/src/material-examples/table-basic/table-basic-example.ts index 4db0a4aaa62e..b260d638a5ed 100644 --- a/src/material-examples/table-basic/table-basic-example.ts +++ b/src/material-examples/table-basic/table-basic-example.ts @@ -1,75 +1,47 @@ import {Component} from '@angular/core'; import {DataSource} from '@angular/cdk/table'; -import {BehaviorSubject} from 'rxjs/BehaviorSubject'; import {Observable} from 'rxjs/Observable'; -import 'rxjs/add/operator/startWith'; -import 'rxjs/add/observable/merge'; -import 'rxjs/add/operator/map'; +import 'rxjs/add/observable/of'; -/** - * @title Basic table - */ @Component({ selector: 'table-basic-example', styleUrls: ['table-basic-example.css'], templateUrl: 'table-basic-example.html', }) export class TableBasicExample { - displayedColumns = ['userId', 'userName', 'progress', 'color']; - exampleDatabase = new ExampleDatabase(); - dataSource: ExampleDataSource | null; - - ngOnInit() { - this.dataSource = new ExampleDataSource(this.exampleDatabase); - } + displayedColumns = ['position', 'name', 'weight', 'symbol']; + dataSource = new ExampleDataSource(); } -/** Constants used to fill up our data base. */ -const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple', - 'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray']; -const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack', - 'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper', - 'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth']; - -export interface UserData { - id: string; +interface Element { name: string; - progress: string; - color: string; + position: number; + weight: number; + symbol: string; } -/** An example database that the data source uses to retrieve data for the table. */ -export class ExampleDatabase { - /** Stream that emits whenever the data has been modified. */ - dataChange: BehaviorSubject = new BehaviorSubject([]); - get data(): UserData[] { return this.dataChange.value; } - - constructor() { - // Fill up the database with 100 users. - for (let i = 0; i < 100; i++) { this.addUser(); } - } - - /** Adds a new user to the database. */ - addUser() { - const copiedData = this.data.slice(); - copiedData.push(this.createNewUser()); - this.dataChange.next(copiedData); - } - - /** Builds and returns a new User. */ - private createNewUser() { - const name = - NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' + - NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.'; - - return { - id: (this.data.length + 1).toString(), - name: name, - progress: Math.round(Math.random() * 100).toString(), - color: COLORS[Math.round(Math.random() * (COLORS.length - 1))] - }; - } -} +const data: Element[] = [ + {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'}, + {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}, + {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'}, + {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}, + {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'}, + {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'}, + {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'}, + {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}, + {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'}, + {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'}, + {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'}, + {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'}, + {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'}, + {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'}, + {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'}, + {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'}, + {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'}, + {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'}, + {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'}, + {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'}, +]; /** * Data source to provide what data should be rendered in the table. Note that the data source @@ -79,13 +51,9 @@ export class ExampleDatabase { * should be rendered. */ export class ExampleDataSource extends DataSource { - constructor(private _exampleDatabase: ExampleDatabase) { - super(); - } - /** Connect function called by the table to retrieve one stream containing the data to render. */ - connect(): Observable { - return this._exampleDatabase.dataChange; + connect(): Observable { + return Observable.of(data); } disconnect() {} From 06d6a5d06c5edf250ad71d3429f08389f3d4b9bd Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 1 Aug 2017 10:30:36 -0700 Subject: [PATCH 2/3] update docs --- src/material-examples/table-basic/table-basic-example.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/material-examples/table-basic/table-basic-example.ts b/src/material-examples/table-basic/table-basic-example.ts index b260d638a5ed..1f69d14f1751 100644 --- a/src/material-examples/table-basic/table-basic-example.ts +++ b/src/material-examples/table-basic/table-basic-example.ts @@ -44,11 +44,10 @@ const data: Element[] = [ ]; /** - * Data source to provide what data should be rendered in the table. Note that the data source - * can retrieve its data in any way. In this case, the data source is provided a reference - * to a common data base, ExampleDatabase. It is not the data source's responsibility to manage - * the underlying data. Instead, it only needs to take the data and send the table exactly what - * should be rendered. + * Data source to provide what data should be rendered in the table. The observable provided + * in connect should emit exactly the data that should be rendered by the table. If the data is + * altered, the observable should emit that new set of data on the stream. In our case here, + * we return a stream that contains only one set of data that doesn't change. */ export class ExampleDataSource extends DataSource { /** Connect function called by the table to retrieve one stream containing the data to render. */ From fe9b3b83fd04d1bbecb956dd889c9b951012104b Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Wed, 2 Aug 2017 09:54:41 -0700 Subject: [PATCH 3/3] export Element --- src/material-examples/table-basic/table-basic-example.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material-examples/table-basic/table-basic-example.ts b/src/material-examples/table-basic/table-basic-example.ts index 1f69d14f1751..be57119ec16e 100644 --- a/src/material-examples/table-basic/table-basic-example.ts +++ b/src/material-examples/table-basic/table-basic-example.ts @@ -13,7 +13,7 @@ export class TableBasicExample { dataSource = new ExampleDataSource(); } -interface Element { +export interface Element { name: string; position: number; weight: number;