From af69860291398c88f5ca2d32d652c8496648de4d Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Mon, 15 Jun 2026 12:52:45 +0200 Subject: [PATCH] Minor code fixes --- .../alarm/alarms-table-widget.component.ts | 13 ++----------- .../entity/entities-table-widget.component.ts | 14 +++----------- .../widget/lib/table-widget.models.ts | 19 ++++++++++++++++--- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts index 8d776ce239..77cbdf3cac 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts @@ -42,7 +42,6 @@ import { hashCode, isDefined, isDefinedAndNotNull, - isEqual, isNotEmptyStr, isObject, isUndefined @@ -81,6 +80,7 @@ import { isValidPageStepIncrement, noDataMessage, prepareTableCellButtonActions, + resetPaginationOnStateChange, RowStyleInfo, TableCellButtonActionDescriptor, TableWidgetDataKeySettings, @@ -333,16 +333,7 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit, if (this.displayPagination) { this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0); - let currentStateParams = deepClone(this.ctx.stateController?.getStateParams()); - this.ctx.stateController?.stateChanged().pipe( - takeUntil(this.destroy$) - ).subscribe(() => { - const newStateParams = this.ctx.stateController.getStateParams(); - if (!isEqual(currentStateParams, newStateParams)) { - currentStateParams = deepClone(newStateParams); - this.paginator.firstPage(); - } - }); + resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$); this.ctx.aliasController?.filtersChanged.pipe( takeUntil(this.destroy$) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts index 7c84293b37..447d15d618 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts @@ -42,7 +42,7 @@ import { import { IWidgetSubscription } from '@core/api/widget-api.models'; import { UtilsService } from '@core/services/utils.service'; import { TranslateService } from '@ngx-translate/core'; -import { deepClone, hashCode, isDefined, isDefinedAndNotNull, isEqual, isObject, isUndefined } from '@core/utils'; +import { deepClone, hashCode, isDefined, isDefinedAndNotNull, isObject, isUndefined } from '@core/utils'; import cssjs from '@core/css/css'; import { CollectionViewer, DataSource } from '@angular/cdk/collections'; import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; @@ -79,6 +79,7 @@ import { isValidPageStepIncrement, noDataMessage, prepareTableCellButtonActions, + resetPaginationOnStateChange, RowStyleInfo, TableCellButtonActionDescriptor, TableWidgetDataKeySettings, @@ -267,16 +268,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni if (this.displayPagination) { this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0); - let currentStateParams = deepClone(this.ctx.stateController?.getStateParams()); - this.ctx.stateController?.stateChanged().pipe( - takeUntil(this.destroy$) - ).subscribe(() => { - const newStateParams = this.ctx.stateController.getStateParams(); - if (!isEqual(currentStateParams, newStateParams)) { - currentStateParams = deepClone(newStateParams); - this.paginator.firstPage(); - } - }); + resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$); this.ctx.aliasController?.filtersChanged.pipe( takeUntil(this.destroy$) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts index d2b191adfd..5a85bf1b7c 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts @@ -16,7 +16,7 @@ import { EntityId } from '@shared/models/id/entity-id'; import { DataKey, FormattedData, WidgetActionDescriptor, WidgetConfig } from '@shared/models/widget.models'; -import { getDescendantProp, isDefined, isNotEmptyStr } from '@core/utils'; +import { deepClone, getDescendantProp, isDefined, isEqual, isNotEmptyStr } from '@core/utils'; import { AlarmDataInfo, alarmFields } from '@shared/models/alarm.models'; import tinycolor from 'tinycolor2'; import { Direction } from '@shared/models/page/sort-order'; @@ -32,8 +32,9 @@ import { isNotEmptyTbFunction, TbFunction } from '@shared/models/js-function.models'; -import { forkJoin, Observable, of, ReplaySubject } from 'rxjs'; -import { catchError, map, share } from 'rxjs/operators'; +import { forkJoin, Observable, of, ReplaySubject, Subject } from 'rxjs'; +import { catchError, distinctUntilChanged, map, share, skip, startWith, takeUntil } from 'rxjs/operators'; +import { MatPaginator } from '@angular/material/paginator'; import type { ValueFormatProcessor } from '@shared/models/widget-settings.models'; type ColumnVisibilityOptions = 'visible' | 'hidden' | 'hidden-mobile'; @@ -516,3 +517,15 @@ export function isValidPageStepIncrement(value: number): boolean { export function isValidPageStepCount(value: number): boolean { return Number.isInteger(value) && value > 0 && value <= 100; } + +export function resetPaginationOnStateChange(ctx: WidgetContext, + paginator: MatPaginator, + destroy$: Subject): void { + ctx.stateController?.stateChanged().pipe( + map(() => deepClone(ctx.stateController.getStateParams())), + startWith(deepClone(ctx.stateController?.getStateParams())), + distinctUntilChanged(isEqual), + skip(1), + takeUntil(destroy$) + ).subscribe(() => paginator.firstPage()); +}