Skip to content

Commit f13c3fb

Browse files
committed
refactor: remove unnecessary calls of detailsRowHeight functions
Previously, the rowHeight of details was always evaluated, even if the details were not expanded. Now they are only evaluated, if actually expanded.
1 parent fc3defa commit f13c3fb

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { DatatableRowDetailDirective } from '../row-detail/row-detail.directive'
2323
<ng-content />
2424
@let rowDetailTemplate = rowDetail()?.template();
2525
@if (rowDetailTemplate && expanded()) {
26-
<div class="datatable-row-detail" [style.height.px]="detailRowHeight()">
26+
<div class="datatable-row-detail" [style.height.px]="detailsRowHeight()">
2727
<ng-template [ngTemplateOutlet]="rowDetailTemplate" [ngTemplateOutletContext]="context()" />
2828
</div>
2929
}
@@ -37,7 +37,7 @@ import { DatatableRowDetailDirective } from '../row-detail/row-detail.directive'
3737
export class DataTableRowWrapperComponent<TRow extends Row = any> implements DoCheck {
3838
readonly innerWidth = input.required<number>();
3939
readonly rowDetail = input<DatatableRowDetailDirective>();
40-
readonly detailRowHeight = input.required<number>();
40+
readonly detailRowHeightFn = input.required<(row?: TRow, index?: number) => number>();
4141
readonly row = input.required<TRow>();
4242
readonly disabled = input<boolean>();
4343
readonly rowContextmenu = output<{
@@ -50,6 +50,7 @@ export class DataTableRowWrapperComponent<TRow extends Row = any> implements DoC
5050
readonly expanded = input(false, { transform: booleanAttribute });
5151
readonly ariaGroupHeaderCheckboxMessage = input.required<string>();
5252

53+
readonly detailsRowHeight = computed(() => this.detailRowHeightFn()(this.row(), this.rowIndex()));
5354
readonly context = computed<RowDetailContext<TRow>>(() => {
5455
this.rowDiffedCount(); // This allows us to get re-evaluated when the row was mutated internally.
5556
return {

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import { DataTableSummaryRowComponent } from './summary/summary-row.component';
121121
"
122122
[innerWidth]="innerWidth()"
123123
[rowDetail]="rowDetail()"
124-
[detailRowHeight]="getDetailRowHeight(row, index)"
124+
[detailRowHeightFn]="detailRowHeightFn()"
125125
[row]="row"
126126
[disabled]="disabled"
127127
[expanded]="getRowExpanded(row)"
@@ -340,6 +340,15 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
340340
return undefined;
341341
});
342342

343+
readonly detailRowHeightFn = computed(() => {
344+
const rowDetail = this.rowDetail();
345+
if (!rowDetail) {
346+
return () => 0;
347+
}
348+
const rowHeight = rowDetail.rowHeight();
349+
return typeof rowHeight === 'function' ? rowHeight : () => rowHeight;
350+
});
351+
343352
readonly rowsToRender = computed(() => {
344353
return this.updateRows();
345354
});
@@ -566,18 +575,6 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
566575
return rowHeight as number;
567576
}
568577

569-
/**
570-
* Get the height of the detail row.
571-
*/
572-
getDetailRowHeight = (row?: TRow, index?: number): number => {
573-
const rowDetail = this.rowDetail();
574-
if (!rowDetail) {
575-
return 0;
576-
}
577-
const rowHeight = rowDetail.rowHeight();
578-
return typeof rowHeight === 'function' ? rowHeight(row, index) : (rowHeight as number);
579-
};
580-
581578
getGroupHeaderRowHeight = (row?: any, index?: any): number => {
582579
const groupHeader = this.groupHeader();
583580
if (!groupHeader) {
@@ -645,7 +642,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
645642
cache.initCache({
646643
rows: this.rows() as TRow[], // TODO: RowHeightCache does not support grouping
647644
rowHeight: this.rowHeight(),
648-
detailRowHeight: this.getDetailRowHeight,
645+
detailRowHeight: this.detailRowHeightFn(),
649646
externalVirtual: this.scrollbarV() && this.externalPaging(),
650647
indexOffset: this.externalPaging() ? this.offset() * this.pageSize() : 0,
651648
rowCount: this.rowCount(),

0 commit comments

Comments
 (0)