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

Created on 3 Apr 2018  路  12Comments  路  Source: primefaces/primeng

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

defect

Most helpful comment

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

All 12 comments

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.

@cagataycivici what do you think?

maybe it would be nice if it was repaired

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

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

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

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

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

its there in v9.1.0

@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

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Helayxa picture Helayxa  路  3Comments

papiroca-tm picture papiroca-tm  路  3Comments

KannanMuruganmony picture KannanMuruganmony  路  3Comments

cyberrranger picture cyberrranger  路  3Comments

watalberto picture watalberto  路  3Comments