Skip to content

Commit 33f77f3

Browse files
committed
refactor: convert columns to signal input
This is just an intermediate solution, so that we can turn the `columns` into a signal input. Since we internally mutate the columns, `_internalColumns` has to be a `linkedSignal`. We should consider alternative approaches, that only use computed.
1 parent 0e1469b commit 33f77f3

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed

projects/ngx-datatable/src/lib/components/datatable.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[innerWidth]="_innerWidth"
1010
[offsetX]="_offsetX"
1111
[dealsWithGroup]="groupedRows !== undefined"
12-
[columns]="_internalColumns"
12+
[columns]="_internalColumns()"
1313
[headerHeight]="headerHeight()"
1414
[reorderable]="reorderable()"
1515
[targetMarkerTemplate]="targetMarkerTemplate()"
@@ -46,7 +46,7 @@
4646
[rowCount]="rowCount"
4747
[offset]="offset"
4848
[trackByProp]="trackByProp()"
49-
[columns]="_internalColumns"
49+
[columns]="_internalColumns()"
5050
[pageSize]="pageSize"
5151
[offsetX]="_offsetX"
5252
[rowDetail]="rowDetail"

projects/ngx-datatable/src/lib/components/datatable.component.ts

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Input,
1717
IterableDiffer,
1818
IterableDiffers,
19+
linkedSignal,
1920
model,
2021
numberAttribute,
2122
OnDestroy,
@@ -171,21 +172,7 @@ export class DatatableComponent<TRow extends Row = any>
171172
/**
172173
* Columns to be displayed.
173174
*/
174-
@Input() set columns(val: TableColumn[]) {
175-
if (val) {
176-
this._internalColumns = toInternalColumn(val, this._defaultColumnWidth);
177-
this.recalculateColumns();
178-
}
179-
180-
this._columns = val;
181-
}
182-
183-
/**
184-
* Get the columns.
185-
*/
186-
get columns(): TableColumn[] {
187-
return this._columns;
188-
}
175+
readonly columns = input<TableColumn[]>();
189176

190177
/**
191178
* List of row objects that should be
@@ -706,8 +693,17 @@ export class DatatableComponent<TRow extends Row = any>
706693
_rows: (TRow | undefined)[] = [];
707694
_groupRowsBy?: keyof TRow;
708695
_internalRows: (TRow | undefined)[] = [];
709-
_internalColumns!: TableColumnInternal<TRow>[];
710-
_columns!: TableColumn[];
696+
// TODO: consider removing internal modifications of the columns.
697+
// This requires a different strategy for certain properties like width.
698+
readonly _internalColumns = linkedSignal(() =>
699+
this.recalculateColumns(
700+
toInternalColumn(
701+
this.columnTemplates().length
702+
? this.columnTemplates().map(c => c.column())
703+
: this.columns()!
704+
)
705+
)
706+
);
711707
_subscriptions: Subscription[] = [];
712708
_defaultColumnWidth = this.configuration?.defaultColumnWidth ?? 150;
713709
/**
@@ -750,7 +746,7 @@ export class DatatableComponent<TRow extends Row = any>
750746
this.limit();
751747
this.count();
752748
// Recalculate without tracking other signals
753-
untracked(() => this.recalculate());
749+
untracked(() => this.recalculateDims());
754750
});
755751
}
756752

@@ -772,7 +768,11 @@ export class DatatableComponent<TRow extends Row = any>
772768
const rowDiffers = this.rowDiffer.diff(this.rows);
773769
if (rowDiffers || this.disableRowCheck()) {
774770
// we don't sort again when ghost loader adds a dummy row
775-
if (!this.ghostLoadingIndicator() && !this.externalSorting() && this._internalColumns) {
771+
if (
772+
!this.ghostLoadingIndicator() &&
773+
!this.externalSorting() &&
774+
this._internalColumns().length
775+
) {
776776
this.sortInternalRows();
777777
} else {
778778
this._internalRows = [...this.rows];
@@ -850,8 +850,6 @@ export class DatatableComponent<TRow extends Row = any>
850850
*/
851851
translateColumns(columns: TableColumn<TRow>[]) {
852852
if (columns.length) {
853-
this._internalColumns = toInternalColumn(columns, this._defaultColumnWidth);
854-
this.recalculateColumns();
855853
if (!this.externalSorting() && this.rows?.length) {
856854
this.sortInternalRows();
857855
}
@@ -905,7 +903,7 @@ export class DatatableComponent<TRow extends Row = any>
905903
*/
906904
recalculate(): void {
907905
this.recalculateDims();
908-
this.recalculateColumns();
906+
this._internalColumns.set(this.recalculateColumns(this._internalColumns().slice()));
909907
this.cd.markForCheck();
910908
}
911909

@@ -923,13 +921,13 @@ export class DatatableComponent<TRow extends Row = any>
923921
* distribution mode and scrollbar offsets.
924922
*/
925923
recalculateColumns(
926-
columns: TableColumnInternal[] = this._internalColumns,
924+
columns: TableColumnInternal[],
927925
forceIdx = -1,
928926
allowBleed: boolean = this.scrollbarH()
929-
): TableColumn[] | undefined {
927+
): TableColumnInternal[] {
930928
let width = this._innerWidth;
931-
if (!columns || !width) {
932-
return undefined;
929+
if (!width) {
930+
return [];
933931
}
934932
const bodyElement = this.bodyElement?.nativeElement;
935933
this.verticalScrollVisible = bodyElement?.scrollHeight > bodyElement?.clientHeight;
@@ -952,17 +950,6 @@ export class DatatableComponent<TRow extends Row = any>
952950
adjustColumnWidths(columns, width);
953951
}
954952

955-
// The type of width is wrong here.
956-
// It can also be undefined, thus the eslint-rule must be disabled.
957-
if (this.bodyComponent && this.bodyComponent.columnGroupWidths().total !== width) {
958-
this._internalColumns = [...this._internalColumns];
959-
this.bodyComponent.cd.markForCheck();
960-
}
961-
962-
if (this.headerComponent && this.headerComponent._columnGroupWidths().total !== width) {
963-
this._internalColumns = [...this._internalColumns];
964-
}
965-
966953
return columns;
967954
}
968955

@@ -1119,14 +1106,14 @@ export class DatatableComponent<TRow extends Row = any>
11191106
return;
11201107
}
11211108

1122-
const idx = this._internalColumns.indexOf(column);
1123-
const cols = this._internalColumns.map(col => ({ ...col }));
1109+
const idx = this._internalColumns().indexOf(column);
1110+
const cols = this._internalColumns().map(col => ({ ...col }));
11241111
cols[idx].width = newValue;
11251112
// set this so we can force the column
11261113
// width distribution to be to this value
11271114
cols[idx].$$oldWidth = newValue;
11281115
this.recalculateColumns(cols, idx);
1129-
this._internalColumns = cols;
1116+
this._internalColumns.set(this.recalculateColumns(cols));
11301117

11311118
this.resize.emit({
11321119
column,
@@ -1141,16 +1128,16 @@ export class DatatableComponent<TRow extends Row = any>
11411128
}
11421129
column.width = newValue;
11431130
column.$$oldWidth = newValue;
1144-
const idx = this._internalColumns.indexOf(column);
1145-
this.recalculateColumns(this._internalColumns, idx);
1131+
const idx = this._internalColumns().indexOf(column);
1132+
this._internalColumns.set(this.recalculateColumns(this._internalColumns().slice(), idx));
11461133
}
11471134

11481135
/**
11491136
* The header triggered a column re-order event.
11501137
*/
11511138
onColumnReorder(event: ReorderEventInternal): void {
11521139
const { column, newValue, prevValue } = event;
1153-
const cols = this._internalColumns.map(c => ({ ...c }));
1140+
const cols = this._internalColumns().map(c => ({ ...c }));
11541141
const prevCol = cols[newValue];
11551142
if (column.frozenLeft !== prevCol.frozenLeft || column.frozenRight !== prevCol.frozenRight) {
11561143
return;
@@ -1175,7 +1162,7 @@ export class DatatableComponent<TRow extends Row = any>
11751162
}
11761163
}
11771164

1178-
this._internalColumns = cols;
1165+
this._internalColumns.set(cols);
11791166

11801167
this.reorder.emit(event);
11811168
}
@@ -1305,13 +1292,13 @@ export class DatatableComponent<TRow extends Row = any>
13051292
this.groupedRows = this.groupArrayBy(this._rows, this._groupRowsBy!);
13061293
this.groupedRows = sortGroupedRows(
13071294
this.groupedRows,
1308-
this._internalColumns,
1295+
this._internalColumns(),
13091296
this.sorts(),
13101297
sortOnGroupHeader
13111298
);
13121299
this._internalRows = [...this._internalRows];
13131300
} else {
1314-
this._internalRows = sortRows(this._internalRows, this._internalColumns, this.sorts());
1301+
this._internalRows = sortRows(this._internalRows, this._internalColumns(), this.sorts());
13151302
}
13161303
}
13171304
}

0 commit comments

Comments
 (0)