Doubt in Selectonemenu Primefaces

Asked

Viewed 103 times

0

I have a Selectonemenu in my xhtml view. I have an onchange in it that when clicking calls a p:dialog to fill out a form. Follow selectOneMenu

<p:column headerText="Mecânica">
                        <div id="mecanicasAll">
                            <p:selectOneMenu onchange="PF('cadastraMecanica').show();"
                                id="mecanica" rendered="#{document.type == 'Pergunta'}"
                                value="#{bancoPerguntasMBean.mecanicas}" effect="fold">
                                <f:selectItem itemLabel="Selecione a mecânica"
                                    noSelectionOption="true" />
                                <f:selectItems value="#{bancoPerguntasMBean.mecanicas}" />
                            </p:selectOneMenu>
                        </div>
                    </p:column>

p:dialog I have it here

<h:form id="cadastraMecanica">
            <p:dialog style="text-align: center" header="Cadastrar Mecânica"
                widgetVar="cadastraMecanica" resizable="false" modal="true"
                width="1050" height="630">

                <c:if test="#{bancoPerguntasMBean.mecanicas == 'QUIZ'}">
                    <ui:include src="bancoPerguntasQuestaoQuiz.xhtml" />
                </c:if>

                <c:if test="#{bancoPerguntasMBean.mecanicas == 'VERDADEIROFALSO'}">
                    <ui:include src="bancoPerguntasQuestaoVerdadeiroFalso.xhtml" />
                </c:if>

                <c:if test="#{bancoPerguntasMBean.mecanicas == 'DESCRITIVA'}">
                    <ui:include src="bancoPerguntasQuestaoDescritiva.xhtml" />
                </c:if>

                <c:if test="#{bancoPerguntasMBean.mecanicas == 'ASSOCIACAO'}">
                    <ui:include src="bancoPerguntasQuestaoAssociacao.xhtml" />
                </c:if>

                <c:if test="#{bancoPerguntasMBean.mecanicas == 'ARRASTASOLTA'}">
                    <ui:include src="bancoPerguntasQuestaoArrastaSolta.xhtml" />
                </c:if>



            </p:dialog>
        </h:form>

What I really need is that depending on the value that is selected in the Selectonemenu it does the include of the file referring to the selected.

Someone to help with that logic?

  • JSTL with primefaces doesn’t usually work well, you tried with an outputPane tag using the rendered property?

  • Ola Giuliana, thanks for the reply, I thought better and changed to outputpanel. However my doubt is on how to redeem the value selected by the user in selectOneMenu, understand. Clicking on the value will open the p:dialog, but before opening I need to send the selected value to the Bean, in order to render?

2 answers

0

If you only need the id and not the whole object, you can do the following:

Bean:

private List<mecanica> mecanicas;
private Long idMecanicaSelecionado;

view:

 <p:column headerText="Mecânica">
                            <div id="mecanicasAll">
                                <p:selectOneMenu onchange="PF('cadastraMecanica').show();"
                                    id="mecanica" rendered="#{document.type == 'Pergunta'}"
                                    value="#{bancoPerguntasMBean.idMecanicaSelecionado}" converter="#{objectConverter}" effect="fold">
                                    <f:selectItem itemLabel="Selecione a mecânica"
                                        noSelectionOption="true" />
                                    <f:selectItems value="#{bancoPerguntasMBean.mecanicas}" var="mec" itemLabel="#{mec.nome}" itemValue="#{mec.id}" />
                                </p:selectOneMenu>
                            </div>
                        </p:column>

If you need the whole object, just change the type of the variable to the object and in itemValue change to #{Mec}

I hope I’ve helped.

0

Michael,

I saw that you used the mechanical variable in p:selectOneMenu and also in f:selectItems, in this case you can create a mechanical property Selected and set it in the value of p:selectOneMenu. For example: Class Bancoperguntasmbean:

private List<mecanica> mecanicas;
private Mecanica mecanicaSelecionada;

View:

<p:column headerText="Mecânica">
                        <div id="mecanicasAll">
                            <p:selectOneMenu onchange="PF('cadastraMecanica').show();"
                                id="mecanica" rendered="#{document.type == 'Pergunta'}"
                                value="#{bancoPerguntasMBean.mecanicaSelecionada}" effect="fold">
                                <f:selectItem itemLabel="Selecione a mecânica"
                                    noSelectionOption="true" />
                                <f:selectItems value="#{bancoPerguntasMBean.mecanicas}" />
                            </p:selectOneMenu>
                        </div>
                    </p:column>

When using it in the dialog just use the rendered with the mechanically variable. Example:

<p:column rendered="#{bancoPerguntasMBean.mecanicaSelecionada == 'QUIZ'}"
    <ui:include src="bancoPerguntasQuestaoQuiz.xhtml" />
</p:column>

You can also create a method to do this check if you prefer and call it instead of rendered.

I hope it helped.

  • I tried the proposed solution, but without success, when selecting an item of selectOneMenu, the dialog opens empty, as, with this no include was done. One detail is that List<mecanica> mecanicas; is an object, when performing the gets and sets I get the id of the selected mechanic and Seto for the idMecanica attribute, so I try to check in the dialog <p:outputPanel rendered="#{bancoPerguntasMBean.idMecanica == 1}">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.