保存时如何循环并从所有组件中重复获取值 [英] How to loop and get the values from all the components in a repeat when saving

查看:30
本文介绍了保存时如何循环并从所有组件中重复获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有重复控件的 XPage,从 viewScope 向量中获取其值.

I have a XPage with a repeat control getting its values from a viewScope vector.

<xp:repeat id="repeat1" rows="100" indexVar="i" value="#{javascript:viewScope.v1}"
</xp:repeat>

字段像这样绑定到向量

<xp:inputText value="#{viewScope.v3[i]}" id="qty" style="width:80px"</xp:inputText>

通过在输入框上使用 onChange 事件,我设法正确地获得了更新.像这样的东西.

I have managed to get the update correctly by using the onChange event on the input boxes. something like this.

<xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="qty">
                                            <xp:this.action><![CDATA[#{javascript:var v3:java.util.Vector = viewScope.v3
v3.setElementAt(getComponent("qty").getValue(),i)
viewScope.v3 = v3;

    }]]></xp:this.action>
                                        </xp:eventHandler>

问题是 onChange 事件正在更新输入字段,因此如果用户输入太快,输入的值会丢失,他们需要再次输入.

The problem is that the onChange event is updating the inputfield so if user is too fast typing the entered values are lost and they need to type it again.

所以我正在寻找一种方法来循环 viewScope 并在保存按钮中设置所有行值.

so I am looking for a way to loop the viewScope and set all the row values in the save button instead.

所以在我的保存按钮中,我想循环 viewScope 中的值并将它们全部放入一个新的 Vector 中,我可以将其写入文档字段.基本上,我需要为 SSJS 循环中的每一行获取用户使用 getComponent("").getValue() 输入的值.

So in my save button I want to loop the values in the viewScope and put them all in a new Vector that I can write to the document fields. basically I need to get hold of the values the user enterd using getComponent("").getValue() for each row in an SSJS loop.

我该怎么做.

推荐答案

如果您的保存按钮使用正确的执行模式/id 组合执行提交,则值已经正确保存.

The values are already correctly saved if your save button performs the submit with the correct execution mode/id combo.

您没有说明您最初选择使用 onchange 事件提交新值的原因,但是可以通过声明来克服附带问题,即用户的新值有时会被擦除refreshMode 属性设置为 norefresh 的事件.这将指示框架执行提交而不是销毁和重新创建生成事件的输入字段.这可能正是您想要的,除非您想为无效值提供视觉反馈,如果在调用之前没有使用 JavaScript 在客户端执行.我还可以看到您没有将 execMode 声明为 partial 从而向服务器发送超出评估所需的数量.记得使用它.

You don't state the reason why you initially chose to submit new values with the onchange event but the collateral problem, the one that users' new values get wiped sometimes, can be overcome by declaring the event with the refreshMode property set to norefresh. This will instruct the framework to do the submit and not destroy and recreate the input field the event is generated from. It might be just what you want, unless you want to give visual feedback for invalid values, if not performed client side with JavaScript prior the call. I can also see that you are not declaring the execMode to partial thus sending more than it's necessary to the server for evaluation. Remember to use it.

话虽如此,这将起作用并尊重您最初的想法(不要介意我添加的不同名称和 console.log 行只是为了测试目的):

Having tha being said, this will work and respects your initial idea (don't mind the different names and console.log line I added just for testing purposes):

<xp:repeat value="#{repeatBean.quantities}" indexVar="index">
    <xp:inputText id="inputQty" value="#{repeatBean.quantities[index]}">
        <xp:eventHandler event="onchange" submit="true"
            execMode="partial" refreshMode="norefresh" script="console.log(Date.now())" />
    </xp:inputText>
</xp:repeat>

即使您选择后一种方法,我也不理解您担心在调用 save 方法时获取新值.viewScope.v1 肯定已经包含所有提交的值,不需要遍历组件来获取这些新值;它们已经存在于您的 v1 Vector 中.考虑以下事项(再次不要介意不同的变量名称):

Even if you were to choose the latter approach I don't understand your concern about getting hold of the new values when your save method is invoked. viewScope.v1 will for sure contain all the submitted values already, there's no need to loop through the components to get such new values; they're already there in your v1 Vector. Consider the following (again don't mind the different variable names):

<xp:repeat value="#{repeatBean.quantities}" indexVar="index">
    <xp:inputText id="inputQty" value="#{repeatBean.quantities[index]}" />
    <br/>
</xp:repeat>

<xp:button id="button1" value="Label">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="partial" refreshId="showMe" action="#{repeatBean.save}" />
</xp:button>

<br/>

<xp:text id="showMe" value="#{repeatBean.quantities}" />

这篇关于保存时如何循环并从所有组件中重复获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆