我有一个迭代对象列表的 dataGrid。在每个网格中,我有一个 commandButton 和一个标签,它试图将所选对象的 Id 放在支持 bean 中,但是当执行操作时,以及在属性的 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 放在外面而不是嵌套在数据表中,它就可以工作。但我希望它在 rowExpansion 中。
仅当我将数据网格放在包含的 tabView 之外时才有效。