Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate listbox options with result of an ajax call using query in spring mvc environment.

Currently I am working on my first jquery/ajax call and I'm having issues with how to populate the server side results into my listbox. The spring controller returns me the data correctly (hopefully) i just have issues with the jquery part with filling the listbox.

Here is my ajax call

    $(function() {
     $projectKey = $('#projectKey');

    $projectKey.change (
        function() {
            $.ajax({
                type: "GET",
                url: "getVersionsByProjectKey",
                data: {"projectKey": $projectKey.val() },
                dataType: 'json',
                success: function(data){
                     alert('success');
                     alert(data);
                     $('#jiraVersion').append(
                             $('<option></option>').html(data)
                         );
                }
            });
        }
    );
});

This is how my Controller look like:

@RequestMapping(value="/getVersionsByProjectKey", method = RequestMethod.GET)
  public @ResponseBody List<String> getVersionsByProjectKey(@RequestParam(value = "projectKey") String projectKey) {  

        List<String> versions = new ArrayList<String>();
        versions.add("Chuck");
        versions.add("Norris");
        versions.add("John");
        versions.add("Doe");

        return versions;  
    }

This is the listbox that I want to fill the data in:

<td>
<form:select path="jiraVersion" id="jiraVersion">

</form:select>
</td>

As I said before, I am just familiarizing myself with the jquery now and tried a couple of solutions from google but no joy. I tried:

success: function(data){
         alert('success');
         alert(data);
         $.each(data, function(index, item) {
         $("#jiraVersion").get(0).options[$("#jiraVersion").get(0).options.length] =    
         new Option(item.Display, item.Value);
       });}

etc etc.

The alert('success') writes me: Chuck,Norris,John,Doe.

If I send directly the request /getVersionsByProjectKey?projectKey=AIL

i am getting back ["Chuck","Norris","John","Doe"]

And I also tried to modify the success to have:

success: function(data){
     alert('success');
     alert(data);
     $('#jiraVersion').append(
        $('<option></option>').html(data)
     );

 }

Then my listbox contains just one option which is ChuckNorrisJohnDoe . Any idea what I am doing wrong?

like image 343
shippi Avatar asked Jan 08 '14 21:01

shippi


2 Answers

As the data from the Ajax call is the array ["Chuck","Norris","John","Doe"], you need to iterate through it using jQuery.each():

Use this function as success:

success: function(data){
    $.each(data, function(index, value) {
        $('#jiraVersion').append($('<option>').text(value).val(index));
    });
}

This will append/generate:

<form:select path="jiraVersion" id="jiraVersion">
    <option value="0">Chuck</option>
    <option value="1">Norris</option>
    <option value="2">John</option>
    <option value="3">Doe</option>
</form:select>
like image 170
acdcjunior Avatar answered Nov 06 '22 21:11

acdcjunior


Since your returned data is an array you have to loop through it to assign each value to each option.Thus, on your success call back try something like :

 success: function(data){
                     alert('success');
                     alert(data);
                     var dataLen = data.length;

                     for (i=0; i<dataLen; i++) {
                     $('#jiraVersion').append('<option value="' + data[i] + '">' + data[i] + '</option>');
                     }
                }
like image 4
Stefanos Vakirtzis Avatar answered Nov 06 '22 20:11

Stefanos Vakirtzis