7 Replies Latest reply on Jul 7, 2011 4:50 AM by 838458

    Hashtable data to JavaScript


      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.

        • 1. Re: Hashtable data to JavaScript
          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.
          1 person found this helpful
          • 2. Re: Hashtable data to JavaScript
            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
              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
                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();
                    ht.put("a", l1);
                    List l2 = new ArrayList();
                    ht.put("b", l2);
                        <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();
                                    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)
                    <body onload="alert(myHash);">
                        <select onchange="getCities(this)">
                            <option value="">Select One</option>
                            <option value="a">a</option>
                            <option value="b">b</option>
                • 5. Re: Hashtable data to JavaScript
                  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).
                  var places = countrylist.UK;
                  • 6. Re: Hashtable data to JavaScript
                    Can anyone explain how to do this implementation using AJAX. Thanks
                    • 7. Re: Hashtable data to JavaScript
                      Ajax with which technology you are trying..... if you mention the technology i can explain you with the code...