@@ -13,6 +13,8 @@ import {MatRadioChange, MatTable, MatTableDataSource} from '@angular/material';
1313import { Observable } from 'rxjs' ;
1414import { DataSource } from '@angular/cdk/collections' ;
1515
16+ export type TrackByStrategy = 'position' | 'reference' | 'index' ;
17+
1618@Component ( {
1719 moduleId : module . id ,
1820 templateUrl : 'data-input-table.html' ,
@@ -23,39 +25,71 @@ export class DataInputTableDemo {
2325
2426 inputType : 'source' | 'stream' | 'array' | null = 'array' ;
2527 data = ELEMENT_DATA . slice ( ) ;
26- tableDataSource = new MatTableDataSource ( this . data ) ;
28+ matTableDataSource = new MatTableDataSource ( this . data ) ;
29+
30+ trackByStrategy : TrackByStrategy = 'reference' ;
31+ trackBy = ( index : number , item : Element ) => {
32+ switch ( this . trackByStrategy ) {
33+ case 'position' : return item . position ;
34+ case 'reference' : return item ;
35+ case 'index' : return index ;
36+ }
37+ }
2738
28- dataSourceInput : DataSource < Element > | Observable < Element [ ] > | Element [ ] | null = this . data ;
39+ dataSource : DataSource < Element > | Observable < Element [ ] > | Element [ ] | null = this . data ;
2940
3041 @ViewChild ( CdkTable ) cdkTable : CdkTable < Element > ;
3142 @ViewChild ( MatTable ) matTable : MatTable < Element > ;
3243
3344 changeInput ( e : MatRadioChange ) {
3445 this . inputType = e . value ;
3546 switch ( this . inputType ) {
36- case 'array' : this . dataSourceInput = this . data ; break ;
37- case 'stream' : this . dataSourceInput = this . tableDataSource . connect ( ) ; break ;
38- case 'source' : this . dataSourceInput = this . tableDataSource ; break ;
47+ case 'array' : this . dataSource = this . data ; break ;
48+ case 'stream' : this . dataSource = this . matTableDataSource . connect ( ) ; break ;
49+ case 'source' : this . dataSource = this . matTableDataSource ; break ;
3950 }
4051 }
4152
4253 addRow ( ) {
43- this . data . push ( { name : 'new' , weight : 0 , symbol : 'New' , position : 0 } ) ;
44- this . tableDataSource . data = this . data ;
54+ this . data . push ( {
55+ name : 'new' ,
56+ weight : Math . floor ( Math . random ( ) * 25 ) ,
57+ symbol : 'New' ,
58+ position : Math . floor ( Math . random ( ) * 25 )
59+ } ) ;
60+
61+ this . matTableDataSource . data = this . data ;
62+ }
63+
64+ shuffle ( ) {
65+ let dataToShuffle = this . data . slice ( ) ;
66+ let currentIndex = dataToShuffle . length ;
67+ while ( 0 !== currentIndex ) {
68+ let randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ;
69+ currentIndex -= 1 ;
70+
71+ // Swap
72+ let temp = dataToShuffle [ currentIndex ] ;
73+ dataToShuffle [ currentIndex ] = dataToShuffle [ randomIndex ] ;
74+ dataToShuffle [ randomIndex ] = temp ;
75+ }
76+
77+ this . data = dataToShuffle ;
78+ this . matTableDataSource . data = dataToShuffle ;
4579 }
4680
4781 removeRow ( ) {
4882 this . data . pop ( ) ;
49- this . tableDataSource . data = this . data ;
83+ this . matTableDataSource . data = this . data ;
5084 }
5185
5286 reassignDataClone ( ) {
5387 this . data = this . data . slice ( ) ;
5488
55- if ( this . dataSourceInput instanceof Array ) {
56- this . dataSourceInput = this . data ;
89+ if ( this . dataSource instanceof Array ) {
90+ this . dataSource = this . data ;
5791 }
58- this . tableDataSource . data = this . data ;
92+ this . matTableDataSource . data = this . data ;
5993 }
6094
6195 renderRows ( ) {
@@ -64,7 +98,11 @@ export class DataInputTableDemo {
6498 }
6599
66100 removeDataSource ( ) {
67- this . dataSourceInput = null ;
101+ this . dataSource = null ;
68102 this . inputType = null ;
69103 }
104+
105+ highlightFirstRow ( ) {
106+ document . querySelector ( 'table tbody tr' ) ! . setAttribute ( 'style' , 'background: red' ) ;
107+ }
70108}
0 commit comments