Skip to content

Commit ca14529

Browse files
committed
checkin
1 parent 4701c7b commit ca14529

19 files changed

+361
-321
lines changed

src/cdk/table/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './row';
1212
export * from './table-module';
1313
export * from './sticky-styler';
1414
export * from './can-stick';
15+
export * from './text-column';
1516

1617
/** Re-export DataSource for a more intuitive experience for users of just the table. */
1718
export {DataSource} from '@angular/cdk/collections';

src/cdk/table/table-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
CdkColumnDef, CdkHeaderCellDef, CdkHeaderCell, CdkCell, CdkCellDef,
1818
CdkFooterCellDef, CdkFooterCell
1919
} from './cell';
20+
import {CdkTextColumn} from './text-column';
2021

2122
const EXPORTED_DECLARATIONS = [
2223
CdkTable,
@@ -37,6 +38,7 @@ const EXPORTED_DECLARATIONS = [
3738
DataRowOutlet,
3839
HeaderRowOutlet,
3940
FooterRowOutlet,
41+
CdkTextColumn,
4042
];
4143

4244
@NgModule({

src/cdk/table/text-column.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Component, Input, OnDestroy, OnInit, Optional, ViewChild} from '@angular/core';
10+
import {CdkColumnDef} from './cell';
11+
import {CdkTable} from './table';
12+
13+
/**
14+
* Column that shows simply shows text content for the header and row cells. Assumes that the table
15+
* is using the native table implementation (`<table>`)
16+
*
17+
* By default, the name of this column will be assumed to be both the header text and data property
18+
* used to access the data value to show in cells. The header text can be overridden with the
19+
* `headerText` input. Cell values can be overridden with the `dataAccessor` input.
20+
*/
21+
@Component({
22+
selector: 'cdk-text-column',
23+
template: `
24+
<ng-container cdkColumnDef>
25+
<th cdk-header-cell *cdkHeaderCellDef [style.text-align]="justify">
26+
{{headerText}}
27+
</th>
28+
<td cdk-cell *cdkCellDef="let data" [style.text-align]="justify">
29+
{{getCellText(data)}}
30+
</td>
31+
</ng-container>
32+
`,
33+
})
34+
export class CdkTextColumn<T> implements OnDestroy, OnInit {
35+
/** Column name that should be used to reference this column. */
36+
@Input()
37+
get name(): string { return this._name; }
38+
set name(name: string) {
39+
this._name = name;
40+
this.columnDef.name = name;
41+
}
42+
_name: string;
43+
44+
/**
45+
* Text label that should be used for the column header. If this property is not
46+
* set, the header text will default to the column name with its first letter capitalized.
47+
*/
48+
@Input() headerText: string;
49+
50+
/**
51+
* Accessor function to retrieve the data should be provided to the cell. If this
52+
* property is not set, the data cells will assume that the column name is the same
53+
* as the data property the cells should display.
54+
*/
55+
@Input() dataAccessor: (data: T, name: string) => string;
56+
57+
/** Alignment of the cell values. */
58+
@Input() justify: 'start' | 'end' = 'start';
59+
60+
@ViewChild(CdkColumnDef) columnDef: CdkColumnDef;
61+
62+
constructor(@Optional() public table: CdkTable<T>) { }
63+
64+
ngOnInit() {
65+
// If there is no header text defined, set it to be the capitalized column name.
66+
if (this.headerText === undefined) {
67+
this.headerText = this.name.charAt(0).toUpperCase() + this.name.slice(1);
68+
}
69+
70+
if (this.table) {
71+
this.table.addColumnDef(this.columnDef);
72+
}
73+
}
74+
75+
ngOnDestroy() {
76+
if (this.table) {
77+
this.table.removeColumnDef(this.columnDef);
78+
}
79+
}
80+
81+
/**
82+
* Provides the cell text by using the data accessor function if one was provided, or access the
83+
* data property with the column name as the key.
84+
*/
85+
getCellText(data: T): any {
86+
if (this.dataAccessor) {
87+
return this.dataAccessor(data, this.name);
88+
}
89+
90+
return (data as any)[this.name];
91+
}
92+
}

src/dev-app/table/table-demo.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@ import {EXAMPLE_COMPONENTS} from '@angular/material-examples';
1212

1313
@Component({
1414
moduleId: module.id,
15-
template: '<material-example-list [ids]="examples"></material-example-list>',
15+
template: `
16+
<material-example-list [ids]="examples" [expandAll]="examples.length < 4">
17+
</material-example-list>
18+
`,
1619
})
1720
export class TableDemo {
1821
examples = Object.keys(EXAMPLE_COMPONENTS)
1922
.filter(example => example.startsWith('table-') || example.startsWith('cdk-table-'));
23+
24+
constructor() {
25+
this.examples = [
26+
'table-text-column-advanced',
27+
'table-text-column',
28+
];
29+
}
2030
}

src/lib/table/cell.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class MatCellDef extends CdkCellDef {}
3131
*/
3232
@Directive({
3333
selector: '[matHeaderCellDef]',
34+
exportAs: 'matHeaderCellDef',
3435
providers: [{provide: CdkHeaderCellDef, useExisting: MatHeaderCellDef}]
3536
})
3637
export class MatHeaderCellDef extends CdkHeaderCellDef {}
@@ -51,6 +52,7 @@ export class MatFooterCellDef extends CdkFooterCellDef {}
5152
*/
5253
@Directive({
5354
selector: '[matColumnDef]',
55+
exportAs: 'matColumnDef',
5456
providers: [
5557
{provide: CdkColumnDef, useExisting: MatColumnDef},
5658
{provide: 'MAT_SORT_HEADER_COLUMN_DEF', useExisting: MatColumnDef}

src/lib/table/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './cell';
1111
export * from './table';
1212
export * from './row';
1313
export * from './table-data-source';
14+
export * from './text-column';

src/lib/table/table-module.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
MatRow,
2727
MatRowDef
2828
} from './row';
29+
import {MatTextColumn} from './text-column';
2930
import {CommonModule} from '@angular/common';
3031
import {MatCommonModule} from '@angular/material/core';
3132

@@ -51,10 +52,16 @@ const EXPORTED_DECLARATIONS = [
5152
MatHeaderRow,
5253
MatRow,
5354
MatFooterRow,
55+
56+
MatTextColumn,
5457
];
5558

5659
@NgModule({
57-
imports: [CdkTableModule, CommonModule, MatCommonModule],
60+
imports: [
61+
CdkTableModule,
62+
CommonModule,
63+
MatCommonModule,
64+
],
5865
exports: EXPORTED_DECLARATIONS,
5966
declarations: EXPORTED_DECLARATIONS,
6067
})

0 commit comments

Comments
 (0)