table onLazyLoad

UI Components for Angular
Post Reply
eloy.silva
Posts: 9
Joined: 03 Apr 2021, 04:24

12 Jun 2021, 21:09

Hello

I am using onLazyLoad to fill the data to the p-table,
in the onInit I initialize data and get generated information for the UI

now when the data load method is executed it does not work correctly since the load method is executed before on onInit

the error occurs in the this.oForm.controls, it is as not initialized undefined

Code: Select all

<p-table [pageLinks]="0" [value]="datasource" styleClass="p-datatable-sm" dataKey="CompanyID" [lazy]="true"
      (onLazyLoad)="loadData($event)" [paginator]="true" [rows]="PreferencesEntity.PageSize"
      currentPageReportTemplate="Mostrando {first} a {last} de {totalRecords} entradas"
      [rowsPerPageOptions]="PreferencesEntity.RowsPerPageOptionsList" [totalRecords]="totalRecords" [loading]="loading" [resizableColumns]="true"
      [showCurrentPageReport]="true" selectionMode="single" (onRowSelect)="onRowSelect($event)"
      (onRowUnselect)="onRowUnselect($event)">

Code: Select all

 PreferencesEntity: IPreferences = {} as IPreferences;
  oForm = new FormGroup({}); 
  datasource = Array<ICompanySearchResponse>();
  totalRecords: number = 0; 

[code] ngOnInit() {
    
    this._commonServices.GetPreferencesByUserID(1).subscribe((data) => {     
      this.PreferencesEntity = data;
       this.onInitializeForm();
    });
  }

Code: Select all

 onInitializeForm() {

    this.oForm = this._builder.group({
      CompanyName: ['', Validators.required],
      PageNumber: [1],
      PageSize: [this.PreferencesEntity.PageSize],
      SortBy: [this.currentSortBy],
      TimeZoneHours: [0]
    });

  }

Code: Select all

loadData(event: LazyLoadEvent) {
    this.loading = true;
    let page: number = 0;

    if (event.first !== undefined) {
      if (event.first === 0) {
        page = 0;
      }
      else {
        page = Math.floor(event.rows ?? 0 / event.first);
      }
    }

    this.oForm.controls["PageNumber"].patchValue(page + 1);     //error here
    if (event.rows !== undefined)
      this.oForm.controls["PageSize"].patchValue(event.rows);

    if (event.sortField !== undefined)
      this.oForm.controls["SortBy"].patchValue(event.sortField + (event.sortOrder === 1 ? " asc" : " desc"));



    this._companyService.getSearch(this.oForm.value).subscribe(data => {
      this.datasource = data;
      this.totalRecords = data.length !== 0 ? data[0].VirtualCount : 0;
      this.loading = false;
    })


  }
Help

Thanks

PhilHuhn
Posts: 177
Joined: 19 Sep 2018, 02:52
Location: Ann Arbor, Michigan USA
Contact:

17 Jun 2021, 20:28

Hey:

I tried mine with console logs and got the following:
In ctor ...
In onInit ...
In lazyLoad ...

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 14 guests