@@ -3,6 +3,7 @@ import { ISortingExpression } from '../data-operations/sorting-expression.interf
3
3
import { FilteringExpressionsTree , IFilteringExpressionsTree } from '../data-operations/filtering-expressions-tree' ;
4
4
import { IFilteringExpression } from '../data-operations/filtering-expression.interface' ;
5
5
import { IgxColumnComponent } from './columns/column.component' ;
6
+ import { IgxColumnGroupComponent } from './columns/column-group.component' ;
6
7
import { IGroupingExpression } from '../data-operations/grouping-expression.interface' ;
7
8
import { IPagingState } from '../data-operations/paging-state.interface' ;
8
9
import { GridColumnDataType } from '../data-operations/data-util' ;
@@ -76,6 +77,8 @@ export interface IColumnState {
76
77
header : string ;
77
78
resizable : boolean ;
78
79
searchable : boolean ;
80
+ columnGroup : boolean ;
81
+ parent : any ;
79
82
}
80
83
81
84
export type GridFeatures = keyof IGridStateOptions ;
@@ -162,37 +165,71 @@ export class IgxGridStateDirective {
162
165
columns : {
163
166
getFeatureState : ( context : IgxGridStateDirective ) : IGridState => {
164
167
const gridColumns : IColumnState [ ] = context . currGrid . columns . map ( ( c ) => ( {
165
- pinned : c . pinned ,
166
- sortable : c . sortable ,
167
- filterable : c . filterable ,
168
- editable : c . editable ,
169
- sortingIgnoreCase : c . sortingIgnoreCase ,
170
- filteringIgnoreCase : c . filteringIgnoreCase ,
171
- headerClasses : c . headerClasses ,
172
- headerGroupClasses : c . headerGroupClasses ,
173
- maxWidth : c . maxWidth ,
174
- groupable : c . groupable ,
175
- movable : c . movable ,
176
- hidden : c . hidden ,
177
- dataType : c . dataType ,
178
- hasSummary : c . hasSummary ,
179
- field : c . field ,
180
- width : c . width ,
181
- header : c . header ,
182
- resizable : c . resizable ,
183
- searchable : c . searchable ,
184
- selectable : c . selectable
185
- } ) ) ;
168
+ pinned : c . pinned ,
169
+ sortable : c . sortable ,
170
+ filterable : c . filterable ,
171
+ editable : c . editable ,
172
+ sortingIgnoreCase : c . sortingIgnoreCase ,
173
+ filteringIgnoreCase : c . filteringIgnoreCase ,
174
+ headerClasses : c . headerClasses ,
175
+ headerGroupClasses : c . headerGroupClasses ,
176
+ maxWidth : c . maxWidth ,
177
+ groupable : c . groupable ,
178
+ movable : c . movable ,
179
+ hidden : c . hidden ,
180
+ dataType : c . dataType ,
181
+ hasSummary : c . hasSummary ,
182
+ field : c . field ,
183
+ width : c . width ,
184
+ header : c . header ,
185
+ resizable : c . resizable ,
186
+ searchable : c . searchable ,
187
+ selectable : c . selectable ,
188
+ parent : c . parent ? c . parent . header : null ,
189
+ columnGroup : c . columnGroup
190
+ } ) ) ;
186
191
return { columns : gridColumns } ;
187
192
} ,
188
193
restoreFeatureState : ( context : IgxGridStateDirective , state : IColumnState [ ] ) : void => {
189
194
const newColumns = [ ] ;
190
195
const factory = context . resolver . resolveComponentFactory ( IgxColumnComponent ) ;
196
+ const groupFactory = context . resolver . resolveComponentFactory ( IgxColumnGroupComponent ) ;
191
197
state . forEach ( ( colState ) => {
192
- const ref = factory . create ( context . viewRef . injector ) ;
193
- Object . assign ( ref . instance , colState ) ;
194
- ref . changeDetectorRef . detectChanges ( ) ;
195
- newColumns . push ( ref . instance ) ;
198
+ const hasColumnGroup = colState . columnGroup ;
199
+ delete colState . columnGroup ;
200
+ if ( hasColumnGroup ) {
201
+ const ref1 = groupFactory . create ( context . viewRef . injector ) ;
202
+ Object . assign ( ref1 . instance , colState ) ;
203
+ if ( ref1 . instance . parent ) {
204
+ const columnGroup : IgxColumnGroupComponent = newColumns . find ( e => e . header === ref1 . instance . parent ) ;
205
+ columnGroup . children . reset ( [ ...columnGroup . children . toArray ( ) , ref1 . instance ] ) ;
206
+ ref1 . instance . parent = columnGroup ;
207
+ }
208
+ ref1 . changeDetectorRef . detectChanges ( ) ;
209
+ newColumns . push ( ref1 . instance ) ;
210
+ } else {
211
+ const ref = factory . create ( context . viewRef . injector ) ;
212
+ Object . assign ( ref . instance , colState ) ;
213
+ if ( ref . instance . parent ) {
214
+ let columnGroup : IgxColumnGroupComponent = newColumns . find ( e => e . header === ref . instance . parent ) ;
215
+ if ( ! columnGroup ) {
216
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
217
+ for ( let i = 0 ; i < newColumns . length ; i ++ ) {
218
+ if ( newColumns [ i ] . children ) {
219
+ const match = context . findColumnGroupParent ( newColumns [ i ] . children . toArray ( ) , ref . instance . parent ) ;
220
+ if ( match ) {
221
+ columnGroup = match ;
222
+ break ;
223
+ }
224
+ }
225
+ }
226
+ }
227
+ ref . instance . parent = columnGroup ;
228
+ columnGroup . children . reset ( [ ...columnGroup . children . toArray ( ) , ref . instance ] ) ;
229
+ }
230
+ ref . changeDetectorRef . detectChanges ( ) ;
231
+ newColumns . push ( ref . instance ) ;
232
+ }
196
233
} ) ;
197
234
context . currGrid . columnList . reset ( newColumns ) ;
198
235
context . currGrid . columnList . notifyOnChanges ( ) ;
@@ -491,6 +528,24 @@ export class IgxGridStateDirective {
491
528
}
492
529
}
493
530
531
+ /**
532
+ * Returns columns parent column group component
533
+ */
534
+ private findColumnGroupParent ( children : IgxColumnComponent [ ] , parentHeader : string ) {
535
+ let parentComponent ;
536
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
537
+ for ( let i = 0 ; i < children . length ; i ++ ) {
538
+ if ( children [ i ] . children ) {
539
+ this . findColumnGroupParent ( children [ i ] . children . toArray ( ) , parentHeader ) ;
540
+ }
541
+ parentComponent = children . find ( e => e . header === parentHeader ) ;
542
+ if ( parentComponent ) {
543
+ break ;
544
+ }
545
+ }
546
+ return parentComponent ;
547
+ }
548
+
494
549
/**
495
550
* This method builds a FilteringExpressionsTree from a provided object.
496
551
*/
0 commit comments