Skip to content

Commit 54c66bc

Browse files
authored
fix(cdk/table): add fallback code if view repeater isn't provided (#20604)
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.
1 parent 5562788 commit 54c66bc

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/cdk/table/table.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -603,21 +603,38 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
603603
}
604604
const viewContainer = this._rowOutlet.viewContainer;
605605

606-
// @breaking-change 11.0.0 Remove null check for `_viewRepeater`
607-
// once it's a required parameter in the constructor.
608-
this._viewRepeater?.applyChanges(
609-
changes,
610-
viewContainer,
611-
(record: IterableChangeRecord<RenderRow<T>>,
612-
_adjustedPreviousIndex: number|null,
613-
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
614-
(record) => record.item.data,
615-
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
616-
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
617-
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
606+
// @breaking-change 11.0.0 Remove null check for `_viewRepeater` and the
607+
// `else` clause once `_viewRepeater` is turned into a required parameter.
608+
if (this._viewRepeater) {
609+
this._viewRepeater.applyChanges(
610+
changes,
611+
viewContainer,
612+
(record: IterableChangeRecord<RenderRow<T>>,
613+
_adjustedPreviousIndex: number|null,
614+
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
615+
(record) => record.item.data,
616+
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
617+
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
618+
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
619+
}
620+
});
621+
} else {
622+
changes.forEachOperation(
623+
(record: IterableChangeRecord<RenderRow<T>>, prevIndex: number|null,
624+
currentIndex: number|null) => {
625+
if (record.previousIndex == null) {
626+
const renderRow = record.item;
627+
const rowDef = renderRow.rowDef;
628+
const context: RowContext<T> = {$implicit: renderRow.data};
629+
this._renderRow(this._rowOutlet, rowDef, currentIndex!, context);
630+
} else if (currentIndex == null) {
631+
viewContainer.remove(prevIndex!);
632+
} else {
633+
const view = <RowViewRef<T>>viewContainer.get(prevIndex!);
634+
viewContainer.move(view!, currentIndex);
618635
}
619-
}
620-
);
636+
});
637+
}
621638

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

0 commit comments

Comments
 (0)