Updating data in Vaadin's grid

烈酒焚心 提交于 2019-12-11 16:49:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!