From 035279c18ded06709c24324a4b72cd29653a69ef Mon Sep 17 00:00:00 2001 From: Viktor Kombov Date: Tue, 16 Jun 2026 12:02:01 +0300 Subject: [PATCH] fix(crud): update total count after add and delete operations --- .../grid-crud-sample/crud-sample.component.ts | 19 ++++++++++++------- src/app/services/crud.service.ts | 15 +++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/app/grid/grid-crud-sample/crud-sample.component.ts b/src/app/grid/grid-crud-sample/crud-sample.component.ts index 89adb7187..b48623134 100644 --- a/src/app/grid/grid-crud-sample/crud-sample.component.ts +++ b/src/app/grid/grid-crud-sample/crud-sample.component.ts @@ -1,5 +1,6 @@ import { ChangeDetectorRef, Component, OnInit, ViewChild, OnDestroy, inject, ChangeDetectionStrategy } from '@angular/core'; import { GridPagingMode, IGridEditDoneEventArgs, IRowDataEventArgs, IgxColumnComponent, IgxGridEditingActionsComponent, IgxGridRow } from 'igniteui-angular/grids/core'; +import { takeUntil } from 'rxjs/operators'; import { IgxGridComponent } from 'igniteui-angular/grids/grid'; import { IgxSnackbarComponent } from 'igniteui-angular/snackbar'; import { NoopFilteringStrategy, NoopSortingStrategy } from 'igniteui-angular/core'; @@ -44,13 +45,17 @@ export class CRUDSampleComponent implements OnInit, OnDestroy { public ngOnInit(): void { this.remoteData$ = this._crudService.data$; this._crudService.getData(this.page * this.perPage, this.perPage); - this._crudService.getDataLength().subscribe((length) => { - this.totalCount = length; - }); - this.remoteData$.subscribe((data: any) => { - this.data = data; - this.grid.isLoading = false; - }); + this._crudService.totalCount$ + .pipe(takeUntil(this.destroy$)) + .subscribe(count => { + this.totalCount = count; + }); + this.remoteData$ + .pipe(takeUntil(this.destroy$)) + .subscribe((data: any) => { + this.data = data; + this.grid.isLoading = false; + }); } public rowAdded(event: IRowDataEventArgs): void { diff --git a/src/app/services/crud.service.ts b/src/app/services/crud.service.ts index 84d7e0a78..747965138 100644 --- a/src/app/services/crud.service.ts +++ b/src/app/services/crud.service.ts @@ -7,10 +7,12 @@ import { Invoice, INVOICE_DATA } from '../data/invoiceData'; @Injectable() export class CRUDService { private _http = inject(HttpClient); + private readonly _totalCount = new BehaviorSubject(0); public dataCollection: Invoice[]; public data$: Observable; private _data: BehaviorSubject; + public totalCount$ = this._totalCount.asObservable(); constructor() { this._data = new BehaviorSubject([]); @@ -19,6 +21,7 @@ export class CRUDService { rec['ID'] = 100 + index; return rec; }).slice(0); + this._totalCount.next(this.dataCollection.length); } public getAllData(): Observable { setTimeout(() => { @@ -49,6 +52,7 @@ export class CRUDService { public add(rec: Invoice): Observable { const data$: Observable = new Observable((observer) => { this.dataCollection.push(rec); + this._totalCount.next(this.dataCollection.length); observer.next(this.dataCollection); observer.complete(); }).pipe(delay(300)); @@ -73,20 +77,11 @@ export class CRUDService { const data$: Observable = new Observable((observer) => { const ind = this.dataCollection.indexOf(rec); const deletedInstance = this.dataCollection.splice(ind, 1)[0]; + this._totalCount.next(this.dataCollection.length); observer.next(deletedInstance); observer.complete(); }).pipe(delay(300)); return data$; } - - public getDataLength(): Observable { - const data$: Observable = new Observable((observer) => { - observer.next(this.dataCollection.length); - observer.complete(); - }); - - return data$; - } - }