Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

java - 如何设置数据提供者将从后端获取的元素数量?(How to set the amount of elements that will be fetched from backend by a DataProvider?)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to find a solution for this.

(我能够找到一个解决方案。)

The query.getLimit() method is depending its Grid's pagesize.

(query.getLimit()方法取决于其网格的页面大小。)

Quoting the Grid API :

(引用Grid API :)

Grid()

(网格())

Creates a new instance, with page size of 50.

(创建一个新实例,页面大小为50。)

My grid was initialized like this: Grid a = new Grid();

(我的网格是这样初始化的: Grid a = new Grid();)

which leads to a pagesize of 50. query.getLimit() will return 50 in this case.

(导致页面大小为50。在这种情况下, query.getLimit()将返回50。)

Grid(int pageSize)

(网格(int pageSize))

Creates a new instance, with the specified page size.

(创建具有指定页面大小的新实例。)

Initializing the grid like this: Grid a = new Grid(5);

(像这样初始化网格: Grid a = new Grid(5);)

.

(。)

query.getLimit() will now return 5 in this case and the DataProvider will fetch only the next 5 elements.

(在这种情况下, query.getLimit()现在将返回5,并且DataProvider仅获取接下来的5个元素。)

Edit : A github issue regarding this SO question was merged: https://github.com/vaadin/flow/issues/6996

(编辑 :关于此SO问题的github问题已合并: https : //github.com/vaadin/flow/issues/6996)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...