Vaadin 8.5.1- 行更新后刷新网格

2022-01-18 00:00:00 grid refresh java vaadin vaadin8

我正在使用 Vaadin 8.5.1 Grid 来显示 1000 行.一旦通过更改它的属性更新了一行,我使用grid.getDataProvider().refreshItem(selectedRow)或者grid.getDataProvider().refreshAll() 更新行失败.

我需要做显式的 grid.setItems() 来查看行的更新属性.

我正在使用下面的代码片段来创建一个网格

 msgGrid = new ABSMsgGrid();列出<ConsoleEntry>messageEntryList = new ArrayList<>();如果(输入控制台!= null){messageEntryList.addAll(inputConsole.getMessageEntryList());}msgGridDataProvider = new ListDataProvider(messageEntryList) {@覆盖公共对象 getId(ConsoleEntry 项) {返回 item.getId();}};msgGrid.setDataProvider(msgGridDataProvider);//在更改网格行的属性时,我使用下面的代码片段私人无效句柄HideRowMenuItem(GridContextMenu contextMenu,ConsoleEntry selectedConsoleItem){if (!selectedConsoleItem.isHidden()) {hideRowMenuItem = contextMenu.addItem("隐藏行", VaadinIcons.EYE_SLASH, selectedMenuItem -> {selectedConsoleItem.hide();**msgGridDataProvider.refreshItem(selectedConsoleItem);**}});}}公共类 ConsoleEntry {@覆盖公共布尔等于(对象 obj){//TODO 自动生成的方法存根if (obj instanceof ConsoleEntry) {ConsoleEntry temp = (ConsoleEntry) obj;字符串 msgRef2 = temp.getMsgRef();返回 this.getMsgRef().equalsIgnoreCase(msgRef2);}返回假;}@覆盖公共 int hashCode() {//TODO 自动生成的方法存根返回 super.hashCode();}公共字符串 getId(){返回 this.getMsgRef();}}

我见过类似的问题,但没有一个解决方案有效.

更改后如何刷新 vaadin Grid什么?

Vaadin - 行修改后刷新网格

如果有人可以分享如何解决这个问题的指针

TIA

解决方案

要让一个项目被视为同一个项目,(并且刷新工作)你需要一个正确实现的 equals() 和对象上的 hashCode() 方法.

来自文档

<块引用>

public void refreshItem(T item)

从接口复制的描述:DataProvider

刷新给定的项目.这种方法应该用于通知所有DataProviderListeners 项目已被更新或替换为新实例.

要使其正常工作,该项目必须实现

equals( Object) 和 #hashCode() 认为旧项目实例和新项目实例相等,或者

DataProvider. getId( Object) 应该被实现以返回一个适当的标识符.

除此之外,您应该创建一个 ListDataProvider,将其分配给网格,然后通过先前分配的 ListDataProvider 的同一实例进行更新p>

I am using Vaadin 8.5.1 Grid to display 1000's of rows. Once a row is updated with change in it property, I use grid.getDataProvider().refreshItem(selectedRow) or grid.getDataProvider().refreshAll() which fails to update the row.

I need to do explicit grid.setItems() to see the updated property of the row.

I am using below snippet to create a Grid

    msgGrid = new ABSMsgGrid();

    List<ConsoleEntry> messageEntryList = new ArrayList<>();
    if (inputConsole != null) {
        messageEntryList.addAll(inputConsole.getMessageEntryList());
    }

    msgGridDataProvider = new ListDataProvider<ConsoleEntry>(messageEntryList) {

        @Override
        public Object getId(ConsoleEntry item) {
            return item.getId();
        }
    };

    msgGrid.setDataProvider(msgGridDataProvider);



//on changing property of the grid row, i use the below snippet
private void handleHideRowMenuItem(GridContextMenu<ConsoleEntry> contextMenu, ConsoleEntry selectedConsoleItem) {
        if (!selectedConsoleItem.isHidden()) {
            hideRowMenuItem = contextMenu.addItem("Hide Row", VaadinIcons.EYE_SLASH, selectedMenuItem -> {
                    selectedConsoleItem.hide();
                    **msgGridDataProvider.refreshItem(selectedConsoleItem);**
                }
            });
        }
}

public class ConsoleEntry {

        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if (obj instanceof ConsoleEntry) {
                ConsoleEntry temp = (ConsoleEntry) obj;
                String msgRef2 = temp.getMsgRef();
                return this.getMsgRef().equalsIgnoreCase(msgRef2);
            }
            return false;
        }

        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            return super.hashCode();
        }

        public String getId(){
            return this.getMsgRef();
        }
}       

I have seen similar question but none of the solutions worked.

How to refresh the vaadin Grid after you change something?

Vaadin - Refresh grid after row modification

Appreciate if any one could share pointers on how to solve this problem

TIA

解决方案

For a item to be seen as the same item, (and the refresh working) you need a corretly implemented equals() and hashCode() methods on the object.

From the documentation

public void refreshItem(T​ item)

Description copied from interface: DataProvider

Refreshes the given item. This method should be used to inform all DataProviderListeners that an item has been updated or replaced with a new instance.

For this to work properly, the item must either implement

equals(​Object) and #hashCode() to consider both the old and the new item instances to be equal, or alternatively

DataProvider.​getId(​Object) should be implemented to return an appropriate identifier.

In addition to this, it's you should create a ListDataProvider, assign it to the grid and then do the updated via the same instance of the previously assigned ListDataProvider

相关文章