Announcement Announcement Module
Collapse
No announcement yet.
Help - how to dynamic populate a drop down list Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help - how to dynamic populate a drop down list

    Hi, Guys

    I am new to String mvc and I couldn't find out how to implement this scenior,

    The jsp page companies.jsp contain one drop down list and a output table. The drop down list contains all the categories and choicing one category will populate the output table with all the companies belong to this category, by calling getCompanies(int categoryId) in the controller.

    I am trying to implement the controller by extending the MultiActionController class and I have reach to a point that populating the output company table by using the categoryId in the request.getParameter("categoryId"), but I don't know how to get this categoryId from the selected value in the categories drop down list. Can anyone pleaes help me? This must be a very easy task to do in Spring, but I just haven't got my head around it yet. Or it will be very helpful if you can point me to the rigth direction.

    Many thanks,

    Kai

  • #2
    Hello

    The jsp page companies.jsp contain one drop down list and a output table. The drop down list contains all the categories and choicing one category will populate the output table with all the companies belong to this category, by calling getCompanies(int categoryId) in the controller.
    If each option in the drop list must generate a different output table, I suggest strongly use Ajax(in this case DWR) and of course Spring MVC

    Therefore when your event with javascript like onchange is trigger from your droplist the client side with javascript should get the element making reference with id to get its value and call a simple controller to the server side and return a simple list and from such list fill the table in your same page

    Neither DWR nor Spring MVC are not hard to learn, first try reading documentation available in google for DWR and spring documentation itself

    Let me know your advance
    Last edited by dr_pompeii; Jun 28th, 2009, 03:30 PM.

    Comment


    • #3
      thanks

      Hi, dr_pompeii

      Thanks for the reply. I have tried use js to implement as follows,
      Code:
      		<script type="text/javascript" >
      		function showCompanies(event){
      			var categoryId = document.getElementById("categoryId");
      			sendRequest();
      		}
      
      		function sendRequest(){
      			xmlHttp=getXmlHttpObject();
      			if (xmlHttp==null){
      				alert ("Your browser does not support AJAX");
      			}
      			var url="companies.htm?categoryId=" + document.getElementById("categoryId").value;
      			xmlHttp.onreadystatechange=stateChanged;
      			xmlHttp.open("GET", url, true);
      			xmlHttp.send(null);
      		}
      
      		function stateChanged(){
      			if (xmlHttp.readyState==4){
      				document.getElementById("outputDiv").innerHTML = xmlHttp.responseText;
      			}
      		}
      		function getXmlHttpObject(){
      			var xmlHttp=null;
      			try{
      				xmlHttp=new XMLHttpRequest();
      			}catch(e){
      				try{
      					xmlHttp=new ActiveXOject("Msxm12.XMLHTTP");
      					if(xmlHttp!=null){
      						
      					}
      				}catch(e){
      					xmlHttp = new ActiveXOject("Microsoft.XMLHTTP");
      				}
      			}
      			return xmlHttp;
      		}
      		</script>
      <H2>contacts:</H2>
      
      <TABLE border="1">
        <TH>Categories</TH>
        <select id="categoryId" onchange="showCompanies(event)">
        	<c:forEach var="category" items="${categoryList}">
        		<OPTION value="<c:out value="${category.id}"/>"><c:out value="${category.name}"/></OPTION>
        	</c:forEach>
        </select>
        <div id="outputDiv"></div>
      The companies.htm page has a MultiActionController on the back. Above code is working as I expected. However, I am not sure what did you mean by "call a simple controller to the server side and return a simple list and from such list fill the table in your same page". How do you call a simple controller from JS code? Do I have to do this by DWR. I just had a quick look of DWR, seems that what it is doing? Is there any way that I can do by Spring mvc??

      Cheers,

      K

      Comment


      • #4
        Hello ksh29

        Above code is working as I expected.
        Good

        I used to work with DWR, it keeps the things easier than the classic way (thats mean without some framework), like your code

        I am not sure what did you mean by "call a simple controller to the server side and return a simple list and from such list fill the table in your same page".
        change the term controller to some object located in the server side to handle your ajax request

        I suggest you get this book in your hands, I learnt really DWR and DWR/Spring from such book "Pro Ajax and Java Frameworks"

        Therefore read these chapters
        Chapter 4 - Java-Specific Frameworks
        that is dedicated to pure DWR
        and
        Chapter 7 - Spring and Ajax

        That chapters I read and gave me solid knowledge to learn these topics

        Therefore after to read these chapters many questions must be clear to you

        HTH

        Comment


        • #5
          Thanks

          Thanks for the recommendation.

          Comment

          Working...
          X