本文介绍了推送后如何更新Vaadin Grid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从其他UI实例对网格进行更改后,我试图在Vaadin中更新网格.

I'm trying to update a grid in Vaadin after a change has been made to the grid from a different UI instance.

为了详细说明,我有两个不同的UI实例,UI1和UI2(来自两个不同浏览器的选项卡).这些UI实例包含一个视图,该视图包含一个由Contact对象组成的网格,这些对象具有各种属性,例如姓名,年龄,性别...

To elaborate, I have two different UI instances, UI1 and UI2 (tabs from two different browsers). These UI instances contain a view which contains a grid consisted of Contact objects that have various properties such as name, age, gender...

我从UI1向网格中添加了一个联系人,我希望UI2可以立即查看更新后的网格而无需刷新.

I add a Contact to the grid from UI1 and I want UI2 to see the updated grid instantly without having to refresh.

要做到这一点,我知道我必须使用已经使用Broadcaster类设置的Vaadin Push.我的UI类中有一个receiveBroadcast()方法,每当View类使用broadcast()方法向UI发送广播时,该方法都会更新UI.我的UI类的一部分看起来像这样:

To accomplish that, I know I have to use Vaadin Push which I already set up using a Broadcaster class. I have a receiveBroadcast() method in my UI class which will update the UI whenever a View class sends a broadcast to UIs using broadcast() method. Part of my UI class looks like this:

@Title("Contact Book")
@Theme("reindeer")
@Push
public class UserInterface extends UI implements BroadcastListener{

    private Navigator navigator;

    @Override
    protected void init(VaadinRequest request){
        navigator = new Navigator(this, this);
        navigator.addView(ContactBookView.NAME, new ContactBookView());

        Broadcaster.register(this);
    }
    @Override
    public void detach(){
        Broadcaster.unregister(this);
        super.detach();
    }

    @Override
    public void receiveBroadcast(Grid grid) {
        access(()->{

            // update grid here
            Notification.show("Grid updated", Notification.Type.TRAY_NOTIFICATION);
        });
    }
}

我的Broadcaster类与此相同链接(我只是稍微改变了方法,以便可以将网格作为参数传递)

And my Broadcaster class is as the same in this link (I just altered the methods a bit so I can pass the grid as a parameter)

然后在我的ContactBookView类中,添加或更改项目后调用Broadcaster.broadcast(grid)方法,以便可以以某种方式在UI类的access()方法中更新网格,但是我尝试过的任何方法似乎均不起作用.我知道推送有效,因为收到广播后,我可以在所有UI实例中看到网格已更新"消息.

Then in my ContactBookView class I call Broadcaster.broadcast(grid) method after adding or changing an item, so that I can update the grid somehow in my UI class' access() method but nothing I tried seems to work. I know that push is working because I can see the "Grid updated" message in all UI instances when broadcast is received.

我试图删除视图并重新初始化

I tried to remove the view and re-initialize

navigator.removeView(ContactBookView.NAME);
navigator.addView(ContactBookView.NAME, new ContactBookView());

试图完全清空网格并重新填充

Tried to completely empty the grid and refill

grid.removeAllColumns();
grid.setContainerDataSource(container);

试图通过这种丑陋的解决方法强制网格进行自我更新

Tried to force the grid to update itself using this ugly workaround

grid.setContainerDataSource(grid.getContainerDataSource());

试图执行JavaScript查询以强制UI进行同步

Tried to execute a JavaScript query to force the UIs to sync

JavaScript.getCurrent().execute("javascript:vaadin.forceSync();");

但是这些都不起作用.也许我在做别的事情吗?仅供参考,我的Vaadin版本是7.5.0.

But none of these worked. Maybe I'm doing something else wrong? FYI my Vaadin version is 7.5.0.

我希望收到任何反馈或建议,

I'd appreciate any kind of feedback or advice,

谢谢.

推荐答案

在重新初始化当前视图后,我设法通过重新导航到当前视图来解决了该问题.最终的UI访问方法如下:

I managed to solve the issue by re-navigating to the current view after I reinitialized it. Final UI access method looks like this:

public void receiveBroadcast() {
    access(()->{

        navigator.removeView(ContactBookView.NAME);
        navigator.addView(ContactBookView.NAME, new ContactBookView());
        navigator.navigateTo(ContactBookView.NAME);
        Notification.show("Grid updated", Notification.Type.TRAY_NOTIFICATION);
    });

这样,视图将在后台刷新并推送到UI实例,而无需进行物理刷新.

With this, the view gets refreshed in the background and pushed to the UI instances without having to refresh physically.

我确定可能会有更好的解决方法,这种方法看起来很难看,但目前可以满足我的需求.但是,我仍然会感激任何其他方法.

I'm sure there might be better workarounds and this one seems kind of ugly but it currently satisfies my needs. However, I'd still appreciate any different approach.

这篇关于推送后如何更新Vaadin Grid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:04
查看更多