Add lazy load events to p-dropdown for server-side paging and filtering
I'm submitting a ... (check one with "x")
[ ] bug report => Search github for a similar issue or PR before submitting
[x] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Current behavior Lazy loading in p-dropdown currently only means that the value list is only loaded when the dropdown is opened. At this point, all values will be loaded. Filtering is done over these loaded values.
Expected behavior For large sets of data it is necessary to do lazy loading with server-side paging, as is possible with p-table. Events should be fired for initial loading and filtering so that the parent component can load the appropriate data from the backend. Optional would be an event on scrolling to the end of the list, which could also trigger lazy loading in the parent.
What is the motivation / use case for changing the behavior? When selecting from a large set of data (e.g. ~1000 complex data structures), loading all values from the backend would slow down the application too much. Filtering is already present and might as well be carried out in the backend.
Any updates on this issue? @cagataycivici
Maybe it is better to use an overlaypanel that has a table inside with lazy page-sort-filter to deal with huge datasets.
How is an improper input element better? An input select should mimic standard HTML select. Not be in a table. The point is to enhance the default capabilities, not give the user an awful user experience by inventing some ridiculous table overlay for selecting an item from a list.
@cagataycivici , I totally agree with @thomas-parrish do you have a voting system for requested features to avoid closing issues that are important for your users
I was shocked when i found out that p-dropdown do not support lazy loading! It is "nice" that i can scroll over 10k items with Virtual Scroll but where i'm going to get 10k items ? From API ofc! and loading all 10k items on page load is "no-go". What is a point of "Virtual Scroll" anyway ? Nobody will embed 10k items inside app code or load it all from API...
At least add an event that will fires when the inner filter value is changed. Thank you!
Is there any updates on this issue?
I'd really love to see this feature, too. Seems though that no one will look at it - the feature request is closed.
@fkamp feature is available in this fork: https://github.com/yelhouti/primeng We are trying to stay up today with this repo. latest release was today.
Just for the count - I am also looking forward to such a feature. Need to workaround it with some (onShow) magic.
+1
any update for this?
please @cagataycivici , would you reopen this ? It seems to hard to get it working.. perhapes a simple change to expose filter function so we can hadle it, would be nice. tks
Bump, any update on this?
I have worked around the issue with onFilter callback and use observables for [options] property with async pipe.
I am using primeng v18 beta 3. I also use ngx-formly v6.
reference to: https://github.com/orgs/primefaces/discussions/2241
This worked for me in v17.
HTML:
<div class="flex flex-column flex-1">
<p-dropdown #contactsDropdown
formControlName="contactId"
[options]="contactOptions$ | async"
optionValue="id"
[filter]="true"
[filterFields]="['email', 'firstName', 'lastName', 'phone']" // important
(onFilter)="onFilterChange($event)"
[style]="{ 'width': '100%' }"
placeholder="Choose contact"
appendTo="body"
[loading]="isLoadingContactOptions">
<ng-template pTemplate="selectedItem" let-contact>
<div class="flex align-items-center gap-2">
{{ contact.firstName ? contact.firstName : "" }}
{{ contact.lastName ? contact.lastName : "" }}
{{ contact.email ? "(" + contact.email + ")" : "" }}
{{ contact.phone ? "(+" + contact.phone + ")" : "" }}
</div>
</ng-template>
<ng-template let-contact pTemplate="item">
<div class="flex align-items-center gap-2">
{{ contact.firstName ? contact.firstName : "" }}
{{ contact.lastName ? contact.lastName : "" }}
{{ contact.email ? "(" + contact.email + ")" : "" }}
{{ contact.phone ? "(+" + contact.phone + ")" : "" }}
</div>
</ng-template>
<ng-template pTemplate="emptyfilter">
<div *ngIf="isLoadingContactOptions"
class="flex flex-column flex-1 justify-content-center align-items-center">
<mat-progress-spinner mode="indeterminate" diameter="25"></mat-progress-spinner>
</div>
<div *ngIf="!isLoadingContactOptions">
Keine Kontakte gefunden.
</div>
</ng-template>
<ng-template pTemplate="empty">
Keine Kontakte gefunden.
</ng-template>
</p-dropdown>
</div>
TS:
ngOnInit() {
....
this.filterSubject$.pipe(debounceTime(500)).subscribe((event) => {
this.getContactOptions(event);
});
this.getContactOptions();
....
}
onFilterChange(event: any) {
if (this.isFilterValueProgramaticallySet) { // important
this.isFilterValueProgramaticallySet = false;
return;
}
this.isLoadingContactOptions = true
this.filterSubject$.next(event);
}
getContactOptions(event: any = null) {
let filterRequestBody: FilterRequestBody = null;
let globalFilter = event?.filter;
if (globalFilter) {
filterRequestBody = {
expression: this.createSearchExpression(globalFilter),
}
} else {
this.contactsFilterValue = null;
}
...
this.isLoadingContactOptions = true
const queryParameters: QueryParameters = {
page: 0,
pageSize: 100, // just some default page size
sortBy: 'createdAt',
sortOrder: 'desc',
}
this.service.filter(queryParameters, filterRequestBody).subscribe((contacts: any) => {
this.contactsDropdown.clear(); // important
this.selectedContactOption = null;
this.isFilterValueProgramaticallySet = true; // important
this.contactsDropdown.filterValue = globalFilter; // important
this.contactOptions$.next(contacts.data || []); // important
this.isLoadingContactOptions = false;
this.contactsDropdown.show(); // optional
});
}
Hope this helps.
Youtube: https://www.youtube.com/channel/UCgNCXa8FoOkVGi2neYUGJBA
Hi, @blochmat. In your code the page never gets updated / increased, is this how you can - theoretically - load 1000s of contacts?
@Logic-Bits
I am not increasing the page, because i just want the first 100 entries that match the current search string to be returned. I chose 100 entries, because the loading time for 100 entries was suitable for my application. If the entry the user is searching for is not yet included in the current result the user has to go on typing to further match the desired entries column.
e.g.
- search string: 'a' returns 10000 entries (first 100 shown in dropdown)
- search string: 'as' returns 5000 entries (first 100 shown in dropdown)
- search string 'asd' returns 1000 entries (first 100 shown in dropdown)
- search string 'asdf' returns 500 entries (first 100 shown in dropdown)
- search string 'asdfx' returns 5 entries
If the search string 'asdfx' is not specific enough (returns 200 entries) and you can not further specify what you are searching for my current solution is not suitable. In that case i would increase the page size to a value that is suitable for your dataset.
I believe that the idea for lazy virtual scroll is to allow you get the data lazily from your API instead of having all data at first and just rendering part of it in the DOM through the virtual scroller. I had to handle this scenario to load the next data in row in the dropdown only when the 'X' element has been rendered as the last element on the viewPort so giving you the flexibility to fire data fetching from the API only at certain threshold.
onLazyLoad(event: DropdownLazyLoadEvent) {
if (!this.dropdownLoading() && event.last === this.dropdownList().length && this.dropdownList().length < this.dropdownItemCount) {
this.getDropdownList();
}
}