Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting item's index through DataView/DataProvider API #18088

Closed
mshabarov opened this issue Nov 21, 2023 · 2 comments · Fixed by #18306
Closed

Getting item's index through DataView/DataProvider API #18088

mshabarov opened this issue Nov 21, 2023 · 2 comments · Fixed by #18306

Comments

@mshabarov
Copy link
Contributor

Describe your motivation

Vaadin components with listed items can be scrolled to item by index. This isn't always possible, because index of an item might be unknown (e.g. depends on the sorting and filtering).

Thus, an API to get the index by item would be helpful for such uses cases.

Describe the solution you'd like

Proposed solution is to have a new method in DataView API for this.

Acceptance criteria:

  1. DataView interface has a method int getItemIndex(T item) (or with a similar name) that takes items object and return the index of this item.

  2. ListDataView reuses the existing private method getItemIndex.

  3. LazyDataView has a new method for applying a callback for getting an index from backend:

    LazyDataView::setItemIndexProvider(T item, Query<T, F> query)

    and this callback, if being set by developer, contains a logic for project's backend/database.

    This callback is a more convenient way of customising data view, than extending data view class, because then developer would need to create it's own data view class, find a proper data view base class, set up it to component and so on.

    If no callback was given, LazyDataView throws an UnsupportedOperationException or similar and highlights that it needs a callback to properly fetch the index.

  4. For point 4, a practical example is provided showing how this callback can be implemented, for instance, for relational database.

Additional context

References:

  1. [grid] Scroll to item as well web-components#1951
  2. Implementation in Viritin add-on.
@tltv tltv self-assigned this Dec 12, 2023
tltv added a commit that referenced this issue Dec 14, 2023
New `getItemIndex` method in `DataView` affects `ListDataView` and `LazyDataView` implementations. `LazyDataView` introduces in addition a new method `setItemIndexProvider(ItemIndexProvider)`.
With `ListDataView`, `getItemIndex` works out-of-the-box with the in-memory data.
With `LazyDataView`, it's required to set item index provider first with `setItemIndexProvider(ItemIndexProvider)`. Otherwise `getItemIndex` will throw `UnsupportedOperationException`. Provider can be implemented to fetch correct item index using item and `Query` parameters. `Query` object is set up to fetch all items with sorting and filter.

Fixes: #18088
@tltv
Copy link
Member

tltv commented Dec 14, 2023

AbstractDataView::verifyDataProviderType blocks LazyDataView's to work for example when using data provider wrapper like ConfigurableFilterDataProviderWrapper, which is practical when using filtering with DataProvider::withConfigurableFilter call.
It's not possible to use new API for example with this example code documented here: https://vaadin.com/docs/latest/components/grid#lazy-loading.

To fix that, I'll make another PR for better support for wrappers with LazyDataView.

tltv added a commit that referenced this issue Dec 15, 2023
…tDataView

Adds in `AbstractDataView` a better support for wrapper data providers that implements `DataProviderWrapper`. `AbstractDataView` won't throw exception anymore if wrapped data container is supported by the data view implementation. Adds public `getWrappedDataProvider` method in `DataProviderWrapper`.

Related-to: #18088
tltv added a commit that referenced this issue Dec 15, 2023
Adds in `AbstractDataView` a better support for wrapper data providers that implements `DataProviderWrapper`. `AbstractDataView` won't throw exception anymore if wrapped data container is supported by the data view implementation. Adds public `getWrappedDataProvider` method in `DataProviderWrapper`.

Related-to: #18088
mshabarov pushed a commit that referenced this issue Dec 15, 2023
* feat: support DataProviderWrapper better in AbstractDataView

Adds in `AbstractDataView` a better support for wrapper data providers that implements `DataProviderWrapper`. `AbstractDataView` won't throw exception anymore if wrapped data container is supported by the data view implementation. Adds public `getWrappedDataProvider` method in `DataProviderWrapper`.

Related-to: #18088

* chore: changed getWrappedDataProvider to package-private
tltv added a commit to vaadin/docs that referenced this issue Dec 20, 2023
Document how to make callback for an item index with lazy data binding.

Related-to: vaadin/flow#18088
mshabarov added a commit that referenced this issue Dec 20, 2023
* feat: add getItemIndex to DataView

New `getItemIndex` method in `DataView` affects `ListDataView` and `LazyDataView` implementations. `LazyDataView` introduces in addition a new method `setItemIndexProvider(ItemIndexProvider)`.
With `ListDataView`, `getItemIndex` works out-of-the-box with the in-memory data.
With `LazyDataView`, it's required to set item index provider first with `setItemIndexProvider(ItemIndexProvider)`. Otherwise `getItemIndex` will throw `UnsupportedOperationException`. Provider can be implemented to fetch correct item index using item and `Query` parameters. `Query` object is set up to fetch all items with sorting and filter.

Fixes: #18088

* chore: moved ItemIndexProvider to component from DataCommunicator

* chore: added JavaDoc

* chore: pass correct filter for item index provider

* chore: update javadoc

* chore: added since 24.4

* chore: changed to return Optional<Integer>

* Update flow-data/src/main/java/com/vaadin/flow/data/provider/ItemIndexProvider.java

Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>

* chore: fixed formatting

---------

Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
mshabarov added a commit to vaadin/docs that referenced this issue Jan 11, 2024
* docs: update data-provider article for item index provider

Document how to make callback for an item index with lazy data binding.

Related-to: vaadin/flow#18088

* docs: fix vale findings

* First pass at editing.

* Vale fix

* Second full pass at editing.

---------

Co-authored-by: Russell J.T. Dyer <6652767+russelljtdyer@users.noreply.github.com>
Co-authored-by: Russell JT Dyer <russelljtdyer@users.noreply.github.com>
Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
tltv added a commit to vaadin/docs that referenced this issue Jan 11, 2024
* docs: update data-provider article for item index provider

Document how to make callback for an item index with lazy data binding.

Related-to: vaadin/flow#18088

* docs: fix vale findings

* First pass at editing.

* Vale fix

* Second full pass at editing.

---------

Co-authored-by: Russell J.T. Dyer <6652767+russelljtdyer@users.noreply.github.com>
Co-authored-by: Russell JT Dyer <russelljtdyer@users.noreply.github.com>
Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Vaadin 24.4.0.alpha1 and is also targeting the upcoming stable 24.4.0 version.

mshabarov added a commit to vaadin/docs that referenced this issue Jan 12, 2024
…3117)

* docs: update data-provider article for item index provider

Document how to make callback for an item index with lazy data binding.

Related-to: vaadin/flow#18088

* docs: fix vale findings

* First pass at editing.

* Vale fix

* Second full pass at editing.

---------

Co-authored-by: Russell J.T. Dyer <6652767+russelljtdyer@users.noreply.github.com>
Co-authored-by: Russell JT Dyer <russelljtdyer@users.noreply.github.com>
Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
rodolforfq pushed a commit that referenced this issue Feb 8, 2024
* feat: support DataProviderWrapper better in AbstractDataView

Adds in `AbstractDataView` a better support for wrapper data providers that implements `DataProviderWrapper`. `AbstractDataView` won't throw exception anymore if wrapped data container is supported by the data view implementation. Adds public `getWrappedDataProvider` method in `DataProviderWrapper`.

Related-to: #18088

* chore: changed getWrappedDataProvider to package-private
rodolforfq pushed a commit that referenced this issue Feb 8, 2024
* feat: add getItemIndex to DataView

New `getItemIndex` method in `DataView` affects `ListDataView` and `LazyDataView` implementations. `LazyDataView` introduces in addition a new method `setItemIndexProvider(ItemIndexProvider)`.
With `ListDataView`, `getItemIndex` works out-of-the-box with the in-memory data.
With `LazyDataView`, it's required to set item index provider first with `setItemIndexProvider(ItemIndexProvider)`. Otherwise `getItemIndex` will throw `UnsupportedOperationException`. Provider can be implemented to fetch correct item index using item and `Query` parameters. `Query` object is set up to fetch all items with sorting and filter.

Fixes: #18088

* chore: moved ItemIndexProvider to component from DataCommunicator

* chore: added JavaDoc

* chore: pass correct filter for item index provider

* chore: update javadoc

* chore: added since 24.4

* chore: changed to return Optional<Integer>

* Update flow-data/src/main/java/com/vaadin/flow/data/provider/ItemIndexProvider.java

Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>

* chore: fixed formatting

---------

Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging a pull request may close this issue.

3 participants