Search code examples
javascriptstruts2ognl

Struts2 dynamic columns in iterator


I have a select Box,

    <table>
    <tr><td>
    <s:select id = "mode" list="#{'0':'SELECT','A':'ABC','B':'XYZ'}" > </s:select>
   </td></tr>
    </table>

Based on selection from this select box, i need to make the columns dynamic :

<table>
<tr>
<td><div align="center">Serial Number</div></td>
<td><div align="center">Employee Number</div></td>
<td id="name"><div align="center">Employee Name</div></td>
</tr>

<s:iterator status="stat" value="empList" id="empList">
 <tr>
    <td><div align="center"><s:property value="%{#stat.count}"/></div></td>
    <td><div align="center"><s:property value="empNum" /></div></td>
    <td><div align="center"><s:select id="nameList%{#stat.index}" list="#{'A':'ABC','B':'XYZ'}" > </s:select></div></td>
</tr>
 </s:iterator>
</table>

The script I am using is like:

<script>
        var mode = document.getElementById("mode");
        var selMode = mode.options[mode.selectedIndex].value;
        var cnt = document.getElementById("rowCnt").value;
        if(selMode=="A"){

            for(i=0;i<=cnt;i++){
            var name = document.getElementById("name");
            name.style.display = "none";
            var lname = document.getElementById("nameList"+i);
            lname.style.display = "none";

            }
        }
        else{

            for(i=0;i<=cnt;i++){
            var name = document.getElementById("name");
            name.style.display = "block";
            var lname = document.getElementById("nameList"+i);
            lname.style.display = "block";
            }
        }
    </script>

But using id="nameList%{#stat.index}", it makes only select box dynamic, I want to make the whole column as dynamic.

<td id="nameList%{#stat.index}"><div align="center"><s:select  list="#{'A':'ABC','B':'XYZ'}" > </s:select></div></td>

DOES NOT work. It seems value of %{#stat.index} cannot be passed as column id.

Is there any other way to make the whole column dynamic?


Solution

  • You can't use OGNL without Struts tag. To print the value of OGNL expression you should use s:property tag.

    <td id="<s:property value='nameList%{#stat.index}'/>"><div align="center"><s:select  list="#{'A':'ABC','B':'XYZ'}" > </s:select></div></td>