Skip to content

Commit

Permalink
fix(cdk/table): add fallback code if view repeater isn't provided (#2…
Browse files Browse the repository at this point in the history
…0604)

The `_viewRepeater` parameter is currently optional due to our breaking change policy,
however if it isn't provided, the table is basically a no-op. These changes add the old
rendering logic while we have to support both cases.

Fixes #20601.

(cherry picked from commit 54c66bc)
  • Loading branch information
crisbeto authored and annieyw committed Sep 23, 2020
1 parent 078b1a3 commit bfb7e57
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,38 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
}
const viewContainer = this._rowOutlet.viewContainer;

// @breaking-change 11.0.0 Remove null check for `_viewRepeater`
// once it's a required parameter in the constructor.
this._viewRepeater?.applyChanges(
changes,
viewContainer,
(record: IterableChangeRecord<RenderRow<T>>,
_adjustedPreviousIndex: number|null,
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
(record) => record.item.data,
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
// @breaking-change 11.0.0 Remove null check for `_viewRepeater` and the
// `else` clause once `_viewRepeater` is turned into a required parameter.
if (this._viewRepeater) {
this._viewRepeater.applyChanges(
changes,
viewContainer,
(record: IterableChangeRecord<RenderRow<T>>,
_adjustedPreviousIndex: number|null,
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
(record) => record.item.data,
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
}
});
} else {
changes.forEachOperation(
(record: IterableChangeRecord<RenderRow<T>>, prevIndex: number|null,
currentIndex: number|null) => {
if (record.previousIndex == null) {
const renderRow = record.item;
const rowDef = renderRow.rowDef;
const context: RowContext<T> = {$implicit: renderRow.data};
this._renderRow(this._rowOutlet, rowDef, currentIndex!, context);
} else if (currentIndex == null) {
viewContainer.remove(prevIndex!);
} else {
const view = <RowViewRef<T>>viewContainer.get(prevIndex!);
viewContainer.move(view!, currentIndex);
}
}
);
});
}

// Update the meta context of a row's context data (index, count, first, last, ...)
this._updateRowIndexContext();
Expand Down

0 comments on commit bfb7e57

Please sign in to comment.