0

I have a problem with my data table. When I am loading data I want to show busy indicator. This is achieved by this.loading passed into DataTableComponent. Everything works fine (switching between pages, searching), but when I change pageSize (same method Load) I get ExpressionChangedAfterItHasBeenCheckedError

DataTable

I have VendorsComponent which contains method Load

load(loadParams: IDatatableLoadEvent): void {
   this.loading = true;
    this.vendorService.loalAll(loadParams.sortBy, loadParams.currentPage, loadParams.pageSize, loadParams.searchTerm, loadParams.sortOrder.toLowerCase())
    .subscribe((res: HttpResponse<IVendor[]>) => {
      this.filteredData = res.body;

      // we have to set x-pagination to COSR rules on API server
      const xPagination = res.headers.get('x-pagination');
      this.filteredTotal = JSON.parse(xPagination).totalCount;

      this.loading = false;
      // Hack - because we are setting loading and until data are loaded
      // we have to stop changeDetection and tell angular when to detect changes
      // - this happens only when we are changing pagesize
      this.cdr.detectChanges();
    }, error => {
      this.loading = false;
    });
  }

Here is how loading bar is implemented in dataTable Component: DataTableComponent

I've created a simple example here Demo, but It doesn't load data, so you can't try the exception. But it's enough for a better overview.

I've found one solution for this problem - to manually setting up Change Detection but I would like to know if this is the correct approach.

Podlipny
  • 75
  • 3
  • 9
  • Try checking the value of this.pageSize is different in your "page()" function: if (this.pageSize != pagingEvent.pageSize) this.pageSize = pagingEvent.pageSize; – Eliseo Apr 19 '18 at 16:33
  • @Eliseo this didn't help, exception occurs, when this.loading is set to false in Load method (subscribe success event) – Podlipny Apr 19 '18 at 16:54
  • Sorry, the problem is not the pageSize, is the emitter, from https://stackoverflow.com/questions/44691745/angular-4-expressionchangedafterithasbeencheckederror you must defined your loadEvent as @Output() loadEvent: EventEmitter = new EventEmitter(true); (yes, the "true" make the trick) – Eliseo Apr 19 '18 at 21:06

0 Answers0