如何在重复控件中使用json对象访问arraylist

时间:2017-10-18 07:40:58

标签: arrays json xpages xpages-ssjs xpages-extlib

我正在尝试一些新的东西。我想通过自定义控件中的属性定义传递带有json对象的arraylist。我称之为cols的属性,属于object类型。

在xpage上,我现在按照以下方式计算了该属性的值:

<xc:this.cols><![CDATA[#{javascript:var cols = [];

cols.push({ 
        "colName" : "Petter",
        "colValue"  : "Developer"
    });
cols.push({ 
        "colName" : "Jesper",
        "colValue"  : "Administrator"
    });
return cols;}]]></xc:this.cols>

现在在我的转发器中我想使用这些对象/值。但我不确定如何?

首先,我在重复控制之外尝试了如何在JavaScript中访问它们,例如:

<xp:text escape="true" id="computedField1">
            <xp:this.value><![CDATA[#{javascript:var cols = compositeData.cols;

cols[0]["colValue"]}]]></xp:this.value>
        </xp:text>
        <xp:text escape="true" id="computedField3">
            <xp:this.value><![CDATA[#{javascript:var cols = compositeData.cols;

cols[1]["colValue"]}]]></xp:this.value>
        </xp:text>

这似乎有效,因为我得到了Developer和Administrator返回的值。

现在我想在我的重复控件中访问json,但我在这里迷路了。

以下是我设置重复控件的方法:

<xp:repeat id="repeat1" rows="30" var="colObj" indexVar="idx"
            value="#{javascript:compositeData.cols}">

然后我在我的重复控件中放置了一个计算文本控件并尝试类似的东西:

<xp:text escape="true" id="computedField2">
                <xp:this.value><![CDATA[#{javascript:colObj[idx]["colValue"]}]]></xp:this.value>
             </xp:text>

但后来我收到了一个错误:

com.ibm.xsp.binding.javascript.JavaScriptValueBinding.getValue(JavaScriptValueBinding.java:132)

有人可以解释我做错了什么以及我该如何正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

尝试更改此内容:

<xp:text escape="true" id="computedField2">
    <xp:this.value><![CDATA[#{javascript:colObj[idx]["colValue"]}]]>    
    </xp:this.value>
</xp:text>

对此:

<xp:text escape="true" id="computedField2">
    <xp:this.value><![CDATA[#{javascript:colObj["colValue"]}]]>
    </xp:this.value>
</xp:text>

你已经拥有了colObj,所以不需要获得子集。重复控件处理idx,因此重复中的colObj与重复之外的colObj[n]相同。