我有一个迭代对象列表的dataGrid。在每个网格中,我有一个commandButton和一个标记,它试图将所选对象的Id放在支持bean中,但是当执行action时,以及属性的setter方法中,该值为null。
这是我的代码:
<p:dataGrid var="element" value="#{CentroController.profiles}" columns="3"
rows="10" paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" >
<p:panel header="#{CentroController.getDescripcionRoot(element)}" style="text-align:center">
<h:panelGrid columns="2" style="width:100%">
<p:panel>
<p:commandButton value="Borrar perfil" icon="ui-icon-trash" update=":form:tabView:profileButtonPanel" action="#{CentroController.deletePerfil}">
<f:setPropertyActionListener value="#{element.id}" target="#{CentroController.selectedItemId}" />
</p:commandButton>
</p:panel>
</h:panelGrid>
</p:panel>
</p:dataGrid>
在支持bean中:
public void deletePerfil()
{
System.out.println("SELECTED ITEM ID: " + this.selectedItemId);
// selectedItemID is always null
}
调用setter时,该值也为null:
public void setSelectedItemId(BigDecimal selectedItemId) {
System.out.println(selectedItemId); // Value is null
this.selectedItemId = selectedItemId;
}
感谢。
编辑:bean的代码(我删除了不相关的方法)。
/* imports */
@Named("CentroController")
@SessionScoped
public class CentroController implements Serializable{
private @Inject CentrosDAO dao;
private @Inject CentrosCanalesDAO daoServicios;
private @Inject CatalogoCentrosLaboratorioDAO habitualLabDAO;
private @Inject CatalogoCentrosRadiologiaDAO habitualRadDAO;
private List<Centros> centros;
private Centros selectedCentro;
private List<CentrosCanales> selectedCanales=new ArrayList<CentrosCanales>();
private List<CentrosCanales> listado;
private CentrosCanales selected;
private CatalogoCentros selectedItem;
private BigDecimal selectedItemId;
private TreeNode root;
private DualListModel<Catalogo> listCatalogo;
private DualListModel <Catalogo> listCatalogoLabHabitual;
private DualListModel <Catalogo> listCatalogoRadHabitual;
private CatalogoCentros perfilNuevo=new CatalogoCentros();
private @Inject CentrosCanalesDAO consulta;
private @Inject CatalogoCentrosDAO consulta1;
private @Inject CatalogoDAO consulta2;
private boolean servicioIsSelected; //, perfilIsSelected, pruebaIsSelected;
private List<CatalogoCentros> profiles;
public CentroController() {
}
@PostConstruct
public void init(){
this.centros=dao.obtenListaCentros();
servicioIsSelected = false;
//perfilIsSelected = false;
}
public BigDecimal getSelectedItemId() {
return selectedItemId;
}
public void setSelectedItemId(BigDecimal selectedItemId) {
System.out.println(selectedItemId);
this.selectedItemId = selectedItemId;
}
public void deletePerfil()
{
System.out.println("SELECTED ITEM ID: " + this.selectedItemId);
}
}
更新
dataGrid位于数据表中的rowExpansion标记内。我意识到如果我把dataGrid放在外面而不是嵌套在datatable中,它就可以了。但是我想在rowExpansion中使用它。
它只能工作如果我将datagrid放在tabView所在的位置之外。
答案 0 :(得分:2)
有关CatalogoCentros的一些问题:
如果问题2为“否”,请执行以下操作:
@Override
public int hashCode() {
return (id != null) ? id.intValue() : 0;
}
@Override
public boolean equals(final Object obj) {
if (obj instanceof CatalogoCentros) {
CatalogoCentros item = (CatalogoCentros) obj;
return item.getId().equals(getId());
}
return false;
}
在 equals 方法中设置一个断点,看看当你在dataGrid中选择某些东西时它会被触发。