开发者

How to conditionally set a css class with struts2

开发者 https://www.devze.com 2023-02-02 16:01 出处:网络
I need to display a sequence of <tr> elements with a <struts:iterator> tag and set a class of each <tr> conditionally.

I need to display a sequence of <tr> elements with a <struts:iterator> tag and set a class of each <tr> conditionally.

Let's say I have a list items = {1, 2a, 2b, 3, 4, 6, 7, 9a, 9b}, and also two other lists duplicates = {2b, 9b} and jumps = {6, 9a}. How can I iterate over those items, d开发者_运维问答isplay them in an HTML element and set a class of that element to duplicate and/or jump depending if the corresponding list contains that item?


You can refer to this website, it's detailing the iterator tag of Struts2


You could add a method boolean isDuplicated(String itemid) to your action and code something as :

<s:iterator id="item"  value="items">
<tr <s:if test="isDuplicated(#item)"> class='duplicated' </s:if> >

You get the idea. You could even (though some purist might object that you put some presentation related code in you action) code a method that gives you the "class" of each item

<s:iterator id="item"  value="items">
<tr class='<s:property value="getClassForItemDuplicated(#item)" />'>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号