Skip to content

Commit a47b961

Browse files
committed
update
1 parent b046af2 commit a47b961

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/cdk/table/row.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export abstract class BaseRowDef {
5050
}
5151

5252
/**
53-
* Returns the difference between the current columns and the collumns from the last diff, or null
53+
* Returns the difference between the current columns and the columns from the last diff, or null
5454
* if there is no difference.
5555
*/
5656
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;
@@ -587,6 +587,40 @@ class TrackByCdkTableApp {
587587
}
588588
}
589589

590+
@Component({
591+
template: `
592+
<cdk-table [dataSource]="dataSource">
593+
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
594+
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
595+
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
596+
</ng-container>
597+
598+
<cdk-header-row *cdkHeaderRowDef="dynamicColumnIds"></cdk-header-row>
599+
<cdk-row *cdkRowDef="let row; columns: dynamicColumnIds;"></cdk-row>
600+
</cdk-table>
601+
`
602+
})
603+
class DynamicColumnDefinitionsCdkTableApp {
604+
dynamicColumnDefs: any[] = [];
605+
dynamicColumnIds: string[] = [];
606+
607+
dataSource: FakeDataSource = new FakeDataSource();
608+
columnsToRender = ['column_a', 'column_b'];
609+
610+
@ViewChild(CdkTable) table: CdkTable<TestData>;
611+
612+
addDynamicColumnDef() {
613+
const nextProperty = this.dynamicColumnDefs.length;
614+
this.dynamicColumnDefs.push({
615+
id: nextProperty,
616+
property: nextProperty,
617+
headerText: nextProperty
618+
});
619+
620+
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
621+
}
622+
}
623+
590624
@Component({
591625
template: `
592626
<cdk-table [dataSource]="dataSource" role="treegrid">

src/cdk/table/table.ts

Lines changed: 18 additions & 1 deletion
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
@@ -175,6 +184,11 @@ export class CdkTable<T> implements CollectionViewer {
175184
this._dataDiffer = this._differs.find([]).create(this._trackByFn);
176185
}
177186

187+
ngAfterContentInit() {
188+
this._updateColumnDefinitions();
189+
this._columnDefinitions.changes.subscribe(() => this._updateColumnDefinitions());
190+
}
191+
178192
ngAfterContentChecked() {
179193
this._updateColumnDefinitions();
180194
}
@@ -210,8 +224,11 @@ export class CdkTable<T> implements CollectionViewer {
210224

211225
/** Update the map containing the content's column definitions. */
212226
private _updateColumnDefinitions() {
213-
// TODO(andrewseguin): Throw an error if two columns share the same name
227+
this._columnDefinitionsByName.clear();
214228
this._columnDefinitions.forEach(columnDef => {
229+
if (this._columnDefinitionsByName.has(columnDef.name)) {
230+
throw getTableDuplicateColumnNameError(columnDef.name);
231+
}
215232
this._columnDefinitionsByName.set(columnDef.name, columnDef);
216233
});
217234
}

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)