I'm building a component. It's a simple list with pagination that fetches data from a webservice. I'm using bootstrap to list the values and ngx-bootstrap pagination component.
The issue is that the currentPage property is not working properly. The value is attatched to the variable, but the component is displaying the wrong info.
Inside the component, the variables have the @Input decorator
@Input() listFields?: string[]; //list of displayable fields in the list
@Input() resourceService: ResourceService<T>; //webservice to fetch data
@Input() pageSize ?: number; //number of items to receive from the webservice
@Input() currentPage ?: number = 1; //current page
@Input() modelParams?: HttpParams; //other parameters to send to the webservice
The variables described above are sent to the pagination component from ngx-bootstrap.
<pagination [(itemsPerPage)]="pageSize" [(ngModel)]="currentPage" [(totalItems)]="page.totalCount"
(pageChanged)="pageChanged($event)"></pagination>
The component I'm developing receives the values from the @inputs as described below.
<general-resource-list
[resourceService]="workshopService"
[listFields]="listFieldsWorkshop"
[currentPage]="currentPage"
[pageSize]="pageSize"
[modelParams]="pageParams">
</general-resource-list>
@Component({
selector: 'app-workshops-list',
templateUrl: './workshops-list.component.html',
styleUrls: ['./workshops-list.component.scss']
})
export class WorkshopsListComponent{
public listFieldsWorkshop = ['id','name','version'];
public title = "Workshop List";
public currentPage:number;
public pageSize:number;
public pageParams:HttpParams;
constructor(
public workshopService: WorkshopService,
public workShopRoute: ActivatedRoute,
public workshopRouter: Router) {
this.workShopRoute.queryParams.subscribe(params => {
this.pageParams = new HttpParams();
for(let key in params){
if(!["page","pageSize"].includes(key)){
this.pageParams = this.pageParams.append(key,params[key]);
}
}
this.currentPage = params['page']? params['page']:1;
this.pageSize = params['pageSize']? params['pageSize']:10;
});
}
}
First I tought it was because the @Input was loaded after the rendering of the component, but even if I set the value in the constructor or directly in the variable declarations it doesn't work.
P.S.: The pageSize property is working, and it's used the same as currentPage.
P.S.2: I don't think it is the case of implementing ControlValueAccessor, this should have been implemented in the ngx-bootstrap component, isn't it?