1- import { Component , ViewChild } from '@angular/core' ;
1+ import { Component , ElementRef , ViewChild } from '@angular/core' ;
22import { PeopleDatabase , UserData } from './people-database' ;
33import { PersonDataSource } from './person-data-source' ;
44import { 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' ;
7+ import { SelectionModel } from '@angular/cdk/collections' ;
8+ import { Observable } from 'rxjs/Observable' ;
9+ import 'rxjs/add/operator/distinctUntilChanged' ;
10+ import 'rxjs/add/operator/debounceTime' ;
11+ import 'rxjs/add/observable/fromEvent' ;
812
913export type UserProperties = 'userId' | 'userName' | 'progress' | 'color' | undefined ;
1014
@@ -35,7 +39,10 @@ export class TableDemo {
3539 highlights = new Set < string > ( ) ;
3640 wasExpanded = new Set < UserData > ( ) ;
3741
38- filter = new FormControl ( ) ;
42+ matTableDataSourceColumns = [ 'select' , 'userId' , 'userName' , 'progress' , 'color' ] ;
43+ selection = new SelectionModel < UserData > ( true , [ ] ) ;
44+
45+ @ViewChild ( 'filter' ) filter : ElementRef ;
3946
4047 dynamicColumnDefs : any [ ] = [ ] ;
4148 dynamicColumnIds : string [ ] = [ ] ;
@@ -62,7 +69,6 @@ export class TableDemo {
6269 } ;
6370 this . matTableDataSource . filterPredicate =
6471 ( data : UserData , filter : string ) => data . name . indexOf ( filter ) != - 1 ;
65- this . filter . valueChanges . subscribe ( filter => this . matTableDataSource ! . filter = filter ) ;
6672 }
6773
6874 ngAfterViewInit ( ) {
@@ -74,6 +80,43 @@ export class TableDemo {
7480
7581 ngOnInit ( ) {
7682 this . connect ( ) ;
83+ Observable . fromEvent ( this . filter . nativeElement , 'keyup' )
84+ . debounceTime ( 150 )
85+ . distinctUntilChanged ( )
86+ . subscribe ( ( ) => {
87+ this . paginatorForDataSource . pageIndex = 0 ;
88+ this . matTableDataSource . filter = this . filter . nativeElement . value ;
89+ } ) ;
90+ }
91+
92+ /** Whether all filtered rows are selected. */
93+ isAllFilteredRowsSelected ( ) {
94+ return this . matTableDataSource . filteredData . every ( data => this . selection . isSelected ( data ) ) ;
95+ }
96+
97+ /** Whether the selection it totally matches the filtered rows. */
98+ isMasterToggleChecked ( ) {
99+ return this . selection . hasValue ( ) &&
100+ this . isAllFilteredRowsSelected ( ) &&
101+ this . selection . selected . length >= this . matTableDataSource . filteredData . length ;
102+ }
103+
104+ /**
105+ * Whether there is a selection that doesn't capture all the
106+ * filtered rows there are no filtered rows displayed.
107+ */
108+ isMasterToggleIndeterminate ( ) {
109+ return this . selection . hasValue ( ) &&
110+ ( ! this . isAllFilteredRowsSelected ( ) || ! this . matTableDataSource . filteredData . length ) ;
111+ }
112+
113+ /** Selects all filtered rows if they are not all selected; otherwise clear selection. */
114+ masterToggle ( ) {
115+ if ( this . isMasterToggleChecked ( ) ) {
116+ this . selection . clear ( ) ;
117+ } else {
118+ this . matTableDataSource . filteredData . forEach ( data => this . selection . select ( data ) ) ;
119+ }
77120 }
78121
79122 addDynamicColumnDef ( ) {
0 commit comments