Skip to content

Commit e9d6fbe

Browse files
chintankavathiatimowolf
authored andcommitted
refactor: use css sticky instead of transform
1 parent 41ae5dd commit e9d6fbe

File tree

4 files changed

+20
-63
lines changed

4 files changed

+20
-63
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import { DatatableRowDetailDirective } from '../row-detail/row-detail.directive'
3232
template: `
3333
@if (groupHeader?.template) {
3434
<div
35-
[style.height.px]="groupHeaderRowHeight"
3635
class="datatable-group-header"
37-
[ngStyle]="getGroupHeaderStyle()"
36+
[style.height.px]="groupHeaderRowHeight"
37+
[style.width.px]="innerWidth"
3838
>
3939
<div class="datatable-group-cell">
4040
@if (groupHeader.checkboxable) {
@@ -191,14 +191,6 @@ export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit
191191
this.rowContextmenu.emit({ event: $event, row: this.row });
192192
}
193193

194-
getGroupHeaderStyle(): NgStyle['ngStyle'] {
195-
return {
196-
'transform': 'translate3d(' + this.offsetX + 'px, 0px, 0px)',
197-
'backface-visibility': 'hidden',
198-
'width': this.innerWidth + 'px'
199-
};
200-
}
201-
202194
onCheckboxChange(groupSelected: boolean): void {
203195
// First remove all rows of this group from `selected`
204196
this.selected = [...this.selected.filter(row => !this.group.value.find(item => item === row))];

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { ColumnGroupWidth, PinnedColumns } from '../../types/internal.types';
3333
@for (colGroup of _columnsByPin; track colGroup.type; let i = $index) {
3434
<div
3535
class="datatable-row-{{ colGroup.type }} datatable-row-group"
36-
[ngStyle]="_groupStyles[colGroup.type]"
36+
[style.width.px]="_columnGroupWidths[colGroup.type]"
3737
[class.row-disabled]="disable$ ? (disable$ | async) : false"
3838
>
3939
@for (column of colGroup.columns; track column.$$id; let ii = $index) {
@@ -67,7 +67,6 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges
6767
@Input() set columns(val: TableColumn[]) {
6868
this._columns = val;
6969
this.recalculateColumns(val);
70-
this.buildStylesByGroup();
7170
}
7271

7372
get columns(): TableColumn[] {
@@ -82,7 +81,6 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges
8281

8382
this._innerWidth = val;
8483
this.recalculateColumns();
85-
this.buildStylesByGroup();
8684
}
8785

8886
get innerWidth(): number {
@@ -104,7 +102,6 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges
104102
@Input()
105103
set offsetX(val: number) {
106104
this._offsetX = val;
107-
this.buildStylesByGroup();
108105
}
109106
get offsetX() {
110107
return this._offsetX;
@@ -173,7 +170,7 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges
173170

174171
ngOnChanges(changes: SimpleChanges): void {
175172
if (changes.verticalScrollVisible) {
176-
this.buildStylesByGroup();
173+
this.recalculateColumns();
177174
}
178175
}
179176

@@ -183,39 +180,6 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges
183180
}
184181
}
185182

186-
buildStylesByGroup() {
187-
this._groupStyles.left = this.calcStylesByGroup('left');
188-
this._groupStyles.center = this.calcStylesByGroup('center');
189-
this._groupStyles.right = this.calcStylesByGroup('right');
190-
this.cd.markForCheck();
191-
}
192-
193-
calcStylesByGroup(group: 'left' | 'right' | 'center') {
194-
const widths = this._columnGroupWidths;
195-
const offsetX = this.offsetX;
196-
197-
if (group === 'left') {
198-
return {
199-
width: `${widths[group]}px`,
200-
...translateXY(offsetX, 0)
201-
};
202-
} else if (group === 'right') {
203-
const bodyWidth = this.innerWidth;
204-
const totalDiff = widths.total - bodyWidth;
205-
const offsetDiff = totalDiff - offsetX;
206-
const offset =
207-
(offsetDiff + (this.verticalScrollVisible ? this.scrollbarHelper.width : 0)) * -1;
208-
return {
209-
width: `${widths[group]}px`,
210-
...translateXY(offset, 0)
211-
};
212-
}
213-
214-
return {
215-
width: `${widths[group]}px`
216-
};
217-
}
218-
219183
onActivate(event: ActivateEvent<TRow>, index: number): void {
220184
event.cellIndex = index;
221185
event.rowElement = this._element;

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,23 @@
117117
}
118118

119119
.datatable-row-left,
120-
.datatable-row-right {
120+
.datatable-row-right,
121+
.datatable-group-header {
121122
z-index: 9;
123+
position: sticky !important;
122124
}
123125

124126
.datatable-row-left,
125-
.datatable-row-center,
126-
.datatable-row-group,
127+
.datatable-group-header {
128+
left: 0;
129+
}
130+
127131
.datatable-row-right {
132+
right: 0;
133+
}
134+
135+
.datatable-row-center,
136+
.datatable-row-group {
128137
position: relative;
129138
}
130139

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
SortPropDir,
2424
SortType
2525
} from '../../types/public.types';
26-
import { translateXY } from '../../utils/translate';
2726
import { NgStyle } from '@angular/common';
2827
import { ScrollbarHelper } from '../../services/scrollbar-helper.service';
2928
import { TableColumn } from '../../types/table-column.type';
@@ -32,6 +31,7 @@ import {
3231
PinnedColumns,
3332
TargetChangedEvent
3433
} from '../../types/internal.types';
34+
import { translateXY } from '../../utils/translate';
3535

3636
@Component({
3737
selector: 'datatable-header',
@@ -357,20 +357,12 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
357357

358358
calcStylesByGroup(group: 'center' | 'right' | 'left'): NgStyle['ngStyle'] {
359359
const widths = this._columnGroupWidths;
360-
const offsetX = this.offsetX;
361360

362361
if (group === 'center') {
363362
return {
364-
...translateXY(offsetX * -1, 0),
365-
width: `${widths[group]}px`
366-
};
367-
} else if (group === 'right') {
368-
const totalDiff = widths.total - this.innerWidth;
369-
const offset =
370-
(totalDiff + (this.verticalScrollVisible ? this.scrollbarHelper.width : 0)) * -1;
371-
return {
372-
...translateXY(offset, 0),
373-
width: `${widths[group]}px`
363+
...translateXY(this.offsetX * -1, 0),
364+
width: `${widths[group]}px`,
365+
willChange: 'transform'
374366
};
375367
}
376368

0 commit comments

Comments
 (0)