Skip to content

Commit fc3defa

Browse files
spike-rabbitfh1ch
authored andcommitted
refactor: use signal compute for indexes in the body
1 parent 4076829 commit fc3defa

File tree

2 files changed

+8
-40
lines changed

2 files changed

+8
-40
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ describe('DataTableBodyComponent', () => {
5959
fixture.componentRef.setInput('offset', 1);
6060
fixture.componentRef.setInput('rowCount', 20);
6161
const expectedIndexes = { first: 10, last: 20 };
62-
component.updateIndexes();
6362
expect(component.indexes()).toEqual(expectedIndexes);
6463
});
6564

@@ -70,7 +69,6 @@ describe('DataTableBodyComponent', () => {
7069
fixture.componentRef.setInput('offset', 1);
7170
fixture.componentRef.setInput('rowCount', 9);
7271
const expectedIndexes = { first: 5, last: 9 };
73-
component.updateIndexes();
7472
expect(component.indexes()).toEqual(expectedIndexes);
7573
});
7674

@@ -92,7 +90,6 @@ describe('DataTableBodyComponent', () => {
9290
fixture.componentRef.setInput('offset', 1);
9391
fixture.componentRef.setInput('rowCount', 20);
9492
const expectedIndexes = { first: 0, last: 10 };
95-
component.updateIndexes();
9693
expect(component.indexes()).toEqual(expectedIndexes);
9794
});
9895

@@ -103,7 +100,6 @@ describe('DataTableBodyComponent', () => {
103100
fixture.componentRef.setInput('offset', 1);
104101
fixture.componentRef.setInput('rowCount', 9);
105102
const expectedIndexes = { first: 0, last: 5 };
106-
component.updateIndexes();
107103
expect(component.indexes()).toEqual(expectedIndexes);
108104
});
109105

@@ -150,7 +146,6 @@ describe('DataTableBodyComponent', () => {
150146
fixture.componentRef.setInput('rowCount', 2);
151147
fixture.componentRef.setInput('pageSize', 2);
152148
fixture.componentRef.setInput('offset', 0);
153-
component.updateIndexes();
154149
fixture.detectChanges();
155150
let rows = fixture.debugElement.queryAll(By.directive(DataTableBodyRowComponent));
156151
expect(rows[0].classes['row-disabled']).toBeFalsy();
@@ -180,7 +175,6 @@ describe('DataTableBodyComponent', () => {
180175
fixture.componentRef.setInput('rowCount', 2);
181176
fixture.componentRef.setInput('pageSize', 2);
182177
fixture.componentRef.setInput('offset', 0);
183-
component.updateIndexes();
184178
fixture.detectChanges();
185179
const rows = fixture.debugElement.queryAll(By.directive(DataTableBodyRowComponent));
186180
expect(rows[0].classes['row-disabled']).toBeFalsy();
@@ -209,7 +203,6 @@ describe('DataTableBodyComponent', () => {
209203
fixture.componentRef.setInput('offset', 0);
210204
fixture.componentRef.setInput('rowIdentity', (row: any) => row.id ?? row.key);
211205

212-
component.updateIndexes();
213206
fixture.detectChanges();
214207

215208
// Initially, group should be collapsed

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)