问题描述:

I'm pretty new to XForms, and I am having problems trying to find the solution to what seems like a relatively trivial problem. I have a select1 control on my form, and I want to bind an item in my model to both the selected value and the selected text of the control, so that I can store both values when the form is submitted.

My model looks something like this:

<model>

<valueofselect/>

<textofselect/>

</model>

Could one of you XForms gurus please post a small example of how this can be achieved.

Thank you very much in advance.

网友答案:

There are different ways to achieve this:

  • using xforms:setvalue to set the value of the /model/textofselect node;
  • using xforms:bind with a calculate property to set the value of the /model/textofselect node dynamically.

Example:

Here's a complete xforms form demonstrating the second solution. The first way would look similar, the most important part (the XPath) would be the same in both solutions.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xforms="http://www.w3.org/2002/xforms">
<head>
<title>Select1 - get value and label</title>
<xforms:model id="main-model">
    <xforms:instance id="data">
        <model xmlns="">
            <valueofselect/>
            <textofselect/>
        </model>
    </xforms:instance>
    <xforms:instance id="items">
        <items xmlns="">
            <item>
                <label />
                <value />
            </item>
            <item>
                <label>label a</label>
                <value>value a</value>
            </item>
            <item>
                <label>label b</label>
                <value>value b</value>
            </item>
            <item>
                <label>label c</label>
                <value>value c</value>
            </item>
        </items>
    </xforms:instance>
    <xforms:bind
         nodeset="instance('data')/textofselect"
         calculate="instance('items')/item[value eq
                    instance('data')/valueofselect]/label/text()" />
</xforms:model>
</head>
<body>
    <h1>Select...</h1>
    <xforms:select1 ref="instance('data')/valueofselect">
        <xforms:itemset nodeset="instance('items')/item">
            <xforms:label ref="label"/>
            <xforms:value ref="value"/>
        </xforms:itemset>
    </xforms:select1>
    <h1>Selection result</h1>
    <xforms:output ref="instance('data')/valueofselect">
        <xforms:label>Selected value: </xforms:label>
    </xforms:output><br />
    <xforms:output ref="instance('data')/textofselect">
        <xforms:label>Selected Value's label: </xforms:label>
    </xforms:output>
</body>
</html>

Explanation:

The xforms:bind makes the XForms engine calculate the value of the /model/textofselect node depending on the value of the /model/valueofselect node. To do so, it selects the text value of the selected item from the items list using a XPath predicate.

To make the select1 items accessible to the bind, they aren't defined as children of the xforms:select1 node. Instead, they're put into the xforms:model using the xforms:itemset element.

相关阅读:
Top