Vaadin Grid (Multiselect): restore selection after refresh - vaadin-flow

A Vaadin grid shows data which is constantly updated by a background process. A user might select one or more rows to carry out various functions. The user might refresh the data from the backend (which updates rows shown in the grid).
The application needs to restore the selected items after a grid refresh.
grid.getSelectedItems() has to return the current instance of the selected items.
Refresh is implemented as follows:
void refresh() {
final var beanSet = grid.getSelectedItems();
dataProvider.refreshAll(); // refresh from backend
grid.asMultiSelect().select(beanSet); // restore previously selected items
}
Updating the grid works fine, but the selection is only partly restored: the "selected" checkbox is checked for the items in beanSet but querying grid.getSelectedItems() still returns the old instances.
Reproducer: https://github.com/skiedrowski/vaadin-grid-restore-selection, package com.example.application.views.idstyle -> check the notification after clicking "Update selected".
What is the correct way to update the selected items?
Context:
Vaadin Flow 23, Grid Pro in multiselect mode
grid items implement equals and hashCode based on an immutable id
grid data provider is a ConfigurableFilterDataProvider fetching paged data from backend

I believe the problem in your sample project is that you're always recycling a set of selected objects which contain the "old" data. You read out a reference to the old items in var beanSet = grid.getSelectedItems(); and store them back into the selection with grid.asMultiSelect().select(beanSet);
A lazy-loading Grid can't know if the programmatically set selection is a collection of objects that are available in the backend - it would need to fetch the entire backing dataset to do that. So when the selection is updated from the server, it could be any objects of the correct type, whether they actually exist in the data set or not.
What could do yourself is pass the selection set to the backend, update the items and then pass them back to the Grid's selection.
An open question that remains is whether Grid should update the selection when a fetch returns items that are equal to items in the selection. I can't immediately tell if that is
a) possible, or
b) a sensible thing to do

This is how we solve this in our application:
A]
fresh items are retrieved lazily
the only time when refreshed selection is needed is when we want to operate with the selected items (such as edit or another action, otherwise obsolete selected items don't matter)
the backend is able to return fresh item by ID
there is no need to update the selection on fetch() (which could introduce inconsistencies if a part of the selection is already updated but the rest haven't been fetched yet)
B]
we have some data providers which just hold wrappers for actual items
so any interaction with the items fetches fresh data under the hood
for the record, this was not done to solve this problem but mitigates it as a sideeffect

Related

Vaadin Grid - Attempted to fetch more items from server than allowed in one go

~1500 data need to be fetched from DB. The code is pretty simple
List<Item> itemList = getItemsFromDB();
Grid<Item> grid = new Grid<>();
grid.addColumn(Item::getID).setHeader("Id").setAutoWidth(true);
grid.addColumn(Item::getName).setHeader("Name").setAutoWidth(true);
grid.setAllRowsVisible(true);
grid.setItems(itemList);
And I got this warning, only the first 1000 data are shown in the grid, the rest are just empty rows.
2022-04-15 15:46:52.475 WARN 19642 --- [nio-8080-exec-6] c.v.flow.data.provider.DataCommunicator : Attempted to fetch more items from server than allowed in one go: number of items requested '1583', maximum items allowed '1000'.
I know I can use lazy loading, but can I do it without it? The size of data will always be around 1500, and I don't actually care that much about how slow it is.
I am using vaadin 14.8.8
The problem comes from setAllRowsVisible and the DoS protection added inside the data provider. It's currently not easily possible to overwrite this without creating your own data provider.
I'm not really sure why you need all the rows visible; this increases the load time for your end user. If your only reason is to have the grid full height, you can just call setHeight on the grid and the let Vaadin handle the callbacks to the server to only fetch the amount of data needed to show on the client.

Showing random data from core-data using Swift 3

I need some help getting random data from core data using Swift 3, Xcode 8.3.1. I currently have an app that creates a list in tableview using data that is entered by the user.. (user enters a name and takes a picture of that person) The entity "Friend" holds the attributes "name", "image".
The first version of this app was just a name and I would use arc4random to randomly update a label with a name on a modally presented VC on a button click. The names were simply stored in an Array.
This version is including an image so I decided to try my hand at core-data (never used it before) and now I'm stuck at my random select button. Currently the app will store the data fine and then retrieve it and display everyone alphabetically along with their image in a tableview. As a new person is submitted the info gets stored and the tableview updates.
I need to show a randomly selected name and its image, but I don't know how to do this and research has failed me on getting it done.
If there is a better way of storing an image & name instead of core-data I'm open to changing as well. The app stores anywhere from 20-80 different names. It will never be used to store much more than that.
You can fetch your items from the context, which will give you an array of objects. Now you just use your favorite random function to get a random index for this array. And then use an object at that index.

Vaadin - Table column order

Anybody know how to/or it is possible - create a Table with column specific order; configuration order which was before save - example in DB,
and uploaded at specific view on? also I wonder how to take generate this columns headers and content from POJOS class - beans.
Any good ideas?
setVisibleColumns
The Table::setVisibleColumns does double-duty:
Controls which columns are visible, and
Sets the order in which the columns appear.
Call Table::getVisibleColumns to see current ordering.
Doc
This is well described in:
Book of Vaadin > Table
Sampler > User Interface > Data Presentation > Table
Table API JavaDoc
Example Code
Basically, you need the code like this to control columns order and also set list of bean instances as datasource.
Code is not tested, just a demonstration. Valid for Vaadin 6, but I guess no significant changes comparing to Vaadin 7.
table = new Table();
// Wrap your beans collection into vaadin data container. There are many
// types of them , check Book of Vaadin.
BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class)
container.addBean(new Bean());
// Set collection of your beans as data source.
// Columns will be created for each property, vaadin uses reflection.
table.setContainerDataSource( container );
// You can select to display only properties you want, not all.
// Order matters. You can get columns list from any source - for example
// store in your DB.
table.setVisibleColumns( new Object[] {"prop1", "prop2"} );
// You can set column headers (by default vaadin will set them same as bean
// properties names). Order matters, should match above order.
table.setColumnHeaders( new String[] {"Property 1", "Property2"} );
The answer by Sergey Makarov is correct. This answer provides further information.
User’s Reordering
You may allow the user to drag-and-drop columns in a table to re-order them at runtime. To enable this feature, call isColumnReorderingAllowed.
You can use a listener to be informed when such a reorder event occurs.
The user’s re-ordering lasts only for this work session. If you want to maintain the user’s order in future work sessions, you must somehow persist their desired order and apply the order when instantiating the table again.
Losing The Order
If you replace the data source of the table, your column order will be reset. You can get the current order before the change, then restore. Example code follows.
// Visible status & ordering.
java.lang.Object[] visibleColumns = this.exampleTable.getVisibleColumns();
// ------| Fresh Data |--------------
// Put fresh data into the Table.
this.exampleTable.setContainerDataSource( freshBeanItemContainer );
// ------| Restore Config |--------------
// Visible status & ordering.
this.exampleTable.setVisibleColumns( visibleColumns ); // Assumes the table has the same properties.
By the way, as with visibility and order, being collapsed will also be reset with fresh data. You may want to track and restore column collapsing as well as ordering.

Get previous value of an entity

I need to get previous value of an entity.
My requirement is like; I have some input fields in an edit page.
1 User can enter some values there and press save button at this time the user should be able to save it.
2 User can enter some values there and press Cancel button at this time the page should be reloaded with whatever values were there before the user start editing the page.
My question is that can entity frame work, help us in getting previous value of an object?
Is self tracking is something related to this?
You mentioned "page" so I guess you are talking about web application. In such case you should simply load entity from the database again because pushing Cancel button will make a new request to your web application. You should use a new context per request so you don't have any previous data or entity to reload - you will run a new query and get last data persisted to database.
What you would want to do is:
myContext.Refresh(RefreshMode.StoreWins, myObject);
This will ask the context to reload the entity removing any changes to the object and replacing the property values from the data store.

How to Reload custom section nodes Umbraco

I have custom Store section in Umbraco 4.5.2 which has list of all the stores coming from custom table.I want to reload the Store tree after updting store detail from code behind.
Thanls
when you create xml tree nodes to bind the custom section you have to add an option to xmlTreeNode for the node to get refreshed
xNode.Menu.AddRange(new List<IAction> { ActionNew.Instance, ActionRefresh.Instance, ActionDelete.Instance });
Here ActionRefresh.Instance will refresh the nodes

Resources