1

I have a Map fetched from a database and I want to use the data to populate Select box. The Map is added to the model using sellerCodeList

In Spring it is simple:

<form:select path="orderDetails[0].SellerItemCode" items="${sellerCodeList}">
</form:select>

But I want to use the same in a Jquery function which builds up my select box. (Reason: I have a table with dynamic rows and each row has a select box as one of the element)

When I use the following I start getting error/exception

$("#poFields").append('<tr valign="top"><td>
<form:select path="orderDetails['+rowNum+'].SellerItemCode" items="${sellerCodeList}">
</form:select>');

This throws an exception

I see on SO that there is a way to iterate over the sellerCodeList and feed to options as per the following Dropdown link from SO

I am not good with JSON stuff though ;-)

Can someone please help

Community
  • 1
  • 1
Saurabh Singhal
  • 271
  • 1
  • 5
  • 18

1 Answers1

1

You'll need to "print" your items into JS so that your JS code has access to them. Something like this:

<script>
    var sellerCodes = {};
    <c:forEach items="${sellerCodeList}" var="entry">
        sellerCodes['${entry.key}'] = '${entry.value}';
    </c:forEach>
    // you can now use sellerCodes in your JS code as a map.
</script>
SergeyB
  • 9,478
  • 4
  • 33
  • 47
  • The sellerCodeList is a map containing a database Key, value pair. I need both of them. Also, could you please advise the way to use sellerCodeListJs..Thanks – Saurabh Singhal Apr 09 '14 at 19:21
  • How do I feed this to the Select box options ? and if I have to see if there is anything previously selected .. can I do something like selected=${someVar} – Saurabh Singhal Apr 10 '14 at 04:02
  • @ike_love.. Thanks for the answers .. I used it partially to get what I wanted... – Saurabh Singhal Apr 10 '14 at 08:43