Skip to content

Commit 036772e

Browse files
committed
New MD/CDK table added
1 parent 10b227a commit 036772e

File tree

5 files changed

+181
-4
lines changed

5 files changed

+181
-4
lines changed

src/app/components/misc/misc.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ <h3>MD Dialog (in progress) : </h3>
5858

5959
<br/>
6060

61+
<app-table></app-table>
62+
63+
<br/>
64+
6165
<!-- Pipes -->
6266
<app-change-detection></app-change-detection>
6367

src/app/components/misc/misc.module.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { CommonModule } from '@angular/common';
33

44
import { MdButtonModule, MdCheckboxModule, MdMenuModule, MdInputModule,
55
MdToolbarModule, MdDialogModule, MdNativeDateModule, MdSlideToggleModule,
6-
MdTooltipModule, MdSidenavModule, MaterialModule } from '@angular/material';
6+
MdTooltipModule, MdSidenavModule, MdTableModule, MaterialModule } from '@angular/material';
77

8+
import { CdkTableModule } from '@angular/cdk';
89
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
910
import { PipesModule } from '../../pipes/pipes.module';
1011

@@ -16,6 +17,7 @@ import { ChangeDetectionComponent } from './change/change-detection.component';
1617
import { ChangeDetailComponent } from './change/change-detail.component';
1718
import { VirtRealComponent } from './virtual-reality/virtreal.component';
1819
import { DialogComponent, DialogResultDialogComponent } from './dialog/dialog.component';
20+
import { TableComponent } from './table/table.component';
1921

2022
@NgModule({
2123
declarations: [
@@ -24,13 +26,15 @@ import { DialogComponent, DialogResultDialogComponent } from './dialog/dialog.co
2426
ChangeDetectionComponent,
2527
ChangeDetailComponent,
2628
VirtRealComponent,
27-
DialogComponent, DialogResultDialogComponent
29+
DialogComponent, DialogResultDialogComponent,
30+
TableComponent
2831
],
2932
imports: [
3033
CommonModule,
3134
MdButtonModule, MdCheckboxModule, MdMenuModule, MdInputModule,
3235
MdToolbarModule, MdDialogModule, MdNativeDateModule, MdSlideToggleModule,
33-
MdTooltipModule, MdSidenavModule, MaterialModule,
36+
MdTooltipModule, MdSidenavModule, MdTableModule, MaterialModule,
37+
CdkTableModule,
3438
FormsModule, ReactiveFormsModule,
3539
PipesModule,
3640
// MiscRoutingModule
@@ -41,7 +45,8 @@ import { DialogComponent, DialogResultDialogComponent } from './dialog/dialog.co
4145
ChangeDetectionComponent,
4246
ChangeDetailComponent,
4347
VirtRealComponent,
44-
DialogComponent, DialogResultDialogComponent
48+
DialogComponent, DialogResultDialogComponent,
49+
TableComponent
4550
]
4651
})
4752

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h3>MD/CDK Table : </h3>
2+
<div class="container mat-elevation-z8">
3+
<md-table #table [dataSource]="dataSource" mdSort>
4+
5+
<ng-container cdkColumnDef="userId">
6+
<md-header-cell *cdkHeaderCellDef md-sort-header> ID </md-header-cell>
7+
<md-cell *cdkCellDef="let row"> {{row.id}} </md-cell>
8+
</ng-container>
9+
10+
<ng-container cdkColumnDef="progress">
11+
<md-header-cell *cdkHeaderCellDef md-sort-header> Progress </md-header-cell>
12+
<md-cell *cdkCellDef="let row"> {{row.progress}}% </md-cell>
13+
</ng-container>
14+
15+
<ng-container cdkColumnDef="userName">
16+
<md-header-cell *cdkHeaderCellDef md-sort-header> Name </md-header-cell>
17+
<md-cell *cdkCellDef="let row"> {{row.name}} </md-cell>
18+
</ng-container>
19+
20+
<ng-container cdkColumnDef="color">
21+
<md-header-cell *cdkHeaderCellDef md-sort-header> Color </md-header-cell>
22+
<md-cell *cdkCellDef="let row" [style.color]="row.color"> {{row.color}} </md-cell>
23+
</ng-container>
24+
25+
<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
26+
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>
27+
28+
</md-table>
29+
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.container {
2+
display: flex;
3+
flex-direction: column;
4+
height: 300px;
5+
min-width: 300px;
6+
}
7+
8+
.header {
9+
min-height: 64px;
10+
display: flex;
11+
align-items: center;
12+
padding-left: 24px;
13+
font-size: 20px;
14+
}
15+
16+
.mat-table {
17+
overflow: auto;
18+
}
19+
20+
.mat-header-cell .mat-sort-header-sorted {
21+
color: black;
22+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { Component, OnInit, ViewChild } from '@angular/core';
2+
import { DataSource } from '@angular/cdk';
3+
import { MdSort } from '@angular/material';
4+
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5+
import { Observable } from 'rxjs/Observable';
6+
7+
import 'rxjs/add/operator/startWith';
8+
import 'rxjs/add/observable/merge';
9+
import 'rxjs/add/operator/map';
10+
11+
@Component({
12+
selector: 'app-table',
13+
templateUrl: './table.component.html',
14+
styleUrls: ['./table.component.scss']
15+
})
16+
export class TableComponent implements OnInit {
17+
displayedColumns = ['userId', 'userName', 'progress', 'color'];
18+
database = new database();
19+
dataSource: dataSource | null;
20+
21+
@ViewChild(MdSort) sort: MdSort;
22+
23+
constructor() { }
24+
25+
ngOnInit() {
26+
this.dataSource = new dataSource(this.database, this.sort);
27+
}
28+
}
29+
30+
// Local data
31+
const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
32+
'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
33+
const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
34+
'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
35+
'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];
36+
37+
export interface UserData {
38+
id: string;
39+
name: string;
40+
progress: string;
41+
color: string;
42+
}
43+
44+
export class database {
45+
// Stream that emits whenever the data has been modified
46+
dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]);
47+
get data(): UserData[] { return this.dataChange.value; }
48+
49+
constructor() {
50+
// Fill up the database with 100 users
51+
for (let i = 0; i < 100; i++) { this.addUser(); }
52+
}
53+
54+
// Adds a new user to the database
55+
addUser() {
56+
const copiedData = this.data.slice();
57+
copiedData.push(this.createNewUser());
58+
this.dataChange.next(copiedData);
59+
}
60+
61+
// Builds and returns a new User
62+
private createNewUser() {
63+
const name =
64+
NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' +
65+
NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.';
66+
67+
return {
68+
id: (this.data.length + 1).toString(),
69+
name: name,
70+
progress: Math.round(Math.random() * 100).toString(),
71+
color: COLORS[Math.round(Math.random() * (COLORS.length - 1))]
72+
};
73+
}
74+
}
75+
76+
// Data source to provide what data should be rendered in the table
77+
export class dataSource extends DataSource<any> {
78+
constructor(private database: database, private sort: MdSort) {
79+
super();
80+
}
81+
82+
connect(): Observable<UserData[]> {
83+
const displayDataChanges = [
84+
this.database.dataChange,
85+
this.sort.mdSortChange,
86+
];
87+
88+
return Observable.merge(...displayDataChanges).map(() => {
89+
return this.getSortedData();
90+
});
91+
}
92+
93+
disconnect() {}
94+
95+
// Sorting the database data
96+
getSortedData(): UserData[] {
97+
const data = this.database.data.slice();
98+
if (!this.sort.active || this.sort.direction == '') { return data; }
99+
100+
return data.sort((a, b) => {
101+
let propertyA: number|string = '';
102+
let propertyB: number|string = '';
103+
104+
switch (this.sort.active) {
105+
case 'userId': [propertyA, propertyB] = [a.id, b.id]; break;
106+
case 'userName': [propertyA, propertyB] = [a.name, b.name]; break;
107+
case 'progress': [propertyA, propertyB] = [a.progress, b.progress]; break;
108+
case 'color': [propertyA, propertyB] = [a.color, b.color]; break;
109+
}
110+
111+
let valueA = isNaN(+propertyA) ? propertyA : +propertyA;
112+
let valueB = isNaN(+propertyB) ? propertyB : +propertyB;
113+
114+
return (valueA < valueB ? -1 : 1) * (this.sort.direction == 'asc' ? 1 : -1);
115+
});
116+
}
117+
}

0 commit comments

Comments
 (0)