[SOLVED] cascading selectOneMenu with ajax issue

UI Components for JSF
Post Reply
jackstuard
Posts: 28
Joined: 03 Aug 2011, 15:53

10 Aug 2011, 14:54

Can someone confirm please, if selectOneMenu has a specific problem with ajax?
At all, I'm trying to do a cascading selectOneMenu, so, first you have to select the first, second and then the third.

here's my code:

Code: Select all

<h:outputLabel value="First List:" for="firstId" />
<p:selectOneMenu value="#{myBean.selectedFirstId}" tabindex="0"
	required="true" id="firstId">
	<f:selectItem itemLabel="Select one item" itemValue="" />
	<f:selectItems value="#{myBean.firstItems}" />
	<p:ajax update="secondId" listener="#{myBean.handleFirstIdChange}" />
</p:selectOneMenu>
<p:message for="firstId" />

<h:outputLabel value="Second List:" for="secondId" />
<p:selectOneMenu value="#{myBean.selectedSecondId}" tabindex="1"
	required="true" id="secondId">
	<f:selectItem itemLabel="Select one item" itemValue="" />
	<f:selectItems value="#{myBean.secondItems}" />
	<p:ajax update="thirdId"
		listener="#{myBean.handleSecondIdChange}" />
</p:selectOneMenu>
<p:message for="secondId" />

<h:outputLabel value="Third List:" for="thirdId" />
<p:selectOneMenu value="#{myBean.selectedThirdId}" tabindex="2"
	required="true" id="thirdId">
	<f:selectItem itemLabel="Select one item" itemValue="" />
	<f:selectItems value="#{myBean.thirdItems}" />
	<p:ajax update="labelId" listener="#{myBean.handleThirdIdChange}" />
</p:selectOneMenu>
<p:message for="thirdId" />

<h:outputLabel value="Result:" for="labelId" />
<h:outputLabel value="#{myBean.labelId}" id="labelId" />
<p:message for="labelId" />
when the first selectOneMenu is selected, myBean.handleFirstIdChange is executed and myBean.secondItems is populated, this way, my second selectOneMenu is populated.
Now, when I select an item in the second selectOneMenu, myBean.handleSecondIdChange is not called. The method exists.

I'm not sure why this is happening, maybe due the second selectOneMenu doesn't have any items initially p:ajax doesn't work anymore, even after everything is being populated. Is this a bug? I'm using primefaces 3.0M2.

MyBean:

Code: Select all

   private String selectedFirstId;
   private String selectedSecondId;
   private String selectedThirdId;
   private Map<String, String> secondItems;
   private Map<String, String> thirdItems;
   private String labelId;

   public Map<String, String> getFirstItems() {
      Map<String, String> resultMap = new HashMap<String, String>();
      resultMap.put("Item 1", "Item 1");
      resultMap.put("Item 2", "Item 2");
      resultMap.put("Item 3", "Item 3");
      return resultMap;
   }

   public void handleFirstIdChange() {
      System.out.println("handleFirstIdChange-----");
      secondItems = new HashMap<String, String>();
      secondItems.put("foo1", "foo1");
      secondItems.put("foo2", "foo2");
      secondItems.put("foo3", "foo3");
   }

   public void handleSecondIdChange() {
      System.out.println("handleSecondIdChange-----");
      thirdItems = new HashMap<String, String>();
      thirdItems.put("zoo1", "zoo1");
      thirdItems.put("zoo2", "zoo2");
      thirdItems.put("zoo3", "zoo3");
   }

   public void handleThirdIdChange() {
      labelId = "ok";
   }

   public String getSelectedFirstId() {
      return selectedFirstId;
   }

   public void setSelectedFirstId(String selectedFirstId) {
      this.selectedFirstId = selectedFirstId;
   }

   public String getSelectedThirdId() {
      return selectedThirdId;
   }

   public void setSelectedThirdId(String selectedThirdId) {
      this.selectedThirdId = selectedThirdId;
   }

   public String getSelectedSecondId() {
      return selectedSecondId;
   }

   public void setSelectedSecondId(String selectedSecondId) {
      this.selectedSecondId = selectedSecondId;
   }

   public String getLabelId() {
      return labelId;
   }

   public void setLabelId(String labelId) {
      this.labelId = labelId;
   }

   public Map<String, String> getThirdItems() {
      return thirdItems;
   }

   public void setThirdItems(Map<String, String> thirdItems) {
      this.thirdItems = thirdItems;
   }

   public Map<String, String> getSecondItems() {
      return secondItems;
   }

   public void setSecondItems(Map<String, String> secondItems) {
      this.secondItems = secondItems;
   }
Thanks in advance.
Last edited by jackstuard on 10 Aug 2011, 15:19, edited 1 time in total.

jackstuard
Posts: 28
Joined: 03 Aug 2011, 15:53

10 Aug 2011, 15:18

Problem solved!
Solution: Remember to use session scope :lol:

davealma
Posts: 14
Joined: 13 Nov 2012, 05:32

04 Mar 2013, 16:53

Such Scope for this task seems to me a little to much usage resources.
View Scope is not enough?? :?:
David
JSF 2.1/Primefaces 3.4.x
Glassfish

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 74 guests