【发布时间】:2019-10-25 13:05:55
【问题描述】:
我有一个包含几列的网格。对于三列,我使用了列渲染器。每一列都包含一个按钮。 如果我单击其中一个按钮,我想用另外两个按钮替换该特定行中的三个按钮。所有其他行不应受到影响。这在 Vaadin 网格中是否可行?
【问题讨论】:
标签: vaadin
我有一个包含几列的网格。对于三列,我使用了列渲染器。每一列都包含一个按钮。 如果我单击其中一个按钮,我想用另外两个按钮替换该特定行中的三个按钮。所有其他行不应受到影响。这在 Vaadin 网格中是否可行?
【问题讨论】:
标签: vaadin
不同列中的组件彼此不知道,因为它们都在单独的范围内定义(在它们自己列的 componentRenderer 中。您不能在 componentRenderer 之外定义 Button,正如您今天在另一个问题中发现的那样) .因此,“显而易见”的解决方案将不起作用,您可以在 Button 上添加一个 clickListener 以直接更改其他按钮。
如果你有一列里面有 3 个按钮,那么这会容易得多。
有一种方法,但我认为这更像是一种技巧,而不是一种解决方案。因为您需要在 item 类中进行一些额外的实现才能使其正常工作。
在 ComponentRenderer 中,您可以添加一个 if 语句来查看项目的某些值。在一种情况下,您将渲染按钮 1,在另一种情况下,您将渲染另一个按钮。在按钮的单击侦听器中,您更改项目中的该值并刷新数据提供程序,因此再次调用 componentRenderer。现在它将看到项目上的值已更改,因此显示了其他一些按钮。
这里有一些代码来说明我的意思:
// 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);
})
}
})
【讨论】: