1

我为我的应用程序使用 struts2 标签。我需要将值从 script 随机更改为 s:set 标记并填充到 javascript 数组中。如果我动态添加意味着我得到的输出为2012 - diff + 1但我的预期输出为2011。我怎么能做到这一点,我的代码是

var threeDatas = [];
var prevyear = [];
var diff;
var flagthree = false;

<% int j = 0;%>

<s:iterator value="testlist">
    prevyear.push(<s:property value="year"/>);
    if (<%= j - 1%> !== -1) {
        if (prevyear[<%= j - 1%>] !== prevyear[<%= j%>] - 1) {
            diff = prevyear[<%= j%>] - prevyear[<%= j - 1%>];
        }
        if (diff > 1) {                                                   
            for (inc = 1; inc < diff; inc++) {
                <s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push(['<s:property value="#incyear"/>', null, null]);
            }
            flagthree = true;
            diff = 0;
        }

<% j++;%>

</s:iterator>

我需要我的最终数组值应该是链接

['x', 'Cats','Dogs'],
     ['2008',   10, null],
     ['2009',   20, null],
     ['2010',   null, null],
     ['2011',   null, 23],
     ['2012',   null, 34]
4

2 回答 2

2

您可以在 JavaScript 中使用 eval() 方法。所以,如果你能生成类似的东西

var incyear = eval('<s:property value="year"/> - diff + 1');

于 2013-10-22T09:18:39.430 回答
0

我找到了另一种方法,通过使用 set

<s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push([<s:property value="#incyear"/>+"", null, null]);

和下面的代码:

var threeDatas = [];
var prevyear = [];
var diff;
var flagthree = false;
<% int j = 0;%>


<s:iterator value="testlist">
    prevyear.push(<s:property value="year"/>);
    if (<%= j - 1%> !== -1) {
        if (prevyear[<%= j - 1%>] !== prevyear[<%= j%>] - 1) {
            diff = prevyear[<%= j%>] - prevyear[<%= j - 1%>];
        }
        if (diff > 1) {                                                   
            for (inc = 1; inc < diff; inc++) {
                <s:set var="incyear"> <s:property value="year"/> - diff+1</s:set>
                 threeDatas.push([<s:property value="#incyear"/>+"", null, null]);
            }
            flagthree = true;
            diff = 0;
        }

<% j++;%>

</s:iterator>

通过使用它,您可以获得输出

['x', 'Cats','Dogs'],
 ['2008',   10, null],
 ['2009',   20, null],
 ['2010',   null, null],
 ['2011',   null, 23],
 ['2012',   null, 34]
于 2013-10-22T10:14:34.807 回答