【发布时间】:2019-08-31 15:38:52
【问题描述】:
我已经实现了一个使用 PrimeNG TurboTable 和 NgRx 的自定义 Grid 组件,并使用内置的 TurboTable 虚拟滚动向它添加了虚拟滚动功能。它也是延迟加载,带有onLazyLoad 处理程序。但是,当我在滚动网格结果时调用 onLazyLoad 时,会出现这种奇怪的行为。
onLazyLoad called with event.first=0 and event.rows=40
onLazyLoad called with event.first=0 and event.rows=80
onLazyLoad called with event.first=0 and event.rows=160
onLazyLoad called with event.first=0 and event.rows=320
onLazyLoad called with event.first=0 and event.rows=640
onLazyLoad called with event.first=0 and event.rows=1280
所以有2个问题:
-
event.first始终为 0 -
event.rows不断翻倍到非常 高价值
我认为这是 PrimeNG 中的一个错误,但当我尝试使用简单的 StackBlitz 重现它时,它的行为正确。 https://stackblitz.com/edit/github-primeng-virtualscroll-issue
我在 StackBlitz 上看到了这个:
loadDataOnScroll is called with event.first=0 and event.rows=40
loadDataOnScroll is called with event.first=20 and event.rows=40
loadDataOnScroll is called with event.first=40 and event.rows=40
loadDataOnScroll is called with event.first=80 and event.rows=40
loadDataOnScroll is called with event.first=120 and event.rows=40
所以event.rows 保持不变,event.first 正常增加。
我使用 TurboTable 的方式似乎触发了这种情况,但我不知道是什么。可能是什么问题? 这是我的网格组件中的相关代码:
延迟加载处理程序:
onLazyLoad(event: LazyLoadEvent) {
// TODO - In this StackBlitz it behaves correctly
// https://stackblitz.com/edit/github-primeng-virtualscroll-issue
console.log(
`onLazyLoad called with event.first=${event.first} and event.rows=${
event.rows
}`
);
// TODO - This state should be handled by NgRx as well.
this.loading = true;
this.filters = {};
const hasPagination = this.settings.features.Pagination;
// TODO - Tweak the behavior of virtual scroll skipCount/maxResultCount
// based on testing results.
// The default behavior is a bit strange, skipCount is always 0
// and maxResultCount keeps getting doubled.
let pageSize = event.rows ? event.rows : GridConfig.PageSize;
this.filters.maxResultCount = pageSize;
this.filters.skipCount = event.first ? event.first : 0;
this.filters.ignorePagination = !hasPagination;
if (event.sortOrder && event.sortField) {
const sortingDirection = event.sortOrder > 0 ? 'ASC' : 'DESC';
this.filters.sorting = event.sortField + ' ' + sortingDirection;
}
if (event.globalFilter) {
this.filters.filter = event.globalFilter;
}
if (event.filters) {
this.filters.columnFilters = this.buildColumnFilters(event.filters);
}
this.filters.parentFilters = this.parentFilters; // this works only with client-side caching & filtering
if (this.settings.features.ClientSideCaching) {
// Load only once
this.gridLoaded().subscribe(loaded => {
if (loaded) {
this.store.dispatch(
new this.settings.stateActions.FilterClientSide(
this.filters
)
);
}
});
} else {
this.store.dispatch(
new this.settings.stateActions.Read(this.filters)
);
}
}
我在模板中传递的参数:
<p-table
#dataTable
[value]="(data$ | async)?.items"
[loading]="loading"
[lazy]="true"
[virtualScroll]="!settings.features.Pagination"
[virtualRowHeight]="35"
[scrollable]="!settings.features.Pagination"
(onLazyLoad)="onLazyLoad($event)"
[autoLayout]="true"
[columns]="selectedColumns"
[paginator]="settings.features.Pagination"
scrollHeight="400px"
[resizableColumns]="true"
[rowsPerPageOptions]="[10, 20, 30]"
[totalRecords]="(data$ | async)?.totalCount"
[rows]="(this.settings.states.Search | async)?.maxResultCount"
[stateStorage]="this.settings.persistence?.stateStorage"
[stateKey]="this.settings.persistence?.stateKey"
[(selection)]="selectedRows"
[selectionMode]="this.settings.rowSelection?.selectionMode || null"
(onRowSelect)="onRowSelect($event)"
[dataKey]="this.settings.rowSelection?.dataKey || null"
>
任何帮助或想法将不胜感激!谢谢!
【问题讨论】:
标签: angular primeng ngrx primeng-turbotable