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;
})
}
Thanks