primeng icon indicating copy to clipboard operation
primeng copied to clipboard

p-table emits lazy load event twice when initializing and multiSortMeta is provided

Open ctrl-brk opened this issue 8 years ago • 15 comments

I'm submitting a ... (check one with "x")

[x] bug report => Search github for a similar issue or PR before submitting

Current behavior When multiSortMeta property is set for a lazy p-table the table will emit onLazyLoad event 2 times during initialization. Once from ngOnInit method here https://github.com/primefaces/primeng/blob/2838bcc4e50fd581457c7fbee9edc6ed6219ec8d/src/app/components/table/table.ts#L330 and another one when assigning multiSortMeta here https://github.com/primefaces/primeng/blob/2838bcc4e50fd581457c7fbee9edc6ed6219ec8d/src/app/components/table/table.ts#L464 by calling sortMultiple without checking this.initialized like it does for single sort here https://github.com/primefaces/primeng/blob/2838bcc4e50fd581457c7fbee9edc6ed6219ec8d/src/app/components/table/table.ts#L439

Expected behavior onLazyLoad should be emitted only once during initialization

ctrl-brk avatar Apr 03 '18 16:04 ctrl-brk

onLazyLoad is also triggered multiple times when sortField and sortMode are specified. For example, I parse state of application from URL query parameters to variable state in component, because router is my single source of truth.

Component's template looks like this:

<p-table [value]="items"
         [rows]="state.pageSize"
         [first]="state.pageOffset"
         [sortMode]="'single'"
         [sortField]="state.sortField"
         [sortOrder]="state.sortOrder"
         [rowsPerPageOptions]="[10, 20, 50]"
         [paginator]="true"
         [lazy]="true"
         [totalRecords]="totalCount"
         [responsive]="true"
         (onLazyLoad)="onLazyLoad($event)"
         class="break-word table-columns-auto-width"
         [rowHover]="true">
.
.
.
</p-table>

Table has multiple sortable columns (e.g createdAt, updatedAt...).

On first load, state.sortField and state.sortOrder are null. Then user clicks on column header to sort results by this column. Click triggers sort() which triggers sortSingle() and therefore onLazyLoad. What we do in onLazyLoad is simple redirect to same page, with updated query parameters. New values from query parameters are set to state variable and this causes change of sortField and sortOrder inputs on TurboTable. These inputs are implemented as setters. Each of setters calls sortSingle() which triggers onLazyLoad.

So, sort triggered by user (click) results in 3 onLazyLoad events. From my point of view, setters should call sortSingle() only if new value differs from internal sort field/order.. or even better, it shouldn't trigger events at all (for all inputs) in lazy mode.

I would say, that our usecase is typical for other developers which use redux/ngrx architecture as well.

bossqone avatar May 28 '18 16:05 bossqone

@cagataycivici what do you think?

bossqone avatar May 29 '18 07:05 bossqone

maybe it would be nice if it was repaired

filip12345678910 avatar Aug 06 '18 12:08 filip12345678910

With 6.1.1, you can do [lazyLoadOnInit]="false" which will prevent the load at ngOnInit.

cagataycivici avatar Aug 14 '18 10:08 cagataycivici

Any way to trigger lazy load event before last row reached on scroll?

ManihtraA avatar Jan 17 '19 08:01 ManihtraA

Upon sorting a lazy loaded p-table it still emitts the event more than once. At least for me. But I am not using mutiSort

exitlol avatar Mar 20 '19 13:03 exitlol

[lazyLoadOnInit]="false" doesn't work. I am using version 6.1.6.

asheikh-alivia avatar Jun 11 '20 14:06 asheikh-alivia

[lazyLoadOnInit]="false" doesn't work. I am using version 6.1.6.

its there in v9.1.0

Maheshkale447 avatar Jun 16 '20 17:06 Maheshkale447

@cagataycivici Is the [lazyLoadOnInit] property possible with the virtual scroller component? Because I'm having the same problem and it doesn't seem to be a known property of the virtual scroller

mfish0005 avatar Jul 28 '20 21:07 mfish0005

This issue was a real problem for me because the multiple lazy loading events meant multiple calls to the back end API. My workaround was to create a component property called lastEvent then in the onLazyLoad event handler:

onLazyLoad(event: LazyLoadEvent) {
    if (JSON.stringify(this.lastEvent) === JSON.stringify(event)) {
        return false;
    }
    this.lastEvent = event;
}

This prevents the onLazyLoad handler from running multiple times.

chriswilley avatar Oct 24 '20 19:10 chriswilley

onLazyLoad is also triggered multiple times when sortField and sortMode are specified. For example, I parse state of application from URL query parameters to variable state in component, because router is my single source of truth.

Component's template looks like this:

<p-table [value]="items"
         [rows]="state.pageSize"
         [first]="state.pageOffset"
         [sortMode]="'single'"
         [sortField]="state.sortField"
         [sortOrder]="state.sortOrder"
         [rowsPerPageOptions]="[10, 20, 50]"
         [paginator]="true"
         [lazy]="true"
         [totalRecords]="totalCount"
         [responsive]="true"
         (onLazyLoad)="onLazyLoad($event)"
         class="break-word table-columns-auto-width"
         [rowHover]="true">
.
.
.
</p-table>

Table has multiple sortable columns (e.g createdAt, updatedAt...).

On first load, state.sortField and state.sortOrder are null. Then user clicks on column header to sort results by this column. Click triggers sort() which triggers sortSingle() and therefore onLazyLoad. What we do in onLazyLoad is simple redirect to same page, with updated query parameters. New values from query parameters are set to state variable and this causes change of sortField and sortOrder inputs on TurboTable. These inputs are implemented as setters. Each of setters calls sortSingle() which triggers onLazyLoad.

So, sort triggered by user (click) results in 3 onLazyLoad events. From my point of view, setters should call sortSingle() only if new value differs from internal sort field/order.. or even better, it shouldn't trigger events at all (for all inputs) in lazy mode.

I would say, that our usecase is typical for other developers which use redux/ngrx architecture as well.

Was this bug addressed on the same PR? I think this is also reproducible using customSort

cLandin avatar Dec 09 '20 17:12 cLandin

You can use the property as [lazyLoadOnInit]="false"

sheikhajaz avatar Mar 09 '21 15:03 sheikhajaz

In my case on scroll call more than one time call event, one's i reached last row it will be calling 0 to last row index api call

YogeshFullStack avatar Nov 02 '22 07:11 YogeshFullStack

Hello, is there any solution? I have same issue. I tried use [lazyLoadOnInit]="false" and it still don't work. OnLazyLoad is still call twice on init

petrkasnal avatar Jun 09 '23 05:06 petrkasnal

OnLazyLoad is called twice with both .first and .rows equal to zero. The first call allows to fetch totalCount from server and initialize virtualItems array, but the second is redundant. The worst thing is that when we scroll back and forth, OnLazyLoad is called again for the items which are already loaded. Is there a reason to implement "infinite load" instead of (or in addition to) the "infinite scroll"? E.g. an explicit "Load more" button as the last row in the table. In this case:

  • items will be appended to the end of items array;
  • there is no need to know totalCount;
  • OnLazyLoad calls would be explicit and just for items that are not already loaded

buchatsky avatar Dec 18 '23 21:12 buchatsky