Doubts in SelectOneMenu Primefaces

0

I have a SelectOneMenu in my xhtml view. In it I have an onchange that on clicking calls a p: dialog to fill 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>

No p: dialog I have this 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 SelectOneMenu it makes the include of the file relative to the selected one.

Someone to help with this logic?

    
asked by anonymous 16.03.2018 / 19:04

2 answers

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 selected mechanical property and set it to the value of p: selectOneMenu. For example: Class BankBackupMembers:

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, simply use the rendered with the mechanicSelected 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 you have helped.

    
02.04.2018 / 06:27
0

If you only need the id and not the entire 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 change the itemValue to # {mec}

I hope I have helped.

    
03.04.2018 / 18:23