Rick Herrick wrote:
> Reg Sherwood wrote:
>> I'm faced with an interesting problem - I am using displaytag 1.0 (non
>> el) combined with struts 1.2.9. I have a table with a number of columns
>> and rows; all columns are static display data except for the last which
>> is a dropdown box where the user can modify a value associated to the
>> given row (think of it as a status column). What I want to know is if
>> its possible on form submission to somehow figure for each row in the
>> displaytag the currently selected value of the dropdown. Anyone have
>> suggestions? All are welcomed.
>
> I do. I'll post it up later.
>
> This is YAF (yet another FAQ :).
OK, apologies for the delay. On top of a sick daughter, a wife leaving
town on a business trip, and a catastrophic hard drive failure... I've
been kinda busy lately!
So I'm gonna write up the Fred Flintstone method for handling drop-down
lists in forms with displaytag. I'll give you some pointers later on how
to do this more elegantly. Also, I'm writing this using Java 5/JSP 2.4
in-line EL. For older set-ups (JSP 2.3 and earlier) you'll have to use
JSTL tags. This is a silly example, with a person's name and the ability
to set a code for the person.
First, set up a form with the displaytag table inside it:
<form action="xxx">
<display:table name="items" uid="item">
<display:column property="name"/>
<display:column title="Code">
<select name="code_${item.id}">
<option value="A"<c:if test="${item.code == 'A'}">
selected</c:if>>A</option>
<option value="B"<c:if test="${item.code == 'B'}">
selected</c:if>>B</option>
<option value="C"<c:if test="${item.code == 'C'}">
selected</c:if>>C</option>
</select>
</display:table>
<input type="submit"
</form>
So what will happen with this is that the list will be iterated, the name
will be displayed, and for each item the code_XXXX drop-down list will be
created. The <c:if>s inside the option list will check each user to see
what code is set for the user and add "selected" to the appropriate option
in the list.
Processing this as it's reposted is simply a matter of find all request
parameters that begin with "code_". Grab those, lop off the "code_" and
that'll give you the item ID to which that selection applies. Get the
value of the list and set the code property on that item to whatever value
you got.
Like I said, this is the Fred Flintstone way of dealing with it. You're
out there munging strings together and parsing them out and it's fairly
ugly. There are many ways to streamline this:
* Take the options in your drop-down list from an enum or string array.
You can then just do a <c:iterate> on that enum or array and collapse the
code for the drop-down list. This is especially useful when you might
have a pretty large number of items in that list.
* Use the BeanUtils library! This is a big one, although it can be kinda
hard to figure out. Basically instead of doing "code_XXXX" and having to
parse the request parameter names, you do something like "code[XXXX]" and
objects will get magically referenced properly. Somewhere I have a nice
sandbox app that illustrates this, but I couldn't find it over the weekend
(catastrophic hard drive failure, remember? :^( ).
Drop a line to the list if you have any questions!
--
Rick Herrick
[EMAIL PROTECTED]
Proud member of the reality-based community
Never try to discourage thinking for you are sure to succeed.--Bertrand
Russell
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
displaytag-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/displaytag-user