Preethi Jha
Preethi Jha

Reputation: 167

Fill struts2 select tag with javascript array

Is it possible to fill the Struts 2 select tag with javascript array on load?

I have a code

<s:select id="attrStpSel" name="attrStpSel" headerKey="-1"
          headerValue="Select" list="${attributeActions}"
          labelposition="left" onchange="displaySelectAction()"
          cssStyle="width: 300px;" />

This is the script in the page.

<script type="text/javascript">
    $(document).ready(function() {
        var attributeActions = [ 'Add', 'Update', 'Search' ];
    });
</script>

Since it is a hard coded values, I don't want them to be populated from action. with the above code the javascript variable attributeActions is not being picked up.

What should be the ideal way to populate the static list into the Struts 2 select tag?

Upvotes: 1

Views: 1738

Answers (2)

Roman C
Roman C

Reputation: 1

Struts select tag renders ordinary html select. It also adds additional tags, attributes, CSS to the generated html because it's UI tag. You can read more about select tag.

Render an HTML input tag of type select.

If you want to populate this select tag with javascript you can do something like this

$(function() {
    var attributeActions = [ 'Add', 'Update', 'Search' ];
    var $select = $('#attrStpSel');
    $select.find('option').remove();
    $('<option>').val("-1").text("Select").appendTo($select);
    $.each(attributeActions, function(index, value) {
      $('<option>').val(value).text(value).appendTo($select);
    });
});

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50193

You can create an inline list with #{}:

<s:select name = "attrStpSel" 
     headerKey = "-1"
   headerValue = "Select" 
          list = "#{'Add','Update','Search'}" />

You can also create an inline map with the same notation:

<s:select name = "attrStpSel" 
     headerKey = "-1"
   headerValue = "Select" 
          list = "#{'0':'Add' , '1':'Update' , '2':'Search'}" />

Upvotes: 1

Related Questions