Closed bisam-rd closed 4 years ago
Calling a private JS API (notice the underscore in the method name) of the web component is not something that we provide guarantee for with Flow. Thus this issue is not really a bug - but an enhancement for Grid
or TreeGrid
to provide an API in server side to scroll to the last row (and maybe also to the beginning). The web component itself is infinite scrolling, but we do know the size of the grid in the server side as it is reported by the data provider.
We will take a look at making this possible quite soon as this use case is quite common. Adding it should be backportable to the Flow 1.0 version (V10 & V11) of the grid, but we cannot guarantee that yet. I've milestoned it for 1.3
version (latest is 1.2
).
See detailed comments in spike ticket vaadin/vaadin-grid-flow#336.
I copy a part from there which is specific to this ticket:
the reason of this behavior is asynchronous nature of data update.
So it's totally fine to call _scrollToIndex
as a JS function in the way described in the ticket.
But data provider update calls chain should be taken into account.
Here is what happens in the click listener on plain Grid or TreeGrid with no hierarchical data:
click
action.setRequestedRange
is requested to be called once from the client side to the server side.Here is what happens with hierarchical data:
setRequestedRange
method calls from the client side and response from the server side with data (which apparently populates the hierarchical data). I see 19 such communications.As a result:
_scrollToIndex
is called on the client side as it should. But it's done in the very first response to the client side.What does it mean:
_scrollToIndex
may not be called if data provider update operation is in progress. It has to be postponed somehow in scrollToIndex
Java API.As in FW8, the scrollTo
, scrollToStart
and scrollToEnd
, work the same way regardless of the data (hierarchical or not). We will for now just go for the same implementation, which means that the row that is scrolled will be determined by the current state of the tree, meaning that expanding/collapsing nodes will change the row that is scrolled to on the same index.
We should override the methods in TreeGrid
though to give improve the javadocs by explaining the behavior of the methods with hierarchical data.
Workaround:
public class MyGrid extends Grid {
/* ... */
private void scrollTo(final TableItem item) {
final int itemIndex = dataProvider.getItemListFiltered().indexOf(item);
if(itemIndex != -1) {
getUI().ifPresent(ui -> ui.getPage().executeJs("document.getElementById('" + getId() + "')._scrollToIndex(" + itemIndex + ")"));
}
}
}
A small note about the workaround: you don't need to rely on getId()
. Instead, you can do getElement().callFunction("_scrollToIndex", itemIndex)
.
It should also be noted that any client-side method prefixed with _
should be considered as private API that might change at any time without warning.
... public class MyGrid extends Grid { final int itemIndex = dataProvider.getItemListFiltered().indexOf(item); .... }
Can you explain this piece of code. Where does getItemListFiltered()
come from?
... public class MyGrid extends Grid { final int itemIndex = dataProvider.getItemListFiltered().indexOf(item); .... }
Can you explain this piece of code. Where does
getItemListFiltered()
come from?
Sure thing.
I just noticed that this dataprovider
just extends a ListDataProvider
and implements the method getItemListFiltered
itself.
The class also overrides setFilter
to gain access to the SerializablePredicate
and stores it as a memmber.
If you have that pice of information you can simply call getItems().stream().filter(serializablePredicate).collect(Collectors.toList())
to get the result you are looking for.
Should be this closed by https://github.com/vaadin/vaadin-grid-flow/pull/778 ?
@TatuLund yes, indeed. Closing.
there is still missing variant with scroll to item...
Scrolling to item is discussed in https://github.com/vaadin/vaadin-grid-flow/issues/904.
Hello,
A feature for our application with Vaadin10 is to create a new line in a TreeGrid by clicking on a button.
The problem is if the tree grid has many lines, the user can't see the new line he created, so we want to scroll programatically when the line is created. To achieve this purpose, we call javascript function "_scrollToIndex" after line creation and expansion. That's doesn't work because javascript function "_effectiveSizeChanged" is called after "_scrollToIndex" and reset scroll to 0. Problem seems to happen when there is a hierarchy in the tree grid.
Here is a sample to reproduce the problem.