How to refresh Vaadin 8 Grid

I currently refresh Vaadin 8 grid by resetting its items:

List<File> content = ...
...
// delete one file
grid.setItems(content)

Overhead a part (no idea how much traffic resulting for sending all files instead of deleting a single row) this works
for insert/delete but not for update. If a file is renamed the old name comtinue unchanged on the grid. I tried markDirty
and markDirtyRecursive and nope. I also tried turning on/off the editor (like suggested for V7) and nope again.
Is there a method I can call?

I think you can use:
grid.getDataProvider().refreshItem(item); // (item = your updated item

Thanks Jean-Christophe, unhappily it doesn’t work too.

For me it’s working (i write a small example)

 pojos = new ArrayList<Pojo>();
        pojos.add(new Pojo("1","test1"));
        pojos.add(new Pojo("2","test2"));
        grid = new Grid<Pojo>();
        grid.addColumn(Pojo::getId).setCaption("id");
        grid.addColumn(Pojo::getValue).setCaption("value");
        
        grid.setItems(pojos);
        Button button = new Button("Click Me");
        button.addClickListener( e -> {
            pojos.get(0).setValue("NEW VALUE");
            grid.getDataProvider().refreshItem(pojos.get(0)); 
//grid.getDataProvider().refreshAll(); also work
        });
        
        layout.addComponents(grid, button);

Can you try with refreshAll ?
Does your updated object is equal to the old one ? (because refreshitem use equals to refresh the right row) ?

Hi Jean-Christophe, thanks indeed for the detaled example. Refresh all does nort work either.

But you hit the point: seems (from javadoc) that refreshItem uses either equals and getId method (probably getId if available first since all objects implements equals) to verify if item changed. In the case (java File) getId does not exist and equals in obviously insensitive to renaming (should be cause the file is still the same).

So the solution could be to wrap file in a class whose ID is an hash code based on its visible properties. But in this case I am little disappointed with V8. The same problem will apply to most business entities that will not change their ID or compare as different whenever some attribute change.

Now (
for the Vaadinn team
) come on! V7 to V8 broke the API (like V6 to V7), OK accettable for the improvements, I really appreciate them. But now I still have to wrap all my beans… Can’t at least provide a method to force redrawing of visible rows?

This in addition to use single-property beans for using validator when no bean is available. This way using V8 becomes overkill.

i don’t understand.
If you update a object the old version and the new version should be “equals”, no ?
refreshitem(Item item) refresh only the item when in your list when item.equals(itemInyourList).
So it should refresh your updated item.

For File:
equals(Object obj)
Tests this abstract pathname for equality with the given object.
If you change the pathname then equals is false and your file is not refreshed

Hi Jean-Christophe, thanks again. I finally found the problem: it was an exception that went unnoticed and occurred before refresh. Now all methods (refresh) work fine and I do not even need to replace the grid content (other cases).

I am now investigating why the exception was not trapped by the error handler but instead was written directly to tomcat output (that I foolishly did not verify counting on error handler).

Found the reason why the exception was not trapped by my error handler. I use a modal dialog to confirm and, upon confirmation, I close the dialog and perform the confirm action. Since the dialog is closed the framework picks the wrong client connector and consequantly error handler. Inverting actions resolves the problem: first execute confirmed actions then close the modal window. The only drawback is that this is little strange, at least if compared with traditional desktop UI whose confirm box is closed immediately: if you have firther messages the confirm box is still open.

Jean-Christophe Gueriaud:
For me it’s working (i write a small example)

pojos = new ArrayList<Pojo>();
        pojos.add(new Pojo("1","test1"));
        pojos.add(new Pojo("2","test2"));
        grid = new Grid<Pojo>();
        grid.addColumn(Pojo::getId).setCaption("id");
        grid.addColumn(Pojo::getValue).setCaption("value");
        
        grid.setItems(pojos);
        Button button = new Button("Click Me");
        button.addClickListener( e -> {
            pojos.get(0).setValue("NEW VALUE");
            grid.getDataProvider().refreshItem(pojos.get(0)); 
//grid.getDataProvider().refreshAll(); also work
        });
        
        layout.addComponents(grid, button);

Can you try with refreshAll ?
Does your updated object is equal to the old one ? (because refreshitem use equals to refresh the right row) ?

Fails to update row on RefreshItem on Grid. Using DataProvider with getId configured and equals overridden for the object. Please find my question here https://vaadin.com/forum/thread/17234067.

Aprreciate if you could help me solve this problem.

Sorry, I do not use V8 any more.

Hello,

I have same issue but not for an updating an item.

I have a list of items in grid and in some events i need to insert new item in grid.

When i refresh newly created item than changes doesn’t reflect to ui.

This same issue i tasted in vaadin flow and It working perfect but not works in vaadin 8.

If I well remember V8 You can call

grid.getDataProvider().refreshAll(); 

(after insertion).

Franco Graziosi:
If I well remember V8 You can call

grid.getDataProvider().refreshAll(); 

(after insertion).

Yes You are right and I am doing a same things and it works perfect.

But in my case i am using components in every column of grid and when i refresh all items it looks to user that whole grid is refresh so i want to just refresh the single item after inserting a new item.

I also tried with server push

access(() ->{
// insert item
// refresh item
});

That’s works when you scroll the grid than it appears new item.

In my case, Newly created item will be place as first row but that item appears when i scroll the grid.

Had same issue with Grid using BackEndDataProvider.

Button action to reload data does not work.

Tried using refreshAll()

it triggers call to my data provider implementation of refresh all

            @Override
            public void refreshAll()

My Workaround:

in my Grid derivation, keep a pointer onmy last created BackEndDataProvider

        lastDataProvider = new BackEndDataProvider

then when refresh is needed, just reassign this very Data Provider to the Grid

    //lastDataProvider.refreshAll(); <== does not work
    setDataProvider(lastDataProvider);  // <== fake changing provider will trigger the refresh !

Same for me with 8.8.5