使用一个保存操作保存多个重复

时间:2014-03-31 13:59:44

标签: xpages xpages-ssjs

我对XPage有一个重复控制。我根据用户选择的内容构建我的重复。重复控制可以有1到10个项目。每次重复都会创建新文档。

有没有办法让一个保存按钮保存每个重复,而不是每个重复都有一个单独的保存按钮?

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:repeat
        id="repeat1"
        rows="30"
        var="rowData"
        indexVar="rowIndex">
        <xp:this.value><![CDATA[#{javascript:["1","2","3"]}]]></xp:this.value>
        <xp:inputText
            id="Number"
            defaultValue="#{javascript:rowData}" />
        <xp:button
            value="Save"
            id="button1">
            <xp:eventHandler
                event="onclick"
                submit="true"
                refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:var doc = database.createDocument();
doc.appendItemValue("Form","PersonDoc");
doc.save();

var doc2 = database.createDocument();
doc2.appendItemValue("Form", "PlaceDoc"); // I need the ability to create multiple documents
doc2.save();}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:br />
    </xp:repeat>
    <xp:br />
    <xp:button
        value="Master Save"
        id="button2" />
    <xp:label
        id="label1">
        <xp:this.value><![CDATA[<-- Able to call save once here instead of having to save 3 individual times in repeat?]]></xp:this.value>
    </xp:label>
</xp:view>

3 个答案:

答案 0 :(得分:2)

我强烈建议您阅读有关Java bean数据绑定的内容。

http://www.mindoo.de/web/blog.nsf/dx/16.07.2009095816KLEBCY.htm

http://www.mindoo.com/web/blog.nsf/dx/22.07.2009175255KLELM7.htm

它将帮助您从页面上的各个位置收集数据(在我们的示例中重复),您只需实现save()方法将数据保存在单个按钮上,但保存到许多文档中。

答案 1 :(得分:2)

我相信按钮有一个简单的动作叫做#34;保存数据源&#34;或类似的。这应该立即保存XPage上存在的所有数据源。

作为替代方案,将重复控件包裹在面板内。然后使用这样的SSJS:

var c = getComponent("mypanel");
var ds = c.getData();

获取该面板的数据源列表。 我知道数据源对象有一个&#34;刷新&#34;方法,我认为它有一个&#34; save&#34;方法也是。因此,您可以通过for ... next循环浏览数据源列表并执行每个数据源的save方法。

有关刷新方法的示例,请参阅http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_datasources#Reset+%2F+clear+a+datasource

答案 2 :(得分:1)

将来自此stackoverflow应答(https://stackoverflow.com/a/10031076)的一些CSJS与远程服务SSJS呼叫结合起来,并提出了以下内容,我认为这样做是您想要的。但正如Frantisek所说,使用Java bean可能是一个更好的解决方案。

下面的XPage上有一个简单的重复,显示3个不同值的输入。点击&#34; Master Save&#34;按钮将使用CSJS拉出重复中所有输入文本框的值,然后调用jsonRpcService以运行创建文档的SSJS。作为一个例子,我添加了一些行来将输入值输入到保存的文档中。

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xe="http://www.ibm.com/xsp/coreex">
<xp:repeat id="repeat1" rows="30" var="rowData" indexVar="rowIndex">
    <xp:this.value><![CDATA[#{javascript:["1","2","3"]}]]></xp:this.value>
    <xp:inputText id="Number" defaultValue="#{javascript:rowData}" />
</xp:repeat>
<xp:br />

<xp:button id="masterSave3" value="Master Save">
    <xp:eventHandler event="onclick" submit="false">
        <xp:this.script><![CDATA[
            var domEl = dojo.byId('#{id:repeat1}');

            //Get array of input textboxes
            var textBoxes = domEl.getElementsByTagName("input");

            //Pull out values of the textBoxes
            var valueList = [];
            for(var i = 0; i < textBoxes.length; i++) {
                valueList[i] = textBoxes[i].value;
            }

            //Call the remote service that runs SSJS, passing in array of values
            var deferred = masterSaveSvc.saveMethod(valueList);
            deferred.addCallback(function(result) {
                //Optional, display an alert upon completion, or perform some other code
                alert(result);
            });
        ]]>
        </xp:this.script>
    </xp:eventHandler>
</xp:button>

<xe:jsonRpcService id="jsonRpcService1" serviceName="masterSaveSvc">
    <xe:this.methods>
        <xe:remoteMethod name="saveMethod">
            <xe:this.arguments>
                <xe:remoteMethodArg name="valueList"></xe:remoteMethodArg>
            </xe:this.arguments>
            <xe:this.script><![CDATA[
                //For each entry in the array
                for(var i = 0; i < valueList.length; i++) {
                    //Create new document
                    var doc = database.createDocument();
                    doc.appendItemValue("Form", "PersonDoc");
                    //Add current value to the document
                    doc.appendItemValue("Name", valueList[i]);
                    doc.save();

                    //Create another new document
                    var doc2 = database.createDocument();
                    doc2.appendItemValue("Form", "PlaceDoc");
                    //Add current value to the document
                    doc2.appendItemValue("Place", valueList[i]);
                    doc2.save();

                    //Print values to the server console
                    println("value " + (i+1) + ": " + valueList[i]);
                }                       
                return "finished";
            ]]>
            </xe:this.script>
        </xe:remoteMethod>
    </xe:this.methods>
</xe:jsonRpcService>
</xp:view>