diff --git a/src/slickgrid-react/components/slickgrid-react.tsx b/src/slickgrid-react/components/slickgrid-react.tsx index b733d054..cd50be53 100644 --- a/src/slickgrid-react/components/slickgrid-react.tsx +++ b/src/slickgrid-react/components/slickgrid-react.tsx @@ -95,8 +95,8 @@ class CustomEventPubSubService extends EventPubSubService { } export class ReactSlickgridComponent extends React.Component { - private _mounted = false; - private setStateValue(key: string, value: any, callback?: () => void): void { + protected _mounted = false; + protected setStateValue(key: string, value: any, callback?: () => void): void { if (this.state && this.state[key] === value) { return; } @@ -114,25 +114,41 @@ export class ReactSlickgridComponent extends React.Component 0 && (this._gridOptions.autosizeColumnsByCellContentOnFirstLoad || this._gridOptions.enableAutoResizeColumnsByCellContent)) { + if (args.itemCount > 0 && (this.gridOptions.autosizeColumnsByCellContentOnFirstLoad || this.gridOptions.enableAutoResizeColumnsByCellContent)) { this.resizerService.resizeColumnsByCellContent(!this._gridOptions?.resizeByContentOnlyOnFirstLoad); } }); @@ -870,7 +887,7 @@ export class ReactSlickgridComponent extends React.Component 0) { // when using multi-column sort, we can have multiple but on single sort then only grab the first sort provided - const sortColumns = this._gridOptions.multiColumnSort ? gridOptions.presets.sorters : gridOptions.presets.sorters.slice(0, 1); + const sortColumns = this._gridOptions?.multiColumnSort ? gridOptions.presets.sorters : gridOptions.presets.sorters.slice(0, 1); backendApiService.updateSorters(undefined, sortColumns); } // Pagination "presets" @@ -950,7 +967,7 @@ export class ReactSlickgridComponent extends React.Component 0) { - if (!this._isDatasetInitialized) { - this.loadFilterPresetsWhenDatasetInitialized(); + if (Array.isArray(dataset) && this.grid && this.dataview?.setItems) { + this.dataview.setItems(dataset, this._gridOptions.datasetIdPropertyName ?? 'id'); + if (!this._gridOptions.backendServiceApi && !this._gridOptions.enableTreeData) { + this.dataview.reSort(); + } + + if (dataset.length > 0) { + if (!this._isDatasetInitialized) { + this.loadFilterPresetsWhenDatasetInitialized(); - if (this._gridOptions.enableCheckboxSelector) { - this.loadRowSelectionPresetWhenExists(); + if (this._gridOptions.enableCheckboxSelector) { + this.loadRowSelectionPresetWhenExists(); + } } + this._isDatasetInitialized = true; } - this._isDatasetInitialized = true; - } - if (dataset) { - this.grid.invalidate(); - } + if (dataset) { + this.grid.invalidate(); + } - // display the Pagination component only after calling this refresh data first, we call it here so that if we preset pagination page number it will be shown correctly - this.showPagination = (this._gridOptions && (this._gridOptions.enablePagination || (this._gridOptions.backendServiceApi && this._gridOptions.enablePagination === undefined))) ? true : false; + // display the Pagination component only after calling this refresh data first, we call it here so that if we preset pagination page number it will be shown correctly + this.showPagination = (this._gridOptions && (this._gridOptions.enablePagination || (this._gridOptions.backendServiceApi && this._gridOptions.enablePagination === undefined))) ? true : false; - if (this._paginationOptions && this._gridOptions?.pagination && this._gridOptions?.backendServiceApi) { - const paginationOptions = this.setPaginationOptionsWhenPresetDefined(this._gridOptions, this._paginationOptions); + if (this._paginationOptions && this._gridOptions?.pagination && this._gridOptions?.backendServiceApi) { + const paginationOptions = this.setPaginationOptionsWhenPresetDefined(this._gridOptions, this._paginationOptions); - // when we have a totalCount use it, else we'll take it from the pagination object - // only update the total items if it's different to avoid refreshing the UI - const totalRecords = (totalCount !== undefined) ? totalCount : (this._gridOptions?.pagination?.totalItems); - if (totalRecords !== undefined && totalRecords !== this.totalItems) { - this.totalItems = +totalRecords; - } - // initialize the Pagination Service with new pagination options (which might have presets) - if (!this._isPaginationInitialized) { - this.initializePaginationService(paginationOptions); - } else { - // update the pagination service with the new total - this.paginationService.updateTotalItems(this.totalItems); + // when we have a totalCount use it, else we'll take it from the pagination object + // only update the total items if it's different to avoid refreshing the UI + const totalRecords = (totalCount !== undefined) ? totalCount : (this._gridOptions?.pagination?.totalItems); + if (totalRecords !== undefined && totalRecords !== this.totalItems) { + this.totalItems = +totalRecords; + } + // initialize the Pagination Service with new pagination options (which might have presets) + if (!this._isPaginationInitialized) { + this.initializePaginationService(paginationOptions); + } else { + // update the pagination service with the new total + this.paginationService.updateTotalItems(this.totalItems); + } } - } - // resize the grid inside a slight timeout, in case other DOM element changed prior to the resize (like a filter/pagination changed) - if (this.grid && this._gridOptions.enableAutoResize) { - const delay = this._gridOptions.autoResize && this._gridOptions.autoResize.delay; - this.resizerService.resizeGrid(delay || 10); + // resize the grid inside a slight timeout, in case other DOM element changed prior to the resize (like a filter/pagination changed) + if (this.grid && this._gridOptions.enableAutoResize) { + const delay = this._gridOptions.autoResize && this._gridOptions.autoResize.delay; + this.resizerService.resizeGrid(delay || 10); + } } } } @@ -1085,7 +1106,7 @@ export class ReactSlickgridComponent extends React.Component col.originalWidth = col.width); } - private displayEmptyDataWarning(showWarning = true) { + protected displayEmptyDataWarning(showWarning = true) { this.slickEmptyWarning?.showEmptyDataMessage(showWarning); } /** When data changes in the DataView, we'll refresh the metrics and/or display a warning if the dataset is empty */ - private handleOnItemCountChanged(currentPageRowItemCount: number, totalItemCount: number) { + protected handleOnItemCountChanged(currentPageRowItemCount: number, totalItemCount: number) { this._currentDatasetLength = totalItemCount; this.metrics = { startTime: new Date(), @@ -1145,21 +1166,21 @@ export class ReactSlickgridComponent extends React.Component this.paginationChanged(paginationChanges)), - this._eventPubSubService.subscribe('onPaginationOptionsChanged', (paginationChanges: ServicePagination) => this.paginationOptionsChanged(paginationChanges)), - this._eventPubSubService.subscribe('onPaginationVisibilityChanged', (visibility: { visible: boolean }) => { + this._eventPubSubService.subscribe('onPaginationChanged', paginationChanges => this.paginationChanged(paginationChanges)), + this._eventPubSubService.subscribe('onPaginationOptionsChanged', paginationChanges => this.paginationOptionsChanged(paginationChanges)), + this._eventPubSubService.subscribe<{ visible: boolean; }>('onPaginationVisibilityChanged', (visibility: { visible: boolean }) => { this.showPagination = visibility?.visible ?? false; - if (this._gridOptions?.backendServiceApi) { - this.backendUtilityService?.refreshBackendDataset(this._gridOptions); + if (this.gridOptions?.backendServiceApi) { + this.backendUtilityService?.refreshBackendDataset(this.gridOptions); } this.renderPagination(this.showPagination); }) @@ -1171,8 +1192,27 @@ export class ReactSlickgridComponent extends React.Component 0) { - const gridColumns: Column[] = this.gridStateService.getAssociatedGridColumns(this.grid, this._gridOptions.presets.columns); + if (this.grid && this.gridOptions.presets && Array.isArray(this.gridOptions.presets.columns) && this.gridOptions.presets.columns.length > 0) { + const gridColumns: Column[] = this.gridStateService.getAssociatedGridColumns(this.grid, this.gridOptions.presets.columns); if (gridColumns && Array.isArray(gridColumns) && gridColumns.length > 0) { // make sure that the checkbox selector is also visible if it is enabled - if (this._gridOptions.enableCheckboxSelector) { + if (this.gridOptions.enableCheckboxSelector) { const checkboxColumn = (Array.isArray(this._columnDefinitions) && this._columnDefinitions.length > 0) ? this._columnDefinitions[0] : null; if (checkboxColumn && checkboxColumn.id === '_checkbox_selector' && gridColumns[0].id !== '_checkbox_selector') { gridColumns.unshift(checkboxColumn); @@ -1230,7 +1270,7 @@ export class ReactSlickgridComponent extends React.Component { if (this.grid && Array.isArray(gridRowIndexes)) { this.grid.setSelectedRows(gridRowIndexes); @@ -1289,7 +1329,7 @@ export class ReactSlickgridComponent extends React.Component} flatDatasetInput - flat dataset input * @param {Boolean} forceGridRefresh - optionally force a full grid refresh * @returns {Array} sort flat parent/child dataset */ - private sortTreeDataset(flatDatasetInput: T[], forceGridRefresh = false): T[] { + protected sortTreeDataset(flatDatasetInput: T[], forceGridRefresh = false): T[] { const prevDatasetLn = this._currentDatasetLength; let sortedDatasetResult; let flatDatasetOutput: any[] = []; @@ -1435,7 +1456,7 @@ export class ReactSlickgridComponent extends React.Component `${col.id}`.includes('.'))) { console.error('[Slickgrid-React] Make sure that none of your Column Definition "id" property includes a dot in its name because that will cause some problems with the Editors. For example if your column definition "field" property is "user.firstName" then use "firstName" as the column "id".'); } @@ -1474,12 +1495,12 @@ export class ReactSlickgridComponent extends React.Component