@@ -35,7 +35,6 @@ import {
35
35
Pagination ,
36
36
ServicePagination ,
37
37
SlickEventHandler ,
38
- TreeDataOption ,
39
38
} from './../models/index' ;
40
39
import { FilterFactory } from '../filters/filterFactory' ;
41
40
import { autoAddEditorFormatterToColumnsWithEditor } from './slick-vanilla-utilities' ;
@@ -124,6 +123,7 @@ const slickgridEventPrefix = 'sg';
124
123
export class AngularSlickgridComponent implements AfterViewInit , OnDestroy , OnInit {
125
124
private _dataset ?: any [ ] | null ;
126
125
private _columnDefinitions ! : Column [ ] ;
126
+ private _currentDatasetLength = 0 ;
127
127
private _eventHandler : SlickEventHandler = new Slick . EventHandler ( ) ;
128
128
private _angularGridInstances : AngularGridInstance | undefined ;
129
129
private _fixedHeight ?: number | null ;
@@ -213,9 +213,24 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
213
213
get dataset ( ) : any [ ] {
214
214
return this . dataView . getItems ( ) ;
215
215
}
216
- set dataset ( dataset : any [ ] ) {
217
- this . _dataset = dataset ;
218
- this . refreshGridData ( dataset ) ;
216
+ set dataset ( newDataset : any [ ] ) {
217
+ const prevDatasetLn = this . _currentDatasetLength ;
218
+ let data = newDataset ;
219
+ this . _dataset = data ;
220
+
221
+ // when Tree Data is enabled and we don't yet have the hierarchical dataset filled, we can force a convert & sort of the array
222
+ if ( this . gridOptions ?. enableTreeData && Array . isArray ( newDataset ) && ( newDataset . length > 0 || newDataset . length !== prevDatasetLn ) ) {
223
+ const sortedDatasetResult = this . treeDataService . initializeHierarchicalDataset ( data , this . _columnDefinitions ) ;
224
+ this . sharedService . hierarchicalDataset = sortedDatasetResult . hierarchical ;
225
+ data = sortedDatasetResult . flat ;
226
+
227
+ // if we add/remove item(s) from the dataset, we need to also refresh our tree data filters
228
+ if ( newDataset . length > 0 && prevDatasetLn > 0 && newDataset . length !== prevDatasetLn ) {
229
+ this . filterService . refreshTreeDataFilters ( ) ;
230
+ }
231
+ }
232
+ this . refreshGridData ( data || [ ] ) ;
233
+ this . _currentDatasetLength = newDataset . length ;
219
234
}
220
235
221
236
@Input ( )
@@ -225,13 +240,13 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
225
240
set datasetHierarchical ( newHierarchicalDataset : any [ ] | undefined ) {
226
241
this . sharedService . hierarchicalDataset = newHierarchicalDataset ;
227
242
228
- if ( newHierarchicalDataset && this . columnDefinitions && this . filterService && this . filterService . clearFilters ) {
243
+ if ( newHierarchicalDataset && this . columnDefinitions && this . filterService ? .clearFilters ) {
229
244
this . filterService . clearFilters ( ) ;
230
245
}
231
246
232
247
// when a hierarchical dataset is set afterward, we can reset the flat dataset and call a tree data sort that will overwrite the flat dataset
233
248
setTimeout ( ( ) => {
234
- if ( newHierarchicalDataset && this . dataView && this . sortService && this . sortService . processTreeDataInitialSort && this . gridOptions && this . gridOptions . enableTreeData ) {
249
+ if ( newHierarchicalDataset && this . dataView && this . sortService ?. processTreeDataInitialSort && this . gridOptions ? .enableTreeData ) {
235
250
this . dataView . setItems ( [ ] , this . gridOptions . datasetIdPropertyName ) ;
236
251
this . sortService . processTreeDataInitialSort ( ) ;
237
252
}
@@ -426,7 +441,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
426
441
427
442
if ( Array . isArray ( dataset ) && this . grid && this . dataView && typeof this . dataView . setItems === 'function' ) {
428
443
this . dataView . setItems ( dataset , this . gridOptions . datasetIdPropertyName ) ;
429
- if ( ! this . gridOptions . backendServiceApi ) {
444
+ if ( ! this . gridOptions . backendServiceApi && ! this . gridOptions . enableTreeData ) {
430
445
this . dataView . reSort ( ) ;
431
446
}
432
447
@@ -439,11 +454,6 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
439
454
}
440
455
}
441
456
this . _isDatasetInitialized = true ;
442
-
443
- // also update the hierarchical dataset
444
- if ( dataset . length > 0 && this . gridOptions . treeDataOptions ) {
445
- this . sharedService . hierarchicalDataset = this . treeDataSortComparer ( dataset ) ;
446
- }
447
457
}
448
458
449
459
if ( dataset ) {
@@ -667,16 +677,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
667
677
throw new Error ( '[Angular-Slickgrid] When enabling tree data, you must also provide the "treeDataOption" property in your Grid Options with "childrenPropName" or "parentPropName" (depending if your array is hierarchical or flat) for the Tree Data to work properly' ) ;
668
678
}
669
679
670
- this . _eventHandler . subscribe ( dataView . onRowsChanged , ( e : any , args : any ) => {
671
- // when dealing with Tree Data, anytime the flat dataset changes, we need to update our hierarchical dataset
672
- // this could be triggered by a DataView setItems or updateItem
673
- if ( this . gridOptions && this . gridOptions . enableTreeData ) {
674
- const items = this . dataView . getItems ( ) ;
675
- if ( Array . isArray ( items ) && items . length > 0 && ! this . _isDatasetInitialized ) {
676
- this . sharedService . hierarchicalDataset = this . treeDataSortComparer ( items ) ;
677
- }
678
- }
679
-
680
+ this . _eventHandler . subscribe ( dataView . onRowsChanged , ( _e : Event , args : any ) => {
680
681
// filtering data with local dataset will not always show correctly unless we call this updateRow/render
681
682
// also don't use "invalidateRows" since it destroys the entire row and as bad user experience when updating a row
682
683
// see commit: https://github.com/ghiscoding/Angular-Slickgrid/commit/bb62c0aa2314a5d61188ff005ccb564577f08805
@@ -805,6 +806,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
805
806
806
807
/** When data changes in the DataView, we'll refresh the metrics and/or display a warning if the dataset is empty */
807
808
private handleOnItemCountChanged ( currentPageRowItemCount : number , totalItemCount : number ) {
809
+ this . _currentDatasetLength = totalItemCount ;
808
810
this . metrics = {
809
811
startTime : new Date ( ) ,
810
812
endTime : new Date ( ) ,
@@ -1218,13 +1220,6 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
1218
1220
}
1219
1221
}
1220
1222
1221
- private treeDataSortComparer ( flatDataset : any [ ] ) : any [ ] {
1222
- const dataViewIdIdentifier = this . gridOptions && this . gridOptions . datasetIdPropertyName || 'id' ;
1223
- const treeDataOpt : TreeDataOption = this . gridOptions && this . gridOptions . treeDataOptions || { columnId : '' } ;
1224
- const treeDataOptions = { ...treeDataOpt , identifierPropName : treeDataOpt . identifierPropName || dataViewIdIdentifier } ;
1225
- return convertParentChildArrayToHierarchicalView ( flatDataset , treeDataOptions ) ;
1226
- }
1227
-
1228
1223
/**
1229
1224
* For convenience to the user, we provide the property "editor" as an Angular-Slickgrid editor complex object
1230
1225
* however "editor" is used internally by SlickGrid for it's own Editor Factory
0 commit comments