Search code examples
jquerystruts2jquery-selectbox

Jquery Select box values using Struts2 UI tags


In a jsp page i have a select Input which gets the values from a Struts2 action, and i am using Struts2 iterator tag to get the values.

<select id="package-type-input" name="packageTypeId">
    <gs:iterator var="package" value="" id="test-iterator">
       <option value='<gs:property value="#package.id"/>'  
       <gs:property value="#package.packagename"/></option>
    </gs:iterator>
</select>

I am getting values from the struts2 action as List packagesList. I am trying to add those values to the iterator using the below line. But its not working

$("#test-iterator").val(packageList). 

The ajax call is

_getAccountDetails: function () {
    var self = this;
    var organizationId = self.id;
    $.ajax({
        type: "POST",
        url: self.url.getOrgPackageDetails,
        dataType: 'json',
        traditional: true,
        data: "organizationId=" + organizationId,
        success: function (response) {
            self._setData(response.data,response.packageList);
        },
        error: function (response) {}
    });
},

the response.packageList is the jsonified form of java Array list. setData method sets the data from response.data into its respective fields.

_setData: function (data, packageList) {
    var self = this;
    self.dom.currentPackageType.val(data.CurrentPackage);
    self.dom.currentUsersLimit.val(data.Users);
    self.dom.currentEntityLimit.val(data.Entities);
    self.dom.CurrentRenewalDate.val(moment(data.RenewDate).calendar());
    self.dom.currentQEntityLimit.val(data.QEntity);
    self._bindGUIEvents()
},

Solution

  • You can do it easily with s2 select tag, iterator tag is not required here

    <gs:select
           id="package-type-input" 
           name="packageTypeId"
           list="packageList"
           listKey="id"
           listValue="packagename"
    />
    

    Take a look at the select tag description

    Also, follow the Tag Reference whenever you have a doubt.

    As for your edited post: In your _setData function

    $.each(packageList, function(i,package){
        var option = $("<option value="+package.id+">"+package.packagename+"</option>");
        $("#package-type-input").append(option);
    });