11import {
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' ;
1512import { 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' ;
2723import { ScrollbarHelper } from '../../services/scrollbar-helper.service' ;
2824import { TableColumn } from '../../types/table-column.type' ;
2925import {
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