Skip to content

Commit 6be19ab

Browse files
committed
refactor: use signals for dimensions
1 parent fc36697 commit 6be19ab

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[sorts]="sorts()"
77
[sortType]="sortType()"
88
[scrollbarH]="scrollbarH()"
9-
[innerWidth]="_innerWidth"
9+
[innerWidth]="_innerWidth()"
1010
[offsetX]="_offsetX"
1111
[dealsWithGroup]="groupedRows !== undefined"
1212
[columns]="_internalColumns()"
@@ -51,8 +51,8 @@
5151
[offsetX]="_offsetX"
5252
[rowDetail]="rowDetail"
5353
[groupHeader]="groupHeader"
54-
[innerWidth]="_innerWidth"
55-
[bodyHeight]="bodyHeight"
54+
[innerWidth]="_innerWidth()"
55+
[bodyHeight]="bodyHeight()"
5656
[selectionType]="selectionType()"
5757
[rowIdentity]="rowIdentity"
5858
[rowClass]="rowClass()"

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,18 @@ export class DatatableComponent<TRow extends Row = any>
667667
}
668668

669669
element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
670-
_innerWidth!: number;
670+
readonly _innerWidth = computed(() => this.dimensions().width);
671671
pageSize!: number;
672-
bodyHeight!: number;
672+
readonly bodyHeight = computed(() => {
673+
if (this.scrollbarV()) {
674+
let height = this.dimensions().height;
675+
if (this.headerElement) {
676+
height = height - this.headerElement.nativeElement.getBoundingClientRect().height;
677+
}
678+
return height - this.footerHeight();
679+
}
680+
return 0;
681+
});
673682
rowCount = 0;
674683
rowDiffer: IterableDiffer<TRow | undefined> = inject(IterableDiffers).find([]).create();
675684
/** This counter is increased, when the rowDiffer detects a change. This will cause an update of _internalRows. */
@@ -728,6 +737,7 @@ export class DatatableComponent<TRow extends Row = any>
728737
// this makes horizontal scroll to appear on load even if columns can fit in view
729738
// this will be set to true once rows are available and rendered on UI
730739
private readonly _rowInitDone = signal(false);
740+
private readonly dimensions = signal<Pick<DOMRect, 'width' | 'height'>>({ height: 0, width: 0 });
731741

732742
constructor() {
733743
// TODO: This should be a computed signal.
@@ -902,7 +912,7 @@ export class DatatableComponent<TRow extends Row = any>
902912
forceIdx = -1,
903913
allowBleed: boolean = this.scrollbarH()
904914
): TableColumnInternal[] {
905-
let width = this._innerWidth;
915+
let width = this._innerWidth();
906916
if (!width) {
907917
return [];
908918
}
@@ -937,18 +947,7 @@ export class DatatableComponent<TRow extends Row = any>
937947
*/
938948
recalculateDims(): void {
939949
const dims = this.element.getBoundingClientRect();
940-
this._innerWidth = Math.floor(dims.width);
941-
942-
if (this.scrollbarV()) {
943-
let height = dims.height;
944-
if (this.headerElement) {
945-
height = height - this.headerElement.nativeElement.getBoundingClientRect().height;
946-
}
947-
if (this.footerHeight()) {
948-
height = height - this.footerHeight();
949-
}
950-
this.bodyHeight = height;
951-
}
950+
this.dimensions.set(dims);
952951

953952
this.recalculatePages();
954953
}
@@ -1024,7 +1023,7 @@ export class DatatableComponent<TRow extends Row = any>
10241023
// This is because an expanded row is still considered to be a child of
10251024
// the original row. Hence calculation would use rowHeight only.
10261025
if (this.scrollbarV() && this.virtualization()) {
1027-
const size = Math.ceil(this.bodyHeight / (this.rowHeight() as number));
1026+
const size = Math.ceil(this.bodyHeight() / (this.rowHeight() as number));
10281027
return Math.max(size, 0);
10291028
}
10301029

0 commit comments

Comments
 (0)