我有一个简单的实体
public class Vulnerability{
private String name;
private int probability;
private int damage;
}
并使用行编辑器进行网格
Grid<Vulnerability> vulnerabilityGrid = new Grid<Vulnerability>(Vulnerability.class);
vulnerabilityGrid.setColumns("name", "probability", "damage");
Grid.Column nameColumn = vulnerabilityGrid.getColumn("name");
Grid.Column probabilityColumn = vulnerabilityGrid.getColumn("damage");
Grid.Column damageColumn = vulnerabilityGrid.getColumn("probability");
绑定到字段的活页夹代码
Binder<Vulnerability> binderVulnerability = new Binder<Vulnerability>();
TextField nameTextFieldGrid = new TextField(), damageTextFieldGrid = new TextField(), probabilityTextFieldGrid = new TextField();
Binder.Binding<Vulnerability, Integer> probabilityBind = binderVulnerability.forField(probabilityTextFieldGrid).withNullRepresentation("").withConverter(new StringToIntegerConverter(Integer.valueOf(0), "integers only")).bind(Vulnerability::getProbability, Vulnerability::setProbability);
Binder.Binding<Vulnerability, Integer> damageBind = binderVulnerability.forField(damageTextFieldGrid).withNullRepresentation("") .withConverter(new StringToIntegerConverter(Integer.valueOf(0), "integers only")).bind(Vulnerability::getDamage, Vulnerability::setDamage);
nameColumn.setEditorBinding(nameBind);
probabilityColumn.setEditorBinding(probabilityBind);
damageColumn.setEditorBinding(damageBind);
和列顺序有关的问题,当我按行并激活编辑器时,我获得了概率列位置上的损坏字段编辑器,依此类推。
如何解决此问题?
答案 0 :(得分:0)
在这里查看您的代码
Grid.Column probabilityColumn = vulnerabilityGrid.getColumn("damage");
Grid.Column damageColumn = vulnerabilityGrid.getColumn("probability");
您将获得“损坏”列以引用probabilityColumn
,并使用具有getter和setter的概率的绑定对其进行设置,如下所示。
Binder.Binding<Vulnerability, Integer> probabilityBind = binderVulnerability.forField(probabilityTextFieldGrid).withNullRepresentation("").withConverter(new StringToIntegerConverter(Integer.valueOf(0), "integers only")).bind(Vulnerability::getProbability, Vulnerability::setProbability);
probabilityColumn.setEditorBinding(probabilityBind);
对于其他专栏,您反之亦然。因此它按编码方式工作。如您所见,Vaadin Grid和Binder中的API允许这种不对称性,因为可以分别定义getter和setter,这在您的情况下是编程错误,但在其他情况下,您可以利用此功能。