7 Replies Latest reply: Jul 6, 2011 11:50 PM by 838458 RSS

    Hashtable data to JavaScript

    835297
      Hi

      I need someone's help for the below scenario.

      I have a JavaScript variable(string). Based on the value of this, I need to select an item(ArrayList object) from a java Hashtable which contains key(countryName) and corresponding states(ArrayList).


      My requirement is:

      when I select an item(countryName) in the Dropdown1, Dropdown2 should display the list of corresponding states(ArrayList). I am trying to do this, by calling a method on the event "onChange" of Dropdown. But I am struggling to get the respective state list based on the country value.


      Thanks.
        • 1. Re: Hashtable data to JavaScript
          DrClap
          Probably your best bet is to send the country name to the server via an AJAX request. On the server you would have a servlet which extracts the appropriate state or province names and returns them to the client. JSON might be a good tool to package that list of names.
          • 2. Re: Hashtable data to JavaScript
            835297
            However I have done that using AJAX.

            But my requirement was to do that with out using AJAX... any idea.
            • 3. Re: Hashtable data to JavaScript
              DrClap
              Then when you generate the page, since it can't send AJAX requests, it will have to contain all of the data it needs. That would include all of the things you mentioned there.
              • 4. Re: Hashtable data to JavaScript
                698065
                If you don't want to use AJAX then another way is we need to set javascript hash using java hash.

                Below is a example of it. Might you get some help.
                <%
                    Hashtable ht = new Hashtable();
                    List l1 = new ArrayList();
                    l1.add("city1");
                    l1.add("city2");
                    ht.put("a", l1);
                
                    List l2 = new ArrayList();
                    l2.add("city3");
                    l2.add("city4");
                    ht.put("b", l2);
                %>
                <html>
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                        <title>JSP Page</title>
                        <script type="text/javascript">
                            var myHash = {};
                            <%
                                Iterator it = ht.keySet().iterator();
                                while(it.hasNext())
                                {
                                    String key = String.valueOf(it.next());
                                    List l = (List)ht.get(key);
                                    %>
                                    myHash['<%= key%>'] = '<%= Arrays.toString(l.toArray(new String[l.size()]))%>';
                                    <%
                                }
                            %>
                
                            function getCities(objSel)
                            {
                                if(objSel.value.length > 0)
                                    alert(myHash[objSel.value])
                            }
                        </script>
                    </head>
                    <body onload="alert(myHash);">
                        <select onchange="getCities(this)">
                            <option value="">Select One</option>
                            <option value="a">a</option>
                            <option value="b">b</option>
                        </select>
                    </body>
                </html>
                • 5. Re: Hashtable data to JavaScript
                  868925
                  if you have organized the hashtable data into javascript object,
                  for example,
                  var countrylist = {UK:["London","Birmingham","Droitwich"],Japan:["Tokyo","Chiba","Yokohama"],China:["Beijing","Shanghai","Dalian"]};

                  then when you selected one country,
                  for example, "UK", you can get the Array(values) easily,
                  var places = countrylist["UK"]; // UK is your JavaScript variable(string).
                  or
                  var places = countrylist.UK;
                  • 6. Re: Hashtable data to JavaScript
                    873676
                    Can anyone explain how to do this implementation using AJAX. Thanks
                    • 7. Re: Hashtable data to JavaScript
                      838458
                      Ajax with which technology you are trying..... if you mention the technology i can explain you with the code...