Skip to content

Commit f486aba

Browse files
committed
update
1 parent 685e636 commit f486aba

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

src/cdk/table/row.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export abstract class BaseRowDef {
4848
}
4949

5050
/**
51-
* Returns the difference between the current columns and the collumns from the last diff, or null
51+
* Returns the difference between the current columns and the columns from the last diff, or null
5252
* if there is no difference.
5353
*/
5454
getColumnsDiff(): IterableChanges<any> | null {

src/cdk/table/table.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {combineLatest} from 'rxjs/observable/combineLatest';
88
import {CdkTableModule} from './index';
99
import {map} from 'rxjs/operator/map';
1010

11-
describe('CdkTable', () => {
11+
fdescribe('CdkTable', () => {
1212
let fixture: ComponentFixture<SimpleCdkTableApp>;
1313

1414
let component: SimpleCdkTableApp;
@@ -571,6 +571,40 @@ class TrackByCdkTableApp {
571571
}
572572
}
573573

574+
@Component({
575+
template: `
576+
<cdk-table [dataSource]="dataSource">
577+
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
578+
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
579+
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
580+
</ng-container>
581+
582+
<cdk-header-row *cdkHeaderRowDef="dynamicColumnIds"></cdk-header-row>
583+
<cdk-row *cdkRowDef="let row; columns: dynamicColumnIds;"></cdk-row>
584+
</cdk-table>
585+
`
586+
})
587+
class DynamicColumnDefinitionsCdkTableApp {
588+
dynamicColumnDefs: any[] = [];
589+
dynamicColumnIds: string[] = [];
590+
591+
dataSource: FakeDataSource = new FakeDataSource();
592+
columnsToRender = ['column_a', 'column_b'];
593+
594+
@ViewChild(CdkTable) table: CdkTable<TestData>;
595+
596+
addDynamicColumnDef() {
597+
const nextProperty = this.dynamicColumnDefs.length;
598+
this.dynamicColumnDefs.push({
599+
id: nextProperty,
600+
property: nextProperty,
601+
headerText: nextProperty
602+
});
603+
604+
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
605+
}
606+
}
607+
574608
@Component({
575609
template: `
576610
<cdk-table [dataSource]="dataSource" role="treegrid">

src/cdk/table/table.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {BehaviorSubject} from 'rxjs/BehaviorSubject';
3636
import {Subscription} from 'rxjs/Subscription';
3737
import {Subject} from 'rxjs/Subject';
3838
import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
39+
import {startWith} from 'rxjs/operator/startWith';
3940

4041
/**
4142
* Returns an error to be thrown when attempting to find an unexisting column.
@@ -46,6 +47,14 @@ export function getTableUnknownColumnError(id: string) {
4647
return new Error(`cdk-table: Could not find column with id "${id}".`);
4748
}
4849

50+
/**
51+
* Returns an error to be thrown when attempting to find an unexisting column.
52+
* @docs-private
53+
*/
54+
export function getTableDuplicateColumnNameError(name: string) {
55+
return new Error(`cdk-table: Duplicate column definition name provided: "${name}".`);
56+
}
57+
4958
/**
5059
* Provides a handle for the table to grab the view container's ng-container to insert data rows.
5160
* @docs-private
@@ -179,8 +188,10 @@ export class CdkTable<T> implements CollectionViewer {
179188
}
180189

181190
ngAfterContentInit() {
182-
this._updateColumnDefinitions();
183-
this._columnDefinitions.changes.subscribe(() => this._updateColumnDefinitions());
191+
// Update the content's column definitions when they are changed. Start with initial null value
192+
// to initialize the map.
193+
startWith.call(this._columnDefinitions.changes, null)
194+
.subscribe(() => this._updateColumnDefinitions());
184195
}
185196

186197
ngAfterContentChecked() {
@@ -189,7 +200,10 @@ export class CdkTable<T> implements CollectionViewer {
189200

190201
ngAfterViewInit() {
191202
this._isViewInitialized = true;
192-
if (this.dataSource && !this._renderChangeSubscription) {
203+
}
204+
205+
ngDoCheck() {
206+
if (this._isViewInitialized && this.dataSource && !this._renderChangeSubscription) {
193207
this._observeRenderChanges();
194208
}
195209
}
@@ -204,8 +218,11 @@ export class CdkTable<T> implements CollectionViewer {
204218

205219
/** Update the map containing the content's column definitions. */
206220
private _updateColumnDefinitions() {
207-
// TODO(andrewseguin): Throw an error if two columns share the same name
221+
this._columnDefinitionsByName.clear();
208222
this._columnDefinitions.forEach(columnDef => {
223+
if (this._columnDefinitionsByName.has(columnDef.name)) {
224+
throw getTableDuplicateColumnNameError(columnDef.name);
225+
}
209226
this._columnDefinitionsByName.set(columnDef.name, columnDef);
210227
});
211228
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class TableDemo {
4747
property: nextProperty,
4848
headerText: nextProperty
4949
});
50+
5051
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
5152
}
5253

0 commit comments

Comments
 (0)