Skip to content

Commit 9109f90

Browse files
committed
refactor: convert offset into a signal model
1 parent 9e76884 commit 9109f90

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
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
@@ -43,7 +43,7 @@
4343
[externalPaging]="externalPaging()"
4444
[rowHeight]="rowHeight()"
4545
[rowCount]="rowCount()"
46-
[offset]="offset"
46+
[offset]="correctedOffset()"
4747
[trackByProp]="trackByProp()"
4848
[columns]="_internalColumns()"
4949
[pageSize]="pageSize()"
@@ -97,7 +97,7 @@
9797
[rowCount]="_internalGroupedRows() !== undefined ? _internalRows().length : rowCount()"
9898
[groupCount]="_internalGroupedRows() !== undefined ? rowCount() : undefined"
9999
[pageSize]="pageSize()"
100-
[offset]="offset"
100+
[offset]="correctedOffset()"
101101
[footerHeight]="footerHeight()"
102102
[footerTemplate]="_footer()"
103103
[totalMessage]="messages().totalMessage ?? 'total'"

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
HostListener,
1616
inject,
1717
input,
18-
Input,
1918
IterableDiffer,
2019
IterableDiffers,
2120
linkedSignal,
@@ -230,12 +229,7 @@ export class DatatableComponent<TRow extends Row = any>
230229
* The current offset ( page - 1 ) shown.
231230
* Default value: `0`
232231
*/
233-
@Input({ transform: numberAttribute }) set offset(val: number) {
234-
this._offset = val;
235-
}
236-
get offset(): number {
237-
return Math.max(Math.min(this._offset, Math.ceil(this.rowCount() / this.pageSize()) - 1), 0);
238-
}
232+
readonly offset = model<number>(0);
239233

240234
/**
241235
* Show the linear loading bar.
@@ -667,7 +661,6 @@ export class DatatableComponent<TRow extends Row = any>
667661
private readonly _rowDiffCount = signal(0);
668662

669663
_offsetX = 0;
670-
_offset = 0;
671664
readonly _internalRows = computed(() => {
672665
this._rowDiffCount(); // to trigger recalculation when row differ detects a change
673666
let rows = this.rows()?.slice() ?? [];
@@ -734,6 +727,18 @@ export class DatatableComponent<TRow extends Row = any>
734727
)
735728
)
736729
);
730+
731+
/**
732+
* Computed signal that returns the corrected offset value.
733+
* It ensures the offset is within valid bounds based on rowCount and pageSize.
734+
*/
735+
readonly correctedOffset = computed(() => {
736+
const offset = this.offset();
737+
const rowCount = this.rowCount();
738+
const pageSize = this.pageSize();
739+
return Math.max(Math.min(offset, Math.ceil(rowCount / pageSize) - 1), 0);
740+
});
741+
737742
_subscriptions: Subscription[] = [];
738743
_defaultColumnWidth = this.configuration?.defaultColumnWidth ?? 150;
739744
/**
@@ -952,14 +957,14 @@ export class DatatableComponent<TRow extends Row = any>
952957
return;
953958
}
954959

955-
this.offset = offset;
960+
this.offset.set(offset);
956961

957-
if (!isNaN(this.offset)) {
962+
if (!isNaN(this.correctedOffset())) {
958963
this.page.emit({
959964
count: this.count(),
960965
pageSize: this.pageSize(),
961966
limit: this.limit(),
962-
offset: this.offset,
967+
offset: this.correctedOffset(),
963968
sorts: this.sorts()
964969
});
965970
}
@@ -977,14 +982,14 @@ export class DatatableComponent<TRow extends Row = any>
977982
* The footer triggered a page event.
978983
*/
979984
onFooterPage(event: PagerPageEvent) {
980-
this.offset = event.page - 1;
981-
this._bodyComponent().updateOffsetY(this.offset);
985+
this.offset.set(event.page - 1);
986+
this._bodyComponent().updateOffsetY(this.correctedOffset());
982987

983988
this.page.emit({
984989
count: this.count(),
985990
pageSize: this.pageSize(),
986991
limit: this.limit(),
987-
offset: this.offset,
992+
offset: this.correctedOffset(),
988993
sorts: this.sorts()
989994
});
990995

@@ -1132,14 +1137,14 @@ export class DatatableComponent<TRow extends Row = any>
11321137
this.sorts.set(event.sorts);
11331138

11341139
// Always go to first page when sorting to see the newly sorted data
1135-
this.offset = 0;
1136-
this._bodyComponent().updateOffsetY(this.offset);
1140+
this.offset.set(0);
1141+
this._bodyComponent().updateOffsetY(this.correctedOffset());
11371142
// Emit the page object with updated offset value
11381143
this.page.emit({
11391144
count: this.count(),
11401145
pageSize: this.pageSize(),
11411146
limit: this.limit(),
1142-
offset: this.offset,
1147+
offset: this.correctedOffset(),
11431148
sorts: this.sorts()
11441149
});
11451150
this.sort.emit(event);

src/app/basic/filter.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ export class FilterComponent {
6868
return d.name.toLowerCase().includes(val) || !val;
6969
});
7070
// Whenever the filter changes, always go back to the first page
71-
this.table.offset = 0;
71+
this.table.offset.set(0);
7272
}
7373
}

0 commit comments

Comments
 (0)