user12106967
user12106967

Reputation:

Vaadin grid - change component column in one row only

I have a grid with several columns. For three columns i used the column renderer. Each of the columns contains one button. If i click one of those buttons, i want to replace the three buttons in that specific row with two other buttons. All the other rows should not be affected. Is this possible in a Vaadin grid?

Upvotes: 0

Views: 713

Answers (1)

kscherrer
kscherrer

Reputation: 5766

Components in different columns don't know each other, as they are all defined in a separate scope (in the componentRenderer of their own column. You cannot define the Button outside of the componentRenderer as you found out in another question today). So the "obvious" solution won't work, where you add a clickListener on the Button to directly change the other buttons.
If you had one column with 3 Buttons inside then this would be much easier.


There is a way, but I see this more as a hack than as a solution. Because you need some extra implementation in the item class for this to work.

In the ComponentRenderer, you can add an if-statement where you look at some value of the item. In one case, you'll render button 1, in the other case you'll render the other button. And in the click listeners of the button you change that value in the item and refresh the dataprovider, so the componentRenderer is invoked again. It will now see the value on the item has changed, therefore displaying some other Button.

Here is some code to show what I mean:

// grid item class
public class Foo {
    private boolean buttonPressed = false;

    public Foo(){

    }

    public isButtonPressed(){
        return buttonPressed;
    }

    public setButtonPressed(boolean buttonPressed){
        this.buttonPressed = buttonPressed;
    }
}

// adding of button columns
// do this 3 times for a test of your scenario. 
grid.addComponentColumn(item -> {
    if(!item.isButtonPressed()){
        return new Button("Before Button was Pressed", click -> {
            item.setButtonPressed(true);
            grid.getDataProvider().refresh(item); 
        });
    } else {
        return new Button("Button was Pressed", click -> {
            item.setButtonPressed(false);
            grid.getDataProvider().refresh(item);
        })
    }
})

Upvotes: 1

Related Questions