vaadin 网格行编辑器更改列顺序

vaadin grid row-editor changes column order

我有一个简单的实体

 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);

还有列顺序问题,当我按下行并激活编辑器时,我会在 probabilityColumn 位置等处获得损坏字段的编辑器。

我怎样才能解决这个问题?

在此处查看您的代码

Grid.Column probabilityColumn = vulnerabilityGrid.getColumn("damage");
Grid.Column damageColumn = vulnerabilityGrid.getColumn("probability");

您正在获取 "damage" 列以引用 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 允许这种不对称,因为 getters 和 setters 可以单独定义,在您的情况下这是一个编程错误,但在其他情况下您可以利用此功能。