0

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;
    }
}
}
Kukeltje
  • 11,924
  • 4
  • 19
  • 44
  • Is the converter activated when you select? I haven't seen a converter like that, only @FacesConverter. What if you put a in there? – Jaqen H'ghar Aug 13 '16 at 07:24
  • @JaqenH'ghar Also stumbled upon this. Have a look at [this answer](http://stackoverflow.com/a/7665768/3736575). – irieill Aug 13 '16 at 07:35
  • Did you check [this answer](http://stackoverflow.com/a/2120183/3736575)? – irieill Aug 13 '16 at 07:37
  • @irieill You're absolutely right. Carlos Is the "new Categoria(...)" expecting a "codigo" as parameter? It seems to me id and codigo are not the same – Jaqen H'ghar Aug 13 '16 at 07:59

0 Answers0