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