/
text-column.ts
173 lines (152 loc) · 5.77 KB
/
text-column.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
ChangeDetectionStrategy,
Component,
Inject,
Input,
OnDestroy,
OnInit,
Optional,
ViewChild,
ViewEncapsulation,
isDevMode,
} from '@angular/core';
import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
import {CdkTable} from './table';
import {
getTableTextColumnMissingParentTableError,
getTableTextColumnMissingNameError,
} from './table-errors';
import {TEXT_COLUMN_OPTIONS, TextColumnOptions} from './tokens';
/**
* Column that simply shows text content for the header and row cells. Assumes that the table
* is using the native table implementation (`<table>`).
*
* By default, the name of this column will be the header text and data property accessor.
* The header text can be overridden with the `headerText` input. Cell values can be overridden with
* the `dataAccessor` input. Change the text justification to the start or end using the `justify`
* input.
*/
@Component({
selector: 'cdk-text-column',
template: `
<ng-container cdkColumnDef>
<th cdk-header-cell *cdkHeaderCellDef [style.text-align]="justify">
{{headerText}}
</th>
<td cdk-cell *cdkCellDef="let data" [style.text-align]="justify">
{{dataAccessor(data, name)}}
</td>
</ng-container>
`,
encapsulation: ViewEncapsulation.None,
// Change detection is intentionally not set to OnPush. This component's template will be provided
// to the table to be inserted into its view. This is problematic when change detection runs since
// the bindings in this template will be evaluated _after_ the table's view is evaluated, which
// mean's the template in the table's view will not have the updated value (and in fact will cause
// an ExpressionChangedAfterItHasBeenCheckedError).
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
})
export class CdkTextColumn<T> implements OnDestroy, OnInit {
/** Column name that should be used to reference this column. */
@Input()
get name(): string {
return this._name;
}
set name(name: string) {
this._name = name;
// With Ivy, inputs can be initialized before static query results are
// available. In that case, we defer the synchronization until "ngOnInit" fires.
this._syncColumnDefName();
}
_name: string;
/**
* Text label that should be used for the column header. If this property is not
* set, the header text will default to the column name with its first letter capitalized.
*/
@Input() headerText: string;
/**
* Accessor function to retrieve the data rendered for each cell. If this
* property is not set, the data cells will render the value found in the data's property matching
* the column's name. For example, if the column is named `id`, then the rendered value will be
* value defined by the data's `id` property.
*/
@Input() dataAccessor: (data: T, name: string) => string;
/** Alignment of the cell values. */
@Input() justify: 'start'|'end' = 'start';
/** @docs-private */
@ViewChild(CdkColumnDef, {static: true}) columnDef: CdkColumnDef;
/**
* The column cell is provided to the column during `ngOnInit` with a static query.
* Normally, this will be retrieved by the column using `ContentChild`, but that assumes the
* column definition was provided in the same view as the table, which is not the case with this
* component.
* @docs-private
*/
@ViewChild(CdkCellDef, {static: true}) cell: CdkCellDef;
/**
* The column headerCell is provided to the column during `ngOnInit` with a static query.
* Normally, this will be retrieved by the column using `ContentChild`, but that assumes the
* column definition was provided in the same view as the table, which is not the case with this
* component.
* @docs-private
*/
@ViewChild(CdkHeaderCellDef, {static: true}) headerCell: CdkHeaderCellDef;
constructor(
@Optional() private _table: CdkTable<T>,
@Optional() @Inject(TEXT_COLUMN_OPTIONS) private _options: TextColumnOptions<T>) {
this._options = _options || {};
}
ngOnInit() {
this._syncColumnDefName();
if (this.headerText === undefined) {
this.headerText = this._createDefaultHeaderText();
}
if (!this.dataAccessor) {
this.dataAccessor =
this._options.defaultDataAccessor || ((data: T, name: string) => (data as any)[name]);
}
if (this._table) {
// Provide the cell and headerCell directly to the table with the static `ViewChild` query,
// since the columnDef will not pick up its content by the time the table finishes checking
// its content and initializing the rows.
this.columnDef.cell = this.cell;
this.columnDef.headerCell = this.headerCell;
this._table.addColumnDef(this.columnDef);
} else {
throw getTableTextColumnMissingParentTableError();
}
}
ngOnDestroy() {
if (this._table) {
this._table.removeColumnDef(this.columnDef);
}
}
/**
* Creates a default header text. Use the options' header text transformation function if one
* has been provided. Otherwise simply capitalize the column name.
*/
_createDefaultHeaderText() {
const name = this.name;
if (isDevMode() && !name) {
throw getTableTextColumnMissingNameError();
}
if (this._options && this._options.defaultHeaderTextTransform) {
return this._options.defaultHeaderTextTransform(name);
}
return name[0].toUpperCase() + name.slice(1);
}
/** Synchronizes the column definition name with the text column name. */
private _syncColumnDefName() {
if (this.columnDef) {
this.columnDef.name = this.name;
}
}
}