每次更改时如何使用 xforms:select 的选定值执行某些操作? [英] How to do something with selected values of an xforms:select every time they change?

查看:38
本文介绍了每次更改时如何使用 xforms:select 的选定值执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Orbeon Forms 上运行的 XForms 有这个问题.我正在使用 fr:box-select 控件,如下所示:

I have this problem with XForms I am running on Orbeon Forms. I am using a fr:box-select control as follows:

<fr:box-select bind="box-select-bind" id="box-select-control">
        <xforms:action ev:event="xforms-value-changed">
         <xxforms:variable name="selected-value" select="."/>
         <xforms:message level="modal">Hello:<xforms:output select="$selected-value" />
         </xforms:message>

        </xforms:action>

        <xforms:itemset nodeset="instance('codes')/box-select/item">
                                    <xforms:label ref="label"/>
                                    <xforms:value ref="value"/>
         </xforms:itemset>
   </fr:box-select>

绑定到一个简单的 XML 文件:

The binding is to a simple XML file:

<box-results></box-results>

代码 XML 如下所示:

The codes XML looks like:

<box-select>
    <item>
        <label>Cat</label>
        <value>cat</value>
    </item>
    <item>
        <label>Dog</label>
        <value>dog</value>
    </item>
    <item>
        <label>Bird</label>
        <value>bird</value>
    </item>
    <item>
        <label>Fish</label>
        <value>fish</value>
    </item>
</box-select>

当我检查框中的条目时,我的节点 <box-results> 将更新为以空格分隔的选定值,这似乎是预期的.但是,我似乎找不到有关如何处理所选值的任何文档.我想访问刚刚选择、取消选择的值并在 xpath 中使用该项目的值.所以,如果选择了一个值,那么我会这样做:

When I check entries in the box, my node <box-results> gets updated with the selected values separated by a space, which seems to be what is expected. However, I can't seem to find any documentation on how to process the selected values. I want to get access to what value was just selected, de-selected and use the value of this item in an xpath. So, if a value was selected then I would do this:

<setvalue
 ref="somexpath[id=$selected-value]/display
 value="'true'"/>

如果一个值被取消选择,我会这样做:

And if a value was deselected I would do this:

<setvalue
 ref="somexpath[id=$selected-value]/display
 value="'false'"/>

基本上,我只想知道要使用的事件,以及如何在触发时访问该值.然后我想在 xpath 中使用这个值.我将使用它来隐藏/显示表单的某些部分.使用 xforms-value-changed 事件将 Xpath "."不会返回我所期望的,就像在select1"控件中一样.

Basically, I just want to know the event to use, and how to get access to the value when it fires. Then I want to use this value in an xpath. I am going to use this to hide/display portions of the form. Using the xforms-value-changed event the Xpath "." doesn't return what I would expect, as it does in "select1" controls.

我可以像这样遍历所有选择的值:

I can loop through all the values that are selected like so:

<xforms:action ev:event="xforms-select" xxforms:iterate="for $s in tokenize(instance('data-inst')/box-results,'\s')return xxforms:element('text',$s)">
    <xforms:message level="modal">Hello selected:<xforms:output select="$s" />
</xforms:action>

然而,这并不是我所需要的.我也许能够完成这项工作,但需要做更多的工作,因为我需要知道取消选择哪些选项以更改用户的显示.

However, this isn't exactly what I need. I might be able to make this work, but it would require a lot more work because I need to do know what ones are deselected to change the display for the user.

推荐答案

由于在您的情况下您不需要具体知道哪个值更改了,您可以在值更改时重置 somexpath[id=$selected-value] 根据需要.您可以使用以下代码执行此操作,该代码仅使用 xxforms:iterate:

Since in your case you don't need to know specifically which value changed, you can on value change reset all the values in somexpath[id=$selected-value] as needed. You can do this with the following code which uses just <xforms:setvalue> with an xxforms:iterate:

<xforms:action ev:event="xforms-value-changed">
    <xxforms:variable name="selected-values" select="tokenize(., '\s+')"/>
    <xforms:setvalue xxforms:iterate="instance('codes')/item" 
                     ref="@selected">false</xforms:setvalue>
    <xforms:setvalue xxforms:iterate="$selected-values"
                     ref="for $v in . return instance('codes')/item
                     [value = $v]/@selected">true</xforms:setvalue>
</xforms:action>

另请参阅使用上述代码段的示例的完整来源.

这篇关于每次更改时如何使用 xforms:select 的选定值执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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