Skip to content

Commit 49d1760

Browse files
spike-rabbitfh1ch
authored andcommitted
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 49d1760

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
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: 34 additions & 47 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,18 @@ 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+
this._defaultColumnWidth
705+
)
706+
)
707+
);
711708
_subscriptions: Subscription[] = [];
712709
_defaultColumnWidth = this.configuration?.defaultColumnWidth ?? 150;
713710
/**
@@ -750,7 +747,7 @@ export class DatatableComponent<TRow extends Row = any>
750747
this.limit();
751748
this.count();
752749
// Recalculate without tracking other signals
753-
untracked(() => this.recalculate());
750+
untracked(() => this.recalculateDims());
754751
});
755752
}
756753

@@ -772,7 +769,11 @@ export class DatatableComponent<TRow extends Row = any>
772769
const rowDiffers = this.rowDiffer.diff(this.rows);
773770
if (rowDiffers || this.disableRowCheck()) {
774771
// we don't sort again when ghost loader adds a dummy row
775-
if (!this.ghostLoadingIndicator() && !this.externalSorting() && this._internalColumns) {
772+
if (
773+
!this.ghostLoadingIndicator() &&
774+
!this.externalSorting() &&
775+
this._internalColumns().length
776+
) {
776777
this.sortInternalRows();
777778
} else {
778779
this._internalRows = [...this.rows];
@@ -850,8 +851,6 @@ export class DatatableComponent<TRow extends Row = any>
850851
*/
851852
translateColumns(columns: TableColumn<TRow>[]) {
852853
if (columns.length) {
853-
this._internalColumns = toInternalColumn(columns, this._defaultColumnWidth);
854-
this.recalculateColumns();
855854
if (!this.externalSorting() && this.rows?.length) {
856855
this.sortInternalRows();
857856
}
@@ -905,7 +904,7 @@ export class DatatableComponent<TRow extends Row = any>
905904
*/
906905
recalculate(): void {
907906
this.recalculateDims();
908-
this.recalculateColumns();
907+
this._internalColumns.set(this.recalculateColumns(this._internalColumns().slice()));
909908
this.cd.markForCheck();
910909
}
911910

@@ -923,13 +922,13 @@ export class DatatableComponent<TRow extends Row = any>
923922
* distribution mode and scrollbar offsets.
924923
*/
925924
recalculateColumns(
926-
columns: TableColumnInternal[] = this._internalColumns,
925+
columns: TableColumnInternal[],
927926
forceIdx = -1,
928927
allowBleed: boolean = this.scrollbarH()
929-
): TableColumn[] | undefined {
928+
): TableColumnInternal[] {
930929
let width = this._innerWidth;
931-
if (!columns || !width) {
932-
return undefined;
930+
if (!width) {
931+
return [];
933932
}
934933
const bodyElement = this.bodyElement?.nativeElement;
935934
this.verticalScrollVisible = bodyElement?.scrollHeight > bodyElement?.clientHeight;
@@ -952,17 +951,6 @@ export class DatatableComponent<TRow extends Row = any>
952951
adjustColumnWidths(columns, width);
953952
}
954953

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-
966954
return columns;
967955
}
968956

@@ -1119,14 +1107,13 @@ export class DatatableComponent<TRow extends Row = any>
11191107
return;
11201108
}
11211109

1122-
const idx = this._internalColumns.indexOf(column);
1123-
const cols = this._internalColumns.map(col => ({ ...col }));
1110+
const idx = this._internalColumns().indexOf(column);
1111+
const cols = this._internalColumns().map(col => ({ ...col }));
11241112
cols[idx].width = newValue;
11251113
// set this so we can force the column
11261114
// width distribution to be to this value
11271115
cols[idx].$$oldWidth = newValue;
1128-
this.recalculateColumns(cols, idx);
1129-
this._internalColumns = cols;
1116+
this._internalColumns.set(this.recalculateColumns(cols, idx));
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)