Announcement Announcement Module
Collapse
No announcement yet.
dynamic <select>/<option> list population Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dynamic <select>/<option> list population

    I have the following scenario and I'm trying to use spring MVC:

    I have form to enter a user address with number, street, city, etc. Also, on the form I have a combobox/<select> holding a list of states as follows:

    Code:
    <spring&#58;bind path="address.state">
    	          <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>" onchange="submit&#40;&#41;">
    				  <c&#58;forEach var="state" items="$&#123;states&#125;">
    					  <option value="<spring&#58;transform value="$&#123;state.state&#125;"/>"
    		<c&#58;if test="$&#123;address.state.state==state.state&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;state.stateAbbr&#125;"/>
    					  </option>
    				  </c&#58;forEach>
    			  </select>
              <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    	      </spring&#58;bind>
    and a combobox/<select> holding a list of counties for the selected state as follows:
    Code:
    	      <spring&#58;bind path="address.county">
    	          <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>">
    				  <c&#58;forEach var="county" items="$&#123;counties&#125;">
    					  <option value="<spring&#58;transform value="$&#123;county.county&#125;"/>"
    		<c&#58;if test="$&#123;address.county.county==county.county&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;county.countyName&#125;"/>
    					  </option>
    				  </c&#58;forEach>
    			  </select>
              <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    	      </spring&#58;bind>
    What I would like to happen is that when a user changes the selection of the state that the county combobox/<select> list would be repopulated with the counties of the newly selected state.

    Does anyone have a solution to a situation like this? Can this be done with Spring MVC alone? Is javascript necessary?

  • #2
    The jsp for the state <select> should look as follows, sorry:

    Code:
    <spring&#58;bind path="address.state">
        <select size="1" name="<c&#58;out value="$&#123;status.expression&#125;"/>" >
    	  <c&#58;forEach var="state" items="$&#123;states&#125;">
    	      <option value="<spring&#58;transform value="$&#123;state.state&#125;"/>"
    		<c&#58;if test="$&#123;address.state.state==state.state&#125;">SELECTED</c&#58;if>>
    					  		<c&#58;out value="$&#123;state.stateAbbr&#125;"/>
                  </option>
    	</c&#58;forEach>
        </select>
           <font color="red"><c&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    </spring&#58;bind>

    Comment


    • #3
      JavaScript vs. roundtrip

      Can this be done with Spring MVC alone? Is javascript necessary?
      The answer is almost and probably. In order to take action on the client (browser) after a form element has been changed you'll need some sort of client-side scripting. The server can only respond to page requests and form submissions (a type of request). The onchange event handler attached to the state select can either load new values into the counties combo from pre-populated JavaScript objects or quietly submit the form and get the values from the server. The first method, in my opinion, relies a little too heaviliy on JavaScript where you have less control of behavior. While a little less seamless, a roundtrip to the server that returns just XHTML is a much safer bet.

      - Justin

      P.S. The HTML font element has gone the way of the dodo. Using CSS to decorate error messages will save you time and headaches.

      Comment


      • #4
        Thanks for the response. I like the 'quietly submitting' idea as I'm trying to stay away from javascript as much as possible. Currently, that is way that I am set up, and the combo box population seems to work just fine. I now need a way to distinguish between a submit from the combo box and the submit from the submit button. Also, when submitting from the combo box onchanged event, I run through a round of validation of the form's fields and get validation errors showing up when the user has only changed the state combo box. I've still got some work to do before this will be 'right.' I'm pretty new to all this. Appreciate the help.

        DJ

        ps. thanks for tip about the font element.

        Comment


        • #5
          To distinguish between form submission and altering model data, override:
          Code:
          isFormSubmission&#40;HttpServletRequest&#41;

          Comment


          • #6
            Also, I have a solution to this using just Spring MVC and Very, Very little JavaScript.

            See http://forum.springframework.org/showthread.php?t=12497 .

            I hope that helps someone else, I'd also like to here about any improvements.

            Take care,
            James Winans
            Last edited by robyn; May 14th, 2006, 01:31 PM.

            Comment

            Working...
            X