问题
Hello everyone my problem is that my grid doesn't update data.
I've a Thread that get new values from a database but when I assign the new DataProvider from the collection that get new values, the grid doesn't change anything.
For example, the Grid contains the orders from clients, every 30 seconds a Thread search if there are new orders available, then y get all the orders in an ArrayList and set the new ArrayList in the grid’s DataProvider but the Grid continues having the same values as before.
How can I refresh data in the Grid without refresh all the web page?
Sorry for my English
Thank you.
回答1:
Poll
You can set browser to check servers for updates with given interval. I have added couple of code samples how to use poll interval for this.
For example, If you want the browser to update every 30 seconds then you can enable polling like this:
MyUI ui = getMyUi();
ui.setPollInterval(30000);
if (ui.isPollListenerRegistered()) {
Registration r = ui.addPollListener(this::reloadFromDatabase);
ui.setPollRegistration(r);
}
Setting pollInterval to value greater than -1 makes browser to call server with the given delay. Adding a poll listener allows you to notice these checks on server and load from database.
If only 1 page needs to refresh like this then add navigation listener as well which can set poll interval back to -1 so that other pages do not send unnecessary poll events.
MyUI ui = getMyUi();
ui.setPollInterval(-1);
if (ui.isPollListenerRegistered()) {
ui.getPollRegistration().unregister();
}
There is some documentation in Vaadin wiki but you do not need a background thread if you can read values from database.
https://vaadin.com/wiki?p_p_id=36
Push
Using Vaadin Push you can update data in the background and then request only clients that should be updated to refresh. This allows you to optimise requests because clients do not do unnecessary polling. You can also have single request to get data from web service and then update all clients with the same data.
You can read more in Vaadin documentation:
https://vaadin.com/docs/-/part/framework/advanced/advanced-push.html
I pushed an example project that shows how to use a single repeating background thread to do updated to data and then update contents of a grid with it. There are more than just a few lines of code for a complete push example so you can find the code here:
https://github.com/m1kah/vaadin-grid-push
来源:https://stackoverflow.com/questions/44352911/updating-data-in-vaadins-grid