I want to lazy load some data from a grid into my Vaadin View.
(我想将网格中的某些数据延迟加载到Vaadin视图中。)
The grid rows are sometimes pretty huge so I want to limit the amount of rows that are loaded at once let's say to 2. (网格行有时非常大,所以我想将一次加载的行数限制为2。)
I tried to do this using DataProvider.fromCallbacks()
.
(我试图使用DataProvider.fromCallbacks()
做到这一点。)
Unfortunately I am not getting the result I wish for and I am a bit confused about the two functions that are passed into the method. (不幸的是,我没有得到想要的结果,并且对于传递给该方法的两个函数有些困惑。)
As far as I am understanding it correctly the first function that is passed describes how the items are fetched and the second function tells the DataProvider the maximal possible amount of items that can be fetched.
(据我正确理解,传递的第一个函数描述了如何提取项目,第二个函数告诉DataProvider可提取的最大项目数。)
fromCallbacks(CallbackDataProvider.FetchCallback fetchCallback, CallbackDataProvider.CountCallback countCallback) Creates a new data provider that uses callbacks for fetching and counting items from any backing store.
(fromCallbacks(CallbackDataProvider.FetchCallback fetchCallback,CallbackDataProvider.CountCallback countCallback)创建一个新的数据提供程序,该提供程序使用回调从任何后备存储中获取和计数项目。)
I tried the following but it always leads to a lot of items fetched.
(我尝试了以下方法,但是它总是导致很多东西被拿走。)
(17 items in list leads to 17 items fetched, 120 items in list leads to 50's batches) ((列表中的17个项目可获取17个项目,列表中的120个项目可获取50个批次))
List<SomeObject> data = ... // filled with objects
DataProvider<CustromCell, Void> lazyLoadingProvider
= DataProvider.fromCallbacks(
query -> {
int offset = query.getOffset();
int limit = query.getLimit(); // Is this the amount of items that are fetched in one step?
if (offset + limit > data.size())
return data.stream();
List<CustromCell> cells = data.subList(offset,offset+limit);
//(offest,offset+2) crashes
return cells.stream()
},
query -> data.size() // Max possible items to fetch
);
grid.setDataProvider(lazyLoadingProvider);
Has the following behavior:
(具有以下行为:)
Size Of Data: 219
Offset: 0
Limit: 50
Size Of Data: 219
Offset: 50
Limit: 50
Size Of Data: 219
Offset: 100
Limit: 50
Size Of Data: 219
Offset: 150
Limit: 50
Size Of Data: 219
Offset: 200
Limit: 19
I have to call getOffset()
and getLimit()
of the Query object otherwise an exception is thrown.
(我必须调用Query对象的getOffset()
和getLimit()
,否则将引发异常。)
If I understand it correctly then the limit tells how many items shall be fetched (the goal would be 2 in my case) but the object is immutable so I can't change this value ( Query API )? (如果我正确理解它,那么该限制会告诉您应该提取多少个项目(在我的情况下,目标是2),但是该对象是不可变的,因此我无法更改此值( 查询API )?)
tl;dr How can I tell the DataProvider to only fetch 2 items of my grid at once?
(tl; dr如何告诉DataProvider一次仅获取网格中的2个项目?)
Edit : I found github issues related to my issue ( #6072 , #3830 )
(编辑 :我发现与我的问题( #6072 , #3830 )相关的github问题)
Edit 2 : A github issue was merged so it is more clear how the getLimit() method works: https://github.com/vaadin/flow/issues/6996
(编辑2 :合并了github问题,因此更清楚getLimit()方法的工作方式: https : //github.com/vaadin/flow/issues/6996)
ask by mkemmerz translate from so