Skip to content

Commit 33a2e62

Browse files
spike-rabbitfh1ch
authored andcommitted
refactor: add types to RowHeightCache
1 parent fd91536 commit 33a2e62

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,16 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
642642
* Refreshes the full Row Height cache. Should be used
643643
* when the entire row array state has changed.
644644
*/
645-
computeRowHeightsCache(): RowHeightCache {
646-
const cache = new RowHeightCache();
645+
computeRowHeightsCache(): RowHeightCache<TRow> {
646+
const cache = new RowHeightCache<TRow>();
647647
if (!this.scrollbarV() || (this.scrollbarV() && !this.virtualization())) {
648648
return cache;
649649
}
650650

651651
// Initialize the tree only if there are rows inside the tree.
652652
if (this.rows().length) {
653653
cache.initCache({
654-
rows: this.rows(),
654+
rows: this.rows() as TRow[], // TODO: RowHeightCache does not support grouping
655655
rowHeight: this.rowHeight(),
656656
detailRowHeight: this.getDetailRowHeight,
657657
externalVirtual: this.scrollbarV() && this.externalPaging(),

projects/ngx-datatable/src/lib/utils/row-height-cache.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* https://github.com/mikolalysenko/fenwick-tree
88
*
99
*/
10-
export class RowHeightCache {
10+
export class RowHeightCache<TRow> {
1111
/**
1212
* Tree Array stores the cumulative information of the row heights to perform efficient
1313
* range queries and updates. Currently the tree is initialized to the base row
@@ -24,12 +24,16 @@ export class RowHeightCache {
2424

2525
/**
2626
* Initialize the Fenwick tree with row Heights.
27-
*
28-
* @param rows The array of rows which contain the expanded status.
29-
* @param rowHeight The row height.
30-
* @param detailRowHeight The detail row height.
3127
*/
32-
initCache(details: any): void {
28+
initCache(details: {
29+
rows: TRow[];
30+
rowHeight: number | 'auto' | ((row: TRow) => number);
31+
detailRowHeight: number | ((row: TRow, index: number) => number);
32+
externalVirtual: boolean | undefined;
33+
indexOffset: number;
34+
rowCount: number;
35+
rowExpansions: Set<TRow>;
36+
}): void {
3337
const {
3438
rows,
3539
rowHeight,
@@ -42,7 +46,7 @@ export class RowHeightCache {
4246
const isFn = typeof rowHeight === 'function';
4347
const isDetailFn = typeof detailRowHeight === 'function';
4448

45-
if (!isFn && isNaN(rowHeight)) {
49+
if (rowHeight === 'auto' || (!isFn && isNaN(rowHeight))) {
4650
throw new Error(`Row Height cache initialization failed. Please ensure that 'rowHeight' is a
4751
valid number or function value: (${rowHeight}) when 'scrollbarV' is enabled.`);
4852
}
@@ -62,21 +66,13 @@ export class RowHeightCache {
6266

6367
for (let i = 0; i < n; ++i) {
6468
const row = rows[i];
65-
let currentRowHeight = rowHeight;
66-
if (isFn) {
67-
currentRowHeight = rowHeight(row);
68-
}
69+
let currentRowHeight = isFn ? rowHeight(row) : rowHeight;
6970

7071
// Add the detail row height to the already expanded rows.
7172
// This is useful for the table that goes through a filter or sort.
7273
const expanded = rowExpansions.has(row);
7374
if (row && expanded) {
74-
if (isDetailFn) {
75-
const index = indexOffset + i;
76-
currentRowHeight += detailRowHeight(row, index);
77-
} else {
78-
currentRowHeight += detailRowHeight;
79-
}
75+
currentRowHeight += isDetailFn ? detailRowHeight(row, indexOffset + i) : detailRowHeight;
8076
}
8177

8278
this.update(i, currentRowHeight);

0 commit comments

Comments
 (0)