我不确定这是否是数据表的行扩展不当行为,还是我做错了什么。我将尽可能简单地描述问题。
首先,尝试制作一个类似于 PrimeFaces 展示的 DataTable,例如,您可以在其中“更改”汽车颜色的汽车列表。这很简单!
然后实现它rowExpansion
,以便ActionListener
通过 commandButton 对属性集进行部分处理。到那里为止,一切都很好。所以现在尝试展开最后一行,然后尝试展开中间的行,然后单击commandButton
第一个展开的行,然后您会注意到属性ActionListener
集将指向或与第二个展开的行相关。在我的示例中,我只是用 3 行填充数据表。
如果您尝试仅扩展一行,则会得到预期的行为。
扩展过程似乎覆盖了先前处理的模型或某种远离知识的其他不当行为。
请参阅以下片段。
<p:dataTable id="carsTable" var="car" value="#{bean.carsSmall}">
<p:ajax event="rowToggle" listener="#{bean.onRowToggle}" />
<p:column style="width:2%">
<p:rowToggler />
</p:column>
<p:column style="width:49%">
<h:outputText value="#{car.model}" />
</p:column>
<p:column style="width:49%">
<h:outputText value="#{car.year}" />
</p:column>
<p:rowExpansion>
<ui:repeat var="color" value="#{bean.availableColors}">
<p:commandButton action="#{bean.chooseColor}" value="#{color.color}">
<f:setPropertyActionListener target="#{bean.selectedColor}" value="#{color}" />
</p:commandButton>
</ui:repeat>
</p:rowExpansion>
</p:dataTable>
豆
@ManagedBean
@SessionScoped
public class Bean implements java.io.Serializable {
private static final long serialVersionUID = 1708652163041196763L;
private List<Car> carsSmall = new ArrayList<Car>();
private final List<Color> availableColors = new ArrayList<Color>();
private Color selectedColor;
public Bean() {
carsSmall.add(new Car("81025d15", "2011"));
carsSmall.add(new Car("44194657", "2012"));
carsSmall.add(new Car("482f2a60", "2013"));
}
public void clear(){
this.availableColors.clear();
}
public void chooseColor(){
System.out.println(this.selectedColor.getColor());
}
public void onRowToggle(ToggleEvent event) {
// This is a dummy logic for this example...
Car car = (Car) event.getData();
this.availableColors.clear();
if (car.getModel().equals("81025d15")) {
this.availableColors.add(new Color("RED"));
this.availableColors.add(new Color("GREEN"));
this.availableColors.add(new Color("BLUE"));
} else if (car.getModel().equals("44194657")) {
this.availableColors.add(new Color("YELLOW"));
this.availableColors.add(new Color("GRAY"));
this.availableColors.add(new Color("BLACK"));
} else if (car.getModel().equals("482f2a60")) {
this.availableColors.add(new Color("ORANGE"));
this.availableColors.add(new Color("BROWN"));
this.availableColors.add(new Color("PINK"));
}
}
// GETTERS and SETTERS...
}
对于那些指出我正在使用的事实的问题原因的人,这种不当行为<ui:repeat>
也会被注意到<h:dataTable>
<p:dataTable>
此示例不适用于将 bean 设置为既不是 asRequestScoped
也不是ViewScoped
。