如何关联两个selectManyListbox?

时间:2011-07-18 09:38:20

标签: java jsf-2 primefaces

我正在使用JSF2.0 / primefaces,我有两个selectManyListbox

   <h:selectManyListbox   value="#{ToolsJIRA.selectedProjects}"  size="3">
      <f:selectItems value="#{ToolsJIRA.getMyListProject()}" />

     </h:selectManyListbox>

      <h:selectManyListbox   value="#{TestAjax.selectedstatus}" size="3">
      <f:selectItems value="#{TestAjax.StepsByProject()}" />
     </h:selectManyListbox>

第二个selectManyListbox与第一个selectManyListbox相关(当用户选择第一个选项时,第二个将被填充), 我该如何联系他们?

1 个答案:

答案 0 :(得分:3)

使用<f:ajax>

<h:selectManyListbox value="#{bean.selectedProjects}" size="3">
    <f:selectItems value="#{bean.availableProjects}" />
    <f:ajax listener="#{bean.updateAvailableSteps}" render="steps" />
</h:selectManyListbox>

<h:selectManyListbox id="steps" value="#{bean.selectedSteps}" size="3">
    <f:selectItems value="#{bean.availableSteps}" />
</h:selectManyListbox>

类似

public void updateAvailableSteps() {
    availableSteps = new ArrayList<Step>();

    for (Project selectedProject : selectedProjects) {
        availableSteps.addAll(selectedProject.getSteps());
    }

    // If you want to filter duplicates, add the following line as well:
    // availableSteps = new ArrayList<Step>(new LinkedHashSet<Step>(availableSteps));
}

在输入组件中,<f:ajax>默认情况下会在每个change事件中触发。触发时,将调用listener属性中声明的bean方法,并更新render属性中定义的组件。