primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Add lazy load events to p-dropdown for server-side paging and filtering

Open Cecilya opened this issue 8 years ago • 20 comments

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.

Cecilya avatar Jun 22 '18 12:06 Cecilya

Any updates on this issue? @cagataycivici

anafreitas avatar Nov 02 '18 20:11 anafreitas

Maybe it is better to use an overlaypanel that has a table inside with lazy page-sort-filter to deal with huge datasets.

cagataycivici avatar Feb 12 '19 11:02 cagataycivici

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.

thomas-parrish avatar May 13 '19 01:05 thomas-parrish

@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

yelhouti avatar Jun 02 '19 08:06 yelhouti

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...

Salamek avatar Nov 09 '19 21:11 Salamek

At least add an event that will fires when the inner filter value is changed. Thank you!

Halynsky avatar Jan 21 '20 08:01 Halynsky

Is there any updates on this issue?

gazi4430 avatar Mar 31 '21 07:03 gazi4430

I'd really love to see this feature, too. Seems though that no one will look at it - the feature request is closed.

fkamp avatar Apr 02 '21 11:04 fkamp

@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.

yelhouti avatar Apr 02 '21 14:04 yelhouti

Just for the count - I am also looking forward to such a feature. Need to workaround it with some (onShow) magic.

livanov avatar Mar 03 '22 14:03 livanov

+1

hanyska avatar Apr 01 '22 08:04 hanyska

any update for this?

hvtung avatar Jun 02 '23 07:06 hvtung

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

lixaotec avatar Oct 23 '23 18:10 lixaotec

Bump, any update on this?

komaira avatar May 29 '24 16:05 komaira

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.

faheemhameed avatar Oct 31 '24 05:10 faheemhameed

reference to: https://github.com/orgs/primefaces/discussions/2241

Logic-Bits avatar Nov 26 '24 18:11 Logic-Bits

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

blochmat avatar Jan 20 '25 08:01 blochmat

Hi, @blochmat. In your code the page never gets updated / increased, is this how you can - theoretically - load 1000s of contacts?

Logic-Bits avatar Jan 20 '25 16:01 Logic-Bits

@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.

  1. search string: 'a' returns 10000 entries (first 100 shown in dropdown)
  2. search string: 'as' returns 5000 entries (first 100 shown in dropdown)
  3. search string 'asd' returns 1000 entries (first 100 shown in dropdown)
  4. search string 'asdf' returns 500 entries (first 100 shown in dropdown)
  5. 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.

blochmat avatar Jan 21 '25 07:01 blochmat

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

kareemwezza avatar Jan 23 '25 13:01 kareemwezza