11import { Component , ViewChild } from '@angular/core' ;
22import { PeopleDatabase , UserData } from './people-database' ;
33import { PersonDataSource } from './person-data-source' ;
4- import { MatPaginator , MatSort } from '@angular/material' ;
4+ import { MatPaginator , MatSort , MatTableDataSource } from '@angular/material' ;
55import { DetailRow , PersonDetailDataSource } from './person-detail-data-source' ;
66import { animate , state , style , transition , trigger } from '@angular/animations' ;
7+ import { FormControl } from '@angular/forms' ;
78
89export type UserProperties = 'userId' | 'userName' | 'progress' | 'color' | undefined ;
910
@@ -27,24 +28,48 @@ const properties = ['id', 'name', 'progress', 'color'];
2728export class TableDemo {
2829 dataSource : PersonDataSource | null ;
2930 dataSourceWithDetails : PersonDetailDataSource | null ;
31+ matTableDataSource = new MatTableDataSource < UserData > ( ) ;
3032 displayedColumns : UserProperties [ ] = [ ] ;
3133 trackByStrategy : TrackByStrategy = 'reference' ;
3234 changeReferences = false ;
3335 highlights = new Set < string > ( ) ;
3436 wasExpanded = new Set < UserData > ( ) ;
3537
38+ filter = new FormControl ( ) ;
39+
3640 dynamicColumnDefs : any [ ] = [ ] ;
3741 dynamicColumnIds : string [ ] = [ ] ;
3842
3943 expandedPerson : UserData ;
4044
41- @ViewChild ( MatPaginator ) _paginator : MatPaginator ;
42-
45+ @ViewChild ( MatPaginator ) paginator : MatPaginator ;
4346 @ViewChild ( MatSort ) sort : MatSort ;
4447
4548 isDetailRow = ( row : DetailRow | UserData ) => row . hasOwnProperty ( 'detailRow' ) ;
4649
47- constructor ( public _peopleDatabase : PeopleDatabase ) { }
50+ @ViewChild ( 'paginatorForDataSource' ) paginatorForDataSource : MatPaginator ;
51+ @ViewChild ( 'sortForDataSource' ) sortForDataSource : MatSort ;
52+
53+ constructor ( public _peopleDatabase : PeopleDatabase ) {
54+ this . matTableDataSource . sortingDataAccessor = ( data : UserData , property : string ) => {
55+ switch ( property ) {
56+ case 'userId' : return + data . id ;
57+ case 'userName' : return data . name ;
58+ case 'progress' : return + data . progress ;
59+ case 'color' : return data . color ;
60+ default : return '' ;
61+ }
62+ } ;
63+ this . matTableDataSource . filterTermAccessor = ( data : UserData ) => data . name ;
64+ this . filter . valueChanges . subscribe ( filter => this . matTableDataSource ! . filter = filter ) ;
65+ }
66+
67+ ngAfterViewInit ( ) {
68+ // Needs to be set up after the view is initialized since the data source will look at the sort
69+ // and paginator's initial values to know what data should be rendered.
70+ this . matTableDataSource ! . paginator = this . paginatorForDataSource ;
71+ this . matTableDataSource ! . sort = this . sortForDataSource ;
72+ }
4873
4974 ngOnInit ( ) {
5075 this . connect ( ) ;
@@ -69,9 +94,10 @@ export class TableDemo {
6994 connect ( ) {
7095 this . displayedColumns = [ 'userId' , 'userName' , 'progress' , 'color' ] ;
7196 this . dataSource = new PersonDataSource ( this . _peopleDatabase ,
72- this . _paginator , this . sort ) ;
97+ this . paginator , this . sort ) ;
7398 this . dataSourceWithDetails = new PersonDetailDataSource ( this . dataSource ) ;
7499 this . _peopleDatabase . initialize ( ) ;
100+ this . matTableDataSource ! . data = this . _peopleDatabase . data . slice ( ) ;
75101 }
76102
77103 disconnect ( ) {
0 commit comments