@@ -524,23 +524,61 @@ describe('CdkTable', () => {
524
524
525
525
it ( 'should error if there is row data that does not have a matching row template' ,
526
526
fakeAsync ( ( ) => {
527
+ const whenRowWithoutDefaultFixture = createComponent ( WhenRowWithoutDefaultCdkTableApp ) ;
528
+ const data = whenRowWithoutDefaultFixture . componentInstance . dataSource . data ;
527
529
expect ( ( ) => {
528
530
try {
529
- createComponent ( WhenRowWithoutDefaultCdkTableApp ) . detectChanges ( ) ;
531
+ whenRowWithoutDefaultFixture . detectChanges ( ) ;
530
532
flush ( ) ;
531
533
} catch {
532
534
flush ( ) ;
533
535
}
534
- } ) . toThrowError ( getTableMissingMatchingRowDefError ( ) . message ) ;
536
+ } ) . toThrowError ( getTableMissingMatchingRowDefError ( data [ 0 ] ) . message ) ;
535
537
} ) ) ;
536
538
537
- it ( 'should error if there are multiple rows that do not have a when function ' , fakeAsync ( ( ) => {
539
+ it ( 'should fail when multiple rows match data without enableRowMultiplex ' , fakeAsync ( ( ) => {
538
540
let whenFixture = createComponent ( WhenRowMultipleDefaultsCdkTableApp ) ;
539
541
expect ( ( ) => {
540
542
whenFixture . detectChanges ( ) ;
541
543
flush ( ) ;
542
544
} ) . toThrowError ( getTableMultipleDefaultRowDefsError ( ) . message ) ;
543
545
} ) ) ;
546
+
547
+ describe ( 'with enableRowMultiplex' , ( ) => {
548
+ it ( 'should be able to render multiple rows per data object' , ( ) => {
549
+ let whenFixture = createComponent ( WhenRowCdkTableApp ) ;
550
+ whenFixture . componentInstance . enableRowMultiplex = true ;
551
+ whenFixture . detectChanges ( ) ;
552
+
553
+ const data = whenFixture . componentInstance . dataSource . data ;
554
+ expectTableToMatchContent ( whenFixture . nativeElement . querySelector ( 'cdk-table' ) , [
555
+ [ 'Column A' , 'Column B' , 'Column C' ] ,
556
+ [ data [ 0 ] . a , data [ 0 ] . b , data [ 0 ] . c ] ,
557
+ [ data [ 1 ] . a , data [ 1 ] . b , data [ 1 ] . c ] ,
558
+ [ 'index_1_special_row' ] ,
559
+ [ data [ 2 ] . a , data [ 2 ] . b , data [ 2 ] . c ] ,
560
+ [ 'c3_special_row' ] ,
561
+ [ data [ 3 ] . a , data [ 3 ] . b , data [ 3 ] . c ] ,
562
+ ] ) ;
563
+ } ) ;
564
+
565
+ it ( 'should have the correct data and row indicies' , ( ) => {
566
+ let whenFixture = createComponent ( WhenRowCdkTableApp ) ;
567
+ whenFixture . componentInstance . enableRowMultiplex = true ;
568
+ whenFixture . componentInstance . showIndexColumns ( ) ;
569
+ whenFixture . detectChanges ( ) ;
570
+
571
+ expectTableToMatchContent ( whenFixture . nativeElement . querySelector ( 'cdk-table' ) , [
572
+ [ 'Data Index' , 'Row Index' ] ,
573
+ [ '0' , '0' ] ,
574
+ [ '1' , '1' ] ,
575
+ [ '1' , '2' ] ,
576
+ [ '2' , '3' ] ,
577
+ [ '2' , '4' ] ,
578
+ [ '3' , '5' ] ,
579
+ ] ) ;
580
+ } ) ;
581
+ } ) ;
544
582
} ) ;
545
583
546
584
describe ( 'with trackBy' , ( ) => {
@@ -923,7 +961,7 @@ class BooleanRowCdkTableApp {
923
961
924
962
@Component ( {
925
963
template : `
926
- <cdk-table [dataSource]="dataSource">
964
+ <cdk-table [dataSource]="dataSource" [enableRowMultiplex]="enableRowMultiplex" >
927
965
<ng-container cdkColumnDef="column_a">
928
966
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
929
967
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
@@ -941,30 +979,50 @@ class BooleanRowCdkTableApp {
941
979
942
980
<ng-container cdkColumnDef="index1Column">
943
981
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
944
- <cdk-cell *cdkCellDef="let row"> index_1_special_row </cdk-cell>
982
+ <cdk-cell *cdkCellDef="let row"> index_1_special_row</cdk-cell>
945
983
</ng-container>
946
984
947
985
<ng-container cdkColumnDef="c3Column">
948
986
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
949
- <cdk-cell *cdkCellDef="let row"> c3_special_row </cdk-cell>
987
+ <cdk-cell *cdkCellDef="let row"> c3_special_row</cdk-cell>
988
+ </ng-container>
989
+
990
+ <ng-container cdkColumnDef="dataIndex">
991
+ <cdk-header-cell *cdkHeaderCellDef> Data Index</cdk-header-cell>
992
+ <cdk-cell *cdkCellDef="let row; let index = index"> {{index}}</cdk-cell>
993
+ </ng-container>
994
+
995
+ <ng-container cdkColumnDef="rowIndex">
996
+ <cdk-header-cell *cdkHeaderCellDef> Row Index</cdk-header-cell>
997
+ <cdk-cell *cdkCellDef="let row; let rowIndex = rowIndex"> {{rowIndex}}</cdk-cell>
950
998
</ng-container>
951
999
952
1000
<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
953
1001
<cdk-row *cdkRowDef="let row; columns: columnsToRender"></cdk-row>
954
- <cdk-row *cdkRowDef="let row; columns: ['index1Column'] ; when: isIndex1"></cdk-row>
955
- <cdk-row *cdkRowDef="let row; columns: ['c3Column'] ; when: hasC3"></cdk-row>
1002
+ <cdk-row *cdkRowDef="let row; columns: isIndex1Columns ; when: isIndex1"></cdk-row>
1003
+ <cdk-row *cdkRowDef="let row; columns: hasC3Columns ; when: hasC3"></cdk-row>
956
1004
</cdk-table>
957
1005
`
958
1006
} )
959
1007
class WhenRowCdkTableApp {
1008
+ enableRowMultiplex = false ;
960
1009
dataSource : FakeDataSource = new FakeDataSource ( ) ;
961
1010
columnsToRender = [ 'column_a' , 'column_b' , 'column_c' ] ;
1011
+ isIndex1Columns = [ 'index1Column' ] ;
1012
+ hasC3Columns = [ 'c3Column' ] ;
962
1013
isIndex1 = ( index : number , _rowData : TestData ) => index == 1 ;
963
1014
hasC3 = ( _index : number , rowData : TestData ) => rowData . c == 'c_3' ;
964
1015
965
1016
constructor ( ) { this . dataSource . addData ( ) ; }
966
1017
967
1018
@ViewChild ( CdkTable ) table : CdkTable < TestData > ;
1019
+
1020
+ showIndexColumns ( ) {
1021
+ const indexColumns = [ 'dataIndex' , 'rowIndex' ] ;
1022
+ this . columnsToRender = indexColumns ;
1023
+ this . isIndex1Columns = indexColumns ;
1024
+ this . hasC3Columns = indexColumns ;
1025
+ }
968
1026
}
969
1027
970
1028
@Component ( {
0 commit comments