parttio / grid-pagination

Adding pagination feature to a Vaadin 10 Grid component
Other
22 stars 16 forks source link

Problems with PageChangeEvent and CallbackDataProvider #10

Open nemekr opened 3 years ago

nemekr commented 3 years ago

Hello,

I am using your component for a UI where the data is retrieved from a backend service using REST calls.

I noticed a few weird behaviors:

1.) When the first PageChangeEvent is being fired the values are as: newPage: 1, oldPage: 2. I see a constructor in your .js file with default value 2 as oldpage. Is this why the first event always sends the new page as 2? If so why is it necessary to be '2'?

2.) If you creating the Grid with a given DataProvider or just setting the MaxPageSize it calls the refresh method and the doCalcs method with newPage as 1. After that has happened and the onAttach finished it fires a PageChangeEvent which again calls the doCalcs thus the fetch from your DataProvider. This means that if you have CallbackDataProvider initiated at the creation of the Grid it will call the corresponding backend service twice (which is a big overhead if you showing vast amount of data)

3.) Even if the Page is either the first or the last one (or the only page) you can still click the forward or the backward buttons which fetches from the DataProvider.

Example: Lets say your have an entity called 'Record' which contains an other entity 'Item' in a List. (Record has a List and Item has a Record). For the two entities you have two Layouts with a searchbar and a grid, the user can search using the searchbar, the ui calls the corresponding backend source with the given API. But lets assume you have a button 'Related Record'/'Related Items' which creates a layout for the other entity showing only the entities that are connected to the selected Item/Record. That means you have a click listener and during that event you create the layout and also creating the DataProvider and setting it. In that case your the Grid will fetch from the DataProvider when you set it using the setDataProvider and after the creation it will fire a PageChangeEvent and fetches from the DataProvider again while the current page has not changed during these events.

Possible solution: 1.)The default oldPage during the first changeEvent could be 1 as it will be in most use cases as you probably will set the pagesize and/or the DataProvider first.(if you just creating an empty grid then it would not matter as the behavior would be same as now only it would fire a PageChangeEvent of newPage 0 and oldPage 1 - now it fires the same but as newPage 0 and oldPage 2)

2.) The PageChangeListener could ignore the events when the oldPage and the newPage are the same because that would mean there are no change - this would also fix the second and third problem mentioned above.

Please do tell if these behaviors could be fixed/these are intentional or should I provide more information regarding the issue

Sincerely

nemekr commented 3 years ago

Also, is there a way to programmatically refresh the current page in the grid? Sticking with CallbackDataProvider currently I see no way to refresh the actual page on the grid while not changing the whole underlying dataprovider.

My issue: I have a Vaadin Dialog where I can create a new Item and I'd like to add it to the Grid that contains all the Items. The simplest way would be too simply refresh the DataProvider or the current Page. Using the getDataProvider would return the DataProvider from the Vaadin Grid (which is a ListDataProvider) and not from the PaginatedGrid so I can not call refresh on the DataProvider