如何在struts 2中将迭代器索引值传递给Javascript?

时间:2013-04-16 15:15:44

标签: javascript jquery html-table struts2 iterator

我在Struts 2中有一个动态表。我想在其中将迭代器索引值传递给Javascript函数。我怎样才能做到这一点?我已经给出了下面的代码。

这是我的表:

<table class="tblborder" id="priorIncident">
        <thead>
            <tr bgcolor="gray">
                <th colspan="5" align="center"><s:text name="priorIncidentTab" /></th>
            </tr>
            <tr bgcolor="black">
                <th><s:text name="date" /></th>
                <th><s:text name="reason" /></th>
                <th><s:text name="warningInd" /></th>
                <th><s:text name="warningDate" /></th>
                <th><s:text name="warningDesc" /></th>
            </tr>
        </thead>
        <tbody>
        <s:iterator value="claimData.priorIncidents" var="priorIncident" status="status">
            <tr>
                <s:hidden name="claimData.priorIncidents[%{#status.index}].oid" />
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].incidentDate" id="incidentDate1"/></td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].incidentReason" id="incidentReason1"/></td>
                <td>
                    <s:select headerKey="" headerValue="Select" list="#{'Y':'Yes', 'N':'No'}" theme="simple" name="claimData.priorIncidents[%{#status.index}].hasWarning" id="hasWarning1"/>
                </td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].warningDate" id="warningDate1"/></td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].warningDesc" id="warningDesc1"/></td>
            </tr>
        </s:iterator>
        </tbody>
    </table>
    <td><a href="#" id="add" onclick="MyFunction(%{#status.index})">Add</a></td>
    <td><a href="#" id="delete">Delete</a></td>

当我单击添加链接(不在迭代器中)时,我应该能够将索引值传递给某个Javascript函数吗? 由于我是JS新手,有人可以提供帮助吗?任何答案都非常感谢?

3 个答案:

答案 0 :(得分:1)

单引号丢失.......这必须有效

<a href="#" id="add" onclick="MyFunction('%{#status.index}')">Add</a></td>

如果不尝试下面的内容

<a href="#" id="add" onclick="MyFunction('<s:property value="%{[#stat.index]}"/>')"> Add</a></td>

答案 1 :(得分:0)

目前还不清楚你究竟要求的是什么。

您可以在渲染时使用列表的大小初始化JS变量。 MyFunction需要递增该值,以便您可以添加行;硬编码函数调用中的值将不起作用。

这意味着对方法调用中的值进行硬编码将不起作用:您需要使用JS变量。

如果您的目标是在一个结尾处开始计数的单个“添加”链接,您还可以<s:set>迭代器块中的一个变量来递增一个计数器,或者在最后一次迭代中设置一个变量。循环。

最终,由于您需要在添加更多项目时不断更新值,但只需使用列表大小设置JS变量似乎是最佳选择。如果您想要每个项目的“添加”链接,那么您可以使用里面的迭代块。

答案 2 :(得分:0)

状态的范围在迭代器内。使用标准c标记库将status.index的值设置为页面范围变量。

<c:set var="foo" scope="page" value="..."/>  

顺便说一下,status.index的最后一个值总是数组-1的大小。