Grid with button click strange behavior

Hello,

I get case where I’m not sure why it doesn’t work as expected (Vaadin 24.7.6):

I have view with grid and modal.
Grid has custom column with button:

grid.addComponentColumn(item -> {
            Button btn = new Button(new Icon(VaadinIcon.EDIT), click -> {
                modal.show(item);
            });
            return btn;
        });

item has some fields like id, name, etc.

Clicking a buton with row opens modal with close/save buttons.

after saving, refreshGrid is invoked:

private void refreshGrid() {
        grid.select(null);
        grid.getDataProvider().refreshAll();
    }

The strange behavior is:

  • when I save unchanged data , I can click the same record again
  • in case of changing any data, grid is refreshed but I can click button only of others rows but not the one I just changed. I can repeat this many times, and always it doesn’t work

No exceptions, warnings in application/browser…

What am I doing wrong? Do I need any trick?

Thomas

My first guess is, that you have an equals/hash over all fields of your model, not just the id? At least that would be a trace why it is working, when you have not changed your item in the dialog.

Also the select(null) might be an issue, use deselectAll() instead (but I am not sure atM, if grid refresh all might do that even automatically)

If equals/hash and select are not helping:

What exactly is happening on the save action inside your dialog?
Do you save the data to jpa?
What kind of data provider do you use (set items with a list or a callback provider)?
Is there maybe other code, that you could provide, that helps with finding the issue?

Hi Stefan,

You’re right - the reason was that I used equals/hash over all fields (and actually Lombok’s @EqualsAndHashCode annotation)

Now I also noticed than now I can use

grid.getDataProvider().refreshItem(inputType);

instead of refreshing all.

Thank you for your help - I need to ofind the explanation for this.

Thomas

PS. I use JPA and setItems based on example:

grid.setItems(query -> inputTypeService.list(VaadinSpringDataHelpers.toSpringPageRequest(query)).stream());

Here is the documentation for it. :slight_smile:

Thanks! This will explain a lot to me.
T.

1 Like

Just a side note : take care when using Lombok with JPA entities.

The gist of it is, that Grid (and basically anything with a data
provider) has to “translate” between your server-side objects and the
client-side JS world.

This is done building a mapping between the two and the server-side uses
the hash-code from the objects.

The most common mistake is to not provide an implementation, the
fallback (object identity) is used, and things break. Overdoing it will
not work properly either, as you have already found out.