3 Replies Latest reply on Dec 18, 2007 4:26 PM by Pete Muir

    Chained select boxes

    Flavio Ferreira Newbie

      hi!

      Is there a JSF (using AJAX if possible) component to help me create a chained select box from Entities of my application? seam-ui example is not working.

      example: Country -> State -> City

      a code snippet would help!

      thanks in advance!

        • 1. Re: Chained select boxes
          Damian Harvey Master

          It's friday, my pub lunch is settling nicely and I'm feeling verbose, so here's a very basic example (probably some syntax errors but you get the drift):

          @Name("citySelectionBean")
          @Scope(ScopeType.CONVERSATION) // <---- This is important. You could also use SESSION or a stateful bean
          public class CitySelectionBean {
          String country;
          String state;
          String city;
          
          public List<String> getCountries() {
           return entityManager.createQuery("select c from Country c").getResultList();
          }
          
          public List<String> getStates() {
           List<String> states = new ArrayList<String>();
           if(this.country != null) {
           return entityManager.createQuery("select s from State s where s.country = :country")
           .setParameter("country", this.country)
           .getResultList();
           }
           return states;
          }
          
          public List<String> getCities() {
           List<String> cities = new ArrayList<String>();
           if(this.state != null) {
           return entityManager.createQuery("select c from City c where c.state = :state")
           .setParameter("state", this.state)
           .getResultList();
           }
           return cities;
          }
          }
          

          And your page:
          <h:selectOneMenu id="country" value="#{citySelectionBean.country}">
           <s:selectItems value="#{citySelectionBean.countries}" var="country" label="#{country}">
           <a:support event="onchange" reRender="state, city"/>
          </h:selectOneMenu>
          
          <h:selectOneMenu id="state" value="#{citySelectionBean.state}">
           <s:selectItems value="#{citySelectionBean.states}" var="state" label="#{state}">
           <a:support event="onchange" reRender="city"/>
          </h:selectOneMenu>
          
          <h:selectOneMenu id="city" value="#{citySelectionBean.city}">
           <s:selectItems value="#{citySelectionBean.cities}" var="city" label="#{city}">
          </h:selectOneMenu>
          

          Also create a page.xml file and ensure that you have something like <begin-conversation> in it.

          Hope this helps.

          Cheers,

          Damian.


          • 2. Re: Chained select boxes
            Pete Muir Master

             

            "damianharvey" wrote:
            It's friday, my pub lunch is settling nicely and I'm feeling verbose, so here's a very basic example


            Tehehe.

            • 3. Re: Chained select boxes
              Pete Muir Master

               

              "syzork" wrote:
              seam-ui example is not working.


              How?