I have a p:selectOneMenu
that changing value should run a listener to update the value of another p:selectOneMenu
(Type: state -> city). In addition to the items have the item "All" in the first p:selectOneMenu
. When you select any item other than the "All" the listener is not called, but when I select the "All" option.
I also realized that the model will not be updated. That is the value of the first p:selectOneMenu
is not set in the entity attribute,
The following codes:
First
<p:selectOneMenu id="pesquisaCategoria" style="width:100%;"
value="#{inscricaoFaces.entity.categoriaSelecionada}"
styleClass="input-block-level" converter="#{categoriaConverter}">
<f:selectItem itemLabel="#{msgs['label.dropdown.todos']}"
noSelectionOption="true" itemValue="#{null}" />
<f:selectItems value="#{categoriaFaces.listarTodasCategorias()}"
var="categoria" itemValue="#{categoria}"
itemLabel="#{categoria.descricao}" />
<p:ajax update="pesquisaAreaAtuacao" event="change"
listener="#{inscricaoFaces.atualizarAreasAtuacao}" />
</p:selectOneMenu>
Second
<p:selectOneMenu id="pesquisaAreaAtuacao" style="width:100%;"
value="#{inscricaoFaces.entity.areaAtuacao}"
styleClass="input-block-level" converter="#{areaAtuacaoConverter}">
<f:selectItem itemLabel="#{msgs['label.dropdown.todos']}"
noSelectionOption="true" itemValue="#{null}" />
<f:selectItems value="#{inscricaoFaces.areasAtuacao}" var="area"
itemValue="#{area}" itemLabel="#{area.descricao}" />
</p:selectOneMenu>
Convert
@ManagedBean
@RequestScoped
public class CategoriaConverter implements Converter, Serializable {
/**
*
*/
private static final long serialVersionUID = -1073909086688882110L;
@EJB
private CategoriaService categoriaService;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if (value != null && value.trim().length() > 0 && !"Todos".equals(value)) {
return categoriaService.consultarPorId(new Categoria(Integer.valueOf(value)));
} else {
return null;
}
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {
if(value != null){
return value instanceof Categoria ? String.valueOf(((Categoria) value).getCodigo()) : null;
}else{
return null;
}
}
}