Skip to content

Commit dd8858b

Browse files
refactor: header component
removed unnecessary transform calculations for scrolling header along with body scroll.
1 parent e818477 commit dd8858b

3 files changed

Lines changed: 23 additions & 74 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
[sortType]="sortType"
88
[scrollbarH]="scrollbarH"
99
[innerWidth]="_innerWidth"
10-
[offsetX]="_offsetX | async"
1110
[dealsWithGroup]="groupedRows !== undefined"
1211
[columns]="_internalColumns"
1312
[headerHeight]="headerHeight"

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ export class DatatableComponent<TRow = any>
671671
@ViewChild(DataTableHeaderComponent)
672672
headerComponent: DataTableHeaderComponent;
673673

674+
@ViewChild(DataTableHeaderComponent, { read: ElementRef })
675+
headerElement: ElementRef<HTMLElement>;
676+
674677
@ViewChild(DataTableBodyComponent, { read: ElementRef })
675678
private bodyElement: ElementRef<HTMLElement>;
676679
@ContentChild(DatatableRowDefDirective, {
@@ -947,7 +950,7 @@ export class DatatableComponent<TRow = any>
947950
this.bodyComponent.cd.markForCheck();
948951
}
949952

950-
if (this.headerComponent && this.headerComponent._columnGroupWidths.total !== width) {
953+
if (this.headerComponent && this.headerComponent._columnGroupWidths().total !== width) {
951954
this.headerComponent.columns = [...this._internalColumns];
952955
}
953956

@@ -1012,7 +1015,10 @@ export class DatatableComponent<TRow = any>
10121015
* The body triggered a scroll event.
10131016
*/
10141017
onBodyScroll(event: ScrollEvent): void {
1015-
this._offsetX.next(event.offsetX);
1018+
// if horizontal scroll is enabled we update the header scroll position
1019+
if (this.headerElement && this.scrollbarH && !isNaN(event.offsetX)) {
1020+
this.headerElement.nativeElement.scrollLeft = event.offsetX;
1021+
}
10161022
this.scroll.emit(event);
10171023
this.cd.detectChanges();
10181024
}

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

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import {
22
ChangeDetectionStrategy,
3-
ChangeDetectorRef,
43
Component,
54
EventEmitter,
65
HostBinding,
76
inject,
87
Input,
9-
OnChanges,
10-
OnDestroy,
118
Output,
12-
SimpleChanges,
9+
signal,
1310
TemplateRef
1411
} from '@angular/core';
1512
import { columnGroupWidths, columnsByPin, columnsByPinArr } from '../../utils/column';
@@ -23,10 +20,10 @@ import {
2320
SortPropDir,
2421
SortType
2522
} from '../../types/public.types';
26-
import { NgStyle } from '@angular/common';
2723
import { ScrollbarHelper } from '../../services/scrollbar-helper.service';
2824
import { TableColumn } from '../../types/table-column.type';
2925
import {
26+
ColumnGroupWidth,
3027
OrderableReorderEvent,
3128
PinnedColumns,
3229
TargetChangedEvent
@@ -45,11 +42,14 @@ import { OrderableDirective } from '../../directives/orderable.directive';
4542
orderable
4643
(reorder)="onColumnReordered($event)"
4744
(targetChanged)="onTargetChanged($event)"
48-
[style.width.px]="_columnGroupWidths.total"
45+
[style.width.px]="_columnGroupWidths().total"
4946
class="datatable-header-inner"
5047
>
5148
@for (colGroup of _columnsByPin; track colGroup.type) {
52-
<div [class]="'datatable-row-' + colGroup.type" [ngStyle]="_styleByGroup[colGroup.type]">
49+
<div
50+
[class]="'datatable-row-' + colGroup.type"
51+
[style.width.px]="_columnGroupWidths()[colGroup.type]"
52+
>
5353
@for (column of colGroup.columns; track column.$$id) {
5454
<datatable-header-cell
5555
role="columnheader"
@@ -97,15 +97,13 @@ import { OrderableDirective } from '../../directives/orderable.directive';
9797
standalone: true,
9898
imports: [
9999
OrderableDirective,
100-
NgStyle,
101100
DataTableHeaderCellComponent,
102101
ResizeableDirective,
103102
LongPressDirective,
104103
DraggableDirective
105104
]
106105
})
107-
export class DataTableHeaderComponent implements OnDestroy, OnChanges {
108-
private cd = inject(ChangeDetectorRef);
106+
export class DataTableHeaderComponent {
109107
private scrollbarHelper = inject(ScrollbarHelper);
110108

111109
@Input() sortAscendingIcon: string;
@@ -121,8 +119,7 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
121119
setTimeout(() => {
122120
if (this._columns) {
123121
const colByPin = columnsByPin(this._columns);
124-
this._columnGroupWidths = columnGroupWidths(colByPin, this._columns);
125-
this.setStylesByGroup();
122+
this._columnGroupWidths.set(columnGroupWidths(colByPin, this._columns));
126123
}
127124
});
128125
}
@@ -160,24 +157,14 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
160157
const colsByPin = columnsByPin(val);
161158
this._columnsByPin = columnsByPinArr(val);
162159
setTimeout(() => {
163-
this._columnGroupWidths = columnGroupWidths(colsByPin, val);
164-
this.setStylesByGroup();
160+
this._columnGroupWidths.set(columnGroupWidths(colsByPin, val));
165161
});
166162
}
167163

168164
get columns(): any[] {
169165
return this._columns;
170166
}
171167

172-
@Input()
173-
set offsetX(val: number) {
174-
this._offsetX = val;
175-
this.setStylesByGroup();
176-
}
177-
get offsetX() {
178-
return this._offsetX;
179-
}
180-
181168
@Output() sort: EventEmitter<SortEvent> = new EventEmitter();
182169
@Output() reorder: EventEmitter<ReorderEvent> = new EventEmitter();
183170
@Output() resize: EventEmitter<ColumnResizeEvent> = new EventEmitter();
@@ -186,33 +173,15 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
186173
@Output() columnContextmenu = new EventEmitter<{ event: MouseEvent; column: TableColumn }>(false);
187174

188175
_columnsByPin: PinnedColumns[];
189-
_columnGroupWidths: any = {
176+
_columnGroupWidths = signal<ColumnGroupWidth>({
177+
left: 0,
178+
center: 0,
179+
right: 0,
190180
total: 100
191-
};
181+
});
192182
_innerWidth: number;
193-
_offsetX: number;
194183
_columns: TableColumn[];
195184
_headerHeight: string;
196-
_styleByGroup: {
197-
left: NgStyle['ngStyle'];
198-
center: NgStyle['ngStyle'];
199-
right: NgStyle['ngStyle'];
200-
} = { left: {}, center: {}, right: {} };
201-
202-
private destroyed = false;
203-
204-
ngOnChanges(changes: SimpleChanges): void {
205-
if (changes.verticalScrollVisible) {
206-
this._styleByGroup.right = this.calcStylesByGroup('right');
207-
if (!this.destroyed) {
208-
this.cd.detectChanges();
209-
}
210-
}
211-
}
212-
213-
ngOnDestroy(): void {
214-
this.destroyed = true;
215-
}
216185

217186
onLongPressStart({ event, model }: { event: MouseEvent; model: TableColumn }) {
218187
model.dragging = true;
@@ -358,29 +327,4 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
358327

359328
return sorts;
360329
}
361-
362-
setStylesByGroup() {
363-
this._styleByGroup.left = this.calcStylesByGroup('left');
364-
this._styleByGroup.center = this.calcStylesByGroup('center');
365-
this._styleByGroup.right = this.calcStylesByGroup('right');
366-
if (!this.destroyed) {
367-
this.cd.detectChanges();
368-
}
369-
}
370-
371-
calcStylesByGroup(group: 'center' | 'right' | 'left'): NgStyle['ngStyle'] {
372-
const widths = this._columnGroupWidths;
373-
374-
if (group === 'center') {
375-
return {
376-
transform: `translateX(${this.offsetX * -1}px)`,
377-
width: `${widths[group]}px`,
378-
willChange: 'transform'
379-
};
380-
}
381-
382-
return {
383-
width: `${widths[group]}px`
384-
};
385-
}
386330
}

0 commit comments

Comments
 (0)