Skip to content

Commit 53b4966

Browse files
committed
refactor: use signal compute for indexes in the body
1 parent c14135b commit 53b4966

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
344344
return this.updateRows();
345345
});
346346
readonly rowHeightsCache = computed(() => this.computeRowHeightsCache());
347-
offsetY = 0;
348-
readonly indexes = signal<{ first: number; last: number }>({ first: 0, last: 0 });
347+
readonly offsetY = signal(0);
348+
readonly indexes = computed(() => this.computeIndexes());
349349
readonly columnGroupWidths = computed(() => {
350350
const colsByPin = columnsByPin(this.columns());
351351
return columnGroupWidths(colsByPin, this.columns());
@@ -398,16 +398,11 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
398398

399399
ngOnChanges(changes: SimpleChanges): void {
400400
if (changes.bodyHeight || changes.rows || changes.rowCount || changes.pageSize) {
401-
this.recalcLayout();
402401
if (changes.pageSize) {
403402
this._offsetEvent = -1;
404403
this.updatePage('up');
405404
this.updatePage('down');
406405
}
407-
} else if (changes.offset) {
408-
if (!this.scrollbarV() || (this.scrollbarV() && !this.virtualization())) {
409-
this.recalcLayout();
410-
}
411406
}
412407
}
413408

@@ -451,7 +446,6 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
451446
}
452447

453448
// Refresh rows after toggle
454-
this.updateIndexes();
455449
this.cd.markForCheck();
456450
}
457451

@@ -464,7 +458,6 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
464458
}
465459

466460
// Refresh rows after toggle
467-
this.updateIndexes();
468461
this.cd.markForCheck();
469462
}
470463

@@ -499,17 +492,16 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
499492

500493
// if scroll change, trigger update
501494
// this is mainly used for header cell positions
502-
if (this.offsetY !== scrollYPos || this.offsetX() !== scrollXPos) {
495+
if (this.offsetY() !== scrollYPos || this.offsetX() !== scrollXPos) {
503496
this.scroll.emit({
504497
offsetY: scrollYPos,
505498
offsetX: scrollXPos
506499
});
507500
}
508501

509-
this.offsetY = scrollYPos;
502+
this.offsetY.set(scrollYPos);
510503
this.offsetX.set(scrollXPos);
511504

512-
this.updateIndexes();
513505
this.updatePage(event.direction);
514506
this.cd.detectChanges();
515507
}
@@ -613,7 +605,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
613605
/**
614606
* Updates the index of the rows in the viewport
615607
*/
616-
updateIndexes(): void {
608+
computeIndexes(): { first: number; last: number } {
617609
let first = 0;
618610
let last = this.rowCount();
619611

@@ -623,8 +615,8 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
623615
// scrollY position would be at. The last index would be the one
624616
// that shows up inside the view port the last.
625617
const height = parseInt(this._bodyHeight(), 10);
626-
first = this.rowHeightsCache().getRowIndex(this.offsetY);
627-
last = this.rowHeightsCache().getRowIndex(height + this.offsetY) + 1;
618+
first = this.rowHeightsCache().getRowIndex(this.offsetY());
619+
last = this.rowHeightsCache().getRowIndex(height + this.offsetY()) + 1;
628620
}
629621
} else {
630622
// The server is handling paging and will pass an array that begins with the
@@ -635,7 +627,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
635627
last = Math.min(first + this.pageSize(), this.rowCount());
636628
}
637629

638-
this.indexes.set({ first, last });
630+
return { first, last };
639631
}
640632

641633
/**
@@ -705,30 +697,13 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
705697
toggleAllRows(expanded: boolean): void {
706698
// TODO requires fixing. This still does not work with groups.
707699
this.rowExpansions.set(expanded ? [...(this.rows() as any)] : []);
708-
709-
if (this.scrollbarV()) {
710-
// Refresh the full row heights cache since every row was affected.
711-
this.recalcLayout();
712-
}
713700
}
714701

715702
/**
716703
* Expand/Collapse all the groups no matter what their state is.
717704
*/
718705
toggleAllGroups(expanded: boolean): void {
719706
this.groupExpansions.set(expanded ? [...this.groupedRows()!] : []);
720-
721-
if (this.scrollbarV()) {
722-
// Refresh the full row heights cache since every row was affected.
723-
this.recalcLayout();
724-
}
725-
}
726-
727-
/**
728-
* Recalculates the table
729-
*/
730-
recalcLayout(): void {
731-
this.updateIndexes();
732707
}
733708

734709
/**

0 commit comments

Comments
 (0)