0 Replies Latest reply on Dec 4, 2012 12:20 AM by songjinglim

    <h:selectOneMenu display prevoius value, how to reinit it again?

    songjinglim Newbie

      When click on <a4j:commandLink>, it will call resetCurrentValue method and doing reinit the field values.  Once completed, a popup will be shown.  However the drop-down list in that popup panel still display previous value.

       

      If I include render=":popupPanel:form2:firstList" in <a4j:commandLink>, drop-down list is reinit however, it will trigger event change listener when click the drop-down value second time.... 

       

      **[JSF]**

       

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          <ui:composition xmlns="http://www.w3.org/1999/xhtml"

              xmlns:h="http://java.sun.com/jsf/html"

              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"

              xmlns:c="http://java.sun.com/jsp/jstl/core">

             

              <ui:define name="body">

                  <h:outputStylesheet library="css" name="application.css" />

                  <f:loadBundle basename="com.cea.common.i18n.message" var="msg" />

                  <f:loadBundle basename="com.cea.common.i18n.admin_message"

                      var="adminMsg" />

         

                  <h:panelGrid id="searchPanel" cellspacing="6">

                      <h:form rendered="true" id="form1">

                          <rich:panel style="width:800px">

                              <a4j:commandLink id="addQueryCriteria" execute="@this" action="#{selectsBean.resetCurrentValue}"    

                                  render="popupPanel"                   

                                  oncomplete="#{rich:component('popupPanel')}.show()">show

                              </a4j:commandLink>

                          </rich:panel>

                      </h:form>

                  </h:panelGrid>

         

                  <rich:popupPanel id="popupPanel"

                      domElementAttachment="parent" width="320" autosized="true"

                      followByScroll="true" header="#{adminMsg.queryCriteria}">

                      <rich:messages for="formAddQueryCriteria" showDetail="true"

                          showSummary="true" />

                      <h:form id="form2">

                          <h:selectOneMenu value="#{selectsBean.currentType}" id="firstList"

                              valueChangeListener="#{selectsBean.valueChanged}">

                              <f:selectItems value="#{selectsBean.firstList}" />                   

                              <a4j:ajax event="valueChange" render="second" execute="@this" />

                          </h:selectOneMenu>

                          <a4j:outputPanel id="second" layout="block">

                              <h:selectOneMenu value="#{selectsBean.currentType}"

                                  rendered="#{not empty selectsBean.currentType}">

                                  <f:selectItems value="#{selectsBean.secondList}" />

                              </h:selectOneMenu>

                          </a4j:outputPanel>

         

                          <h:panelGrid columns="2">

                              <a4j:commandButton value="#{msg.return}"

                                  onclick="#{rich:component('popupPanel')}.hide();" />

                          </h:panelGrid>

                      </h:form>

         

                  </rich:popupPanel>

         

              </ui:define>

          </ui:composition>

       

      **[Bean]**

       

          package test.jsf;

          

          import java.util.ArrayList;

          import java.util.List;

          

          import javax.faces.bean.ManagedBean;

          import javax.faces.bean.RequestScoped;

          import javax.faces.component.UIComponent;

          import javax.faces.component.UIInput;

          import javax.faces.component.html.HtmlSelectOneMenu;

          import javax.faces.event.ValueChangeEvent;

          import javax.faces.model.SelectItem;

          

          @ManagedBean(name = "selectsBean")

          @RequestScoped

          public class SelectsBean{

              private static final String[] FRUITS = { "", "Banana", "Cranberry", "Blueberry", "Orange" };

              private static final String[] VEGETABLES = { "", "Potatoes", "Broccoli", "Garlic", "Carrot" };

              private String currentItem = "";

              private String currentType = "";

              private List<SelectItem> firstList = new ArrayList<SelectItem>();

              private List<SelectItem> secondList = new ArrayList<SelectItem>();

          

              public void resetCurrentValue(){

                  currentItem = "";

                  currentType = "";

              }

              public SelectsBean() {

                  SelectItem item = new SelectItem("", "");

          

                  firstList.add(item);

                  item = new SelectItem("fruits", "Fruits");

                  firstList.add(item);

                  item = new SelectItem("vegetables", "Vegetables");

                  firstList.add(item);

          

                  for (int i = 0; i < FRUITS.length; i++) {

                      item = new SelectItem(FRUITS[i]);

                  }

              }

          

              public List<SelectItem> getFirstList() {

                  return firstList;

              }

          

              public List<SelectItem> getSecondList() {

                  return secondList;

              }

          

              public static String[] getFRUITS() {

                  return FRUITS;

              }

          

              public static String[] getVEGETABLES() {

                  return VEGETABLES;

              }

          

              public void valueChanged(ValueChangeEvent event) {

                  System.out.println(">>>valueChanged, event=" +event +", currentType=" +currentType);

                  secondList.clear();

                  if (null != event.getNewValue()) {

                      String[] currentItems;

          

                      if (((String) event.getNewValue()).equals("fruits")) {

                          currentItems = FRUITS;

                      } else {

                          currentItems = VEGETABLES;

                      }

          

                      for (int i = 0; i < currentItems.length; i++) {

                          SelectItem item = new SelectItem(currentItems[i]);

          

                          secondList.add(item);

                      }

                  }

                  System.out.println(">>>valueChanged, currentType=" +currentType);

              }

          

              public String getCurrentType() {

                  return currentType;

              }

          

              public void setCurrentType(String currentType) {

                  this.currentType = currentType;

              }

          

              public String getCurrentItem() {

                  return currentItem;

              }

          

              public void setCurrentItem(String currentItem) {

                  this.currentItem = currentItem;

              }

          }