想要从jsf下拉框中选择一个值,然后点击添加按钮后需要在底部的下拉框中显示

时间:2018-06-29 14:06:56

标签: jsf

我将所有AppObjects保留在数组列表中,并能够在第一个下拉框中看到它们, 单击添加按钮后,在Java端需要执行的操作以及如何在表单的以下下拉框中显示所选对象 非常感谢任何帮助,我是JSF的新手。谢谢

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

<h:head>
    <title></title>
</h:head>
<h:body>
    <h:form>
        <a4j:outputPanel id="results" ajaxRendered="true">
            <h:panelGrid columns="7">
                <rich:panel header="APP Objects">
                    <rich:select defaultLabel="APP Objects" value="#{searchObjectinDB.chosenAppObject}" listWidth="200">
                        <f:selectItems value="#{searchObjectinDB.AppObjects}" /> 
                        //getting items as list here
                    </rich:select>                                              
                </rich:panel>
                &#160;&#160;&#160;
                <a4j:commandButton id="addAppObject" value="Add" 
                    action="#{searchObjectinDB.getObjectDetails()}" //Upon clicking add button, what needs to be done in this method to show in the below drop down box
                    reRender="selected_objects">
                </a4j:commandButton>
                &#160;&#160;&#160;

                <a4j:outputPanel id="selected_objects">
                    <h:panelGrid columns = "1">
                        <rich:panel header="Selected Objects" style="width: 100%">
                            //need to create another list and display the selected items from first list
                        </rich:panel>
                    </h:panelGrid>
                </a4j:outputPanel>
            </h:panelGrid>
        </a4j:outputPanel>
    </h:form>
<h:body>

我将所有AppObjects保留在数组列表中,并能够在下拉框中看到它们, 单击添加按钮后,在Java端需要执行的操作以及如何在下面的下拉框中显示所选对象 非常感谢您的帮助,我是JSF的新手

1 个答案:

答案 0 :(得分:1)

这非常简单,基本上,您只需要一个新集合即可保留所选项目。每次调用方法getObjectDetails时,都会将selectedAppObject变量添加到所选项目的集合中,就像这样:

public class SearchObjectinDB {

   Object chosenAppObject;
   List<Object> AppObjects;
   List<Object> selectedAppObjects = new ArrayList<>();

   //getters and setters

   public void getObjectDetails() {
      selectedAppObjects.add(chosenAppObject);
   }

}

您的XHTML代码还需要创建一个新列表并显示选择的项目:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

<h:head>
    <title></title>
</h:head>
<h:body>
    <h:form>
        <a4j:outputPanel id="results" ajaxRendered="true">
            <h:panelGrid columns="7">
                <rich:panel header="APP Objects">
                    <rich:select defaultLabel="APP Objects" value="#{searchObjectinDB.chosenAppObject}" listWidth="200">
                        <f:selectItems value="#{searchObjectinDB.AppObjects}" /> 
                        //getting items as list here
                    </rich:select>                                              
                </rich:panel>
                &#160;&#160;&#160;
                <a4j:commandButton id="addAppObject" value="Add" 
                    action="#{searchObjectinDB.getObjectDetails()}" //Upon clicking add button, what needs to be done in this method to show in the below drop down box
                    reRender="selected_objects">
                </a4j:commandButton>
                &#160;&#160;&#160;

                <a4j:outputPanel id="selected_objects">
                    <h:panelGrid columns = "1">
                        <rich:panel header="Selected Objects" style="width: 100%">
                            <a4j:repeat value="#{searchObjectinDB.selectedAppObjects}" var="item">
                                <tr>
                                    <td><h:outputText value="#{item.toString}" id="item1" /></td>
                                </tr>
                            </a4j:repeat>
                        </rich:panel>
                    </h:panelGrid>
                </a4j:outputPanel>
            </h:panelGrid>
        </a4j:outputPanel>
    </h:form>
<h:body>