@@ -20,21 +20,23 @@ import {
20
20
} from '@angular/core' ;
21
21
import { CdkColumnDef } from './cell' ;
22
22
import { CdkTable } from './table' ;
23
+ import { getTableTextColumnMissingParentTableError } from './table-errors' ;
23
24
24
25
/** Configurable options for `CdkTextColumn`. */
25
- export interface TextColumnOptions {
26
+ export interface TextColumnOptions < T > {
26
27
/**
27
28
* Default function that provides the header text based on the column name if a header
28
29
* text is not provided.
29
30
*/
30
- defaultHeaderTextTransformation ?: ( name : string ) => string ;
31
+ defaultHeaderTextTransform ?: ( name : string ) => string ;
31
32
32
33
/** Default data accessor to use if one is not provided. */
33
- defaultDataAccessor ?: ( data : any , name : string ) => string ;
34
+ defaultDataAccessor ?: ( data : T , name : string ) => string ;
34
35
}
35
36
36
37
/** Injection token that can be used to specify the text column options. */
37
- export const TEXT_COLUMN_OPTIONS = new InjectionToken < TextColumnOptions > ( 'text-column-options' ) ;
38
+ export const TEXT_COLUMN_OPTIONS =
39
+ new InjectionToken < TextColumnOptions < any > > ( 'text-column-options' ) ;
38
40
39
41
/**
40
42
* Column that simply shows text content for the header and row cells. Assumes that the table
@@ -70,7 +72,7 @@ export const TEXT_COLUMN_OPTIONS = new InjectionToken<TextColumnOptions>('text-c
70
72
export class CdkTextColumn < T > implements OnDestroy , OnInit {
71
73
/** Column name that should be used to reference this column. */
72
74
@Input ( )
73
- get name ( ) : string { return this . _name ; }
75
+ get name ( ) : string { return this . _name ; }
74
76
set name ( name : string ) {
75
77
this . _name = name ;
76
78
this . columnDef . name = name ;
@@ -97,7 +99,7 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
97
99
@ViewChild ( CdkColumnDef ) columnDef : CdkColumnDef ;
98
100
99
101
constructor ( @Optional ( ) private table : CdkTable < T > ,
100
- @Optional ( ) @Inject ( TEXT_COLUMN_OPTIONS ) private options : TextColumnOptions ) {
102
+ @Optional ( ) @Inject ( TEXT_COLUMN_OPTIONS ) private options : TextColumnOptions < T > ) {
101
103
this . options = options || { } ;
102
104
}
103
105
@@ -108,11 +110,13 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
108
110
109
111
if ( ! this . dataAccessor ) {
110
112
this . dataAccessor = this . options . defaultDataAccessor ||
111
- ( ( data : T , name : string ) => ( data as any ) [ name ] ) ;
113
+ ( ( data : T , name : string ) => ( data as any ) [ name ] ) ;
112
114
}
113
115
114
116
if ( this . table ) {
115
117
this . table . addColumnDef ( this . columnDef ) ;
118
+ } else {
119
+ throw getTableTextColumnMissingParentTableError ( ) ;
116
120
}
117
121
}
118
122
@@ -127,10 +131,10 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
127
131
* has been provided. Otherwise simply capitalize the column name.
128
132
*/
129
133
_createDefaultHeaderText ( ) {
130
- if ( this . options && this . options . defaultHeaderTextTransformation ) {
131
- return this . options . defaultHeaderTextTransformation ( this . name ) ;
134
+ if ( this . options && this . options . defaultHeaderTextTransform ) {
135
+ return this . options . defaultHeaderTextTransform ( this . name ) ;
132
136
}
133
137
134
- return this . name . charAt ( 0 ) . toUpperCase ( ) + this . name . slice ( 1 ) ;
138
+ return this . name [ 0 ] . toUpperCase ( ) + this . name . slice ( 1 ) ;
135
139
}
136
140
}
0 commit comments