I need to send params in my URL to get filtered data from the server.
I set params like this
const queryParams = new HttpParams(
{
fromObject: {
page: this.paginator.pageIndex.toString(),
pageSize: this.paginator.pageSize.toString(),
search: this.searchKey,
sortBy: this.selectedFilterByStatusValue,
sortByType: this.selectedFilterSortByValue
}
});
And in my service
getData(params: HttpParams) {
return this.apiService.get<IResponseList<IData>>(`${this.url}/list`, params);
}
Problem is, when some filter is not selected, because I don't need them, and it is set like undefined, but this is not correct... I need to remove undefined params.
Here is console.log for queryParams
cloneFrom: null
encoder: HttpUrlEncodingCodec {}
map: Map(5)
[[Entries]]
0: {"page" => Array(1)}
1: {"pageSize" => Array(1)}
key: "pageSize"
value: ["3"]
2: {"search" => Array(1)}
3: {"sortBy" => Array(1)}
4: {"sortByType" => Array(1)}
I try to use .set("page", this.paginator.pageIndex) and also .append(...) but I always get same structure
Also, I try directly in service fix this, but this not working, I again get undefined
this.httpClient.get(`${this.url}/list`, {
params: Object.entries(queryParameterObject).reduce((queryParams, [key, value]) => queryParams.set(key, value), new HttpParams());
});
Thnx