Diego Ramos
Diego Ramos

Reputation: 1059

How to get multiple "listKey" values from a Struts 2 <s:select>?

I have a code like this:

<s:select id="s" list="list" listKey="id" listValue="displayValue"> 

When I get the value from the object I'll always get the id.

How can I change the "listKey" value dinamically so I could get for example, the name of the object (supposing other of my attributes besides id is name ) instead always de id?

I was trying some code in jQuery like this:

function changeListKey(){
     var id = $("#s").val();
     $('#s').attr('listKey','name');
     var name = $("#s").val();
     document.write(name);  // Only to test if I could get the value
}

When I execute it it doesn't seem to change the "lisKey" value so I could get another one, what would be the solution?

Thanks a lot in advance.

John Smith.

Upvotes: 1

Views: 2198

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Why would you want to mix up what's used as the key value? That would make a mess on the server side.

In any case, listKey isn't an HTML attribute, it's a custom tag attribute, and is meaningless after the HTML has been rendered and sent to the client. You would need to iterate over the HTML select element's options collection and change the values on the options.

All this said, you'd be much better off doing any bizarre manipulations like this in the action itself, and exposing only the desired key/value pairs to the JSP, either in a list, or more easily, in a map. Using a map eliminates the need to provide listKey/listValue attributes--just use the map's key as the option text, and the value as the option's value.

Upvotes: 1

Related Questions