问题
I've got a problem and hope it's only my lack of experience in XForms. I need to create checkbox item for data that is defined as an enumeration 'Yes'/'No'. Basically it's just boolean value but with another pair of values. What I've already been able to do is something that basically works but need extra data node in model:
<xhtml:html xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:f="http://orbeon.org/oxf/xml/formatting"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xhtml:head>
<xforms:model xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xs="http://www.w3.org/2001/XMLSchema" id="main-model">
<xforms:instance id="instance">
<main>
<Boolean>true</Boolean>
<YesNo>Yes</YesNo>
</main>
</xforms:instance>
<xforms:bind ref="Boolean" type="xsd:boolean" />
<xforms:bind ref="YesNo" constraint=". = 'Yes' or . = 'No'" />
</xforms:model>
</xhtml:head>
<xhtml:body>
<xforms:input ref="instance('instance')/Boolean">
<xforms:label>Boolean: </xforms:label>
<xforms:action ev:event="xforms-value-changed">
<xforms:setvalue ref="instance('instance')/YesNo" value="if ( instance('instance')/Boolean = 'true' ) then 'Yes' else 'No'" />
</xforms:action>
</xforms:input>
<br/>
<xforms:output ref="instance('instance')/Boolean">
<xforms:label>Boolean:</xforms:label>
</xforms:output>
<br/>
<br/>
<xforms:select ref="instance('instance')/YesNo" appearance="full">
<xforms:label>Yes/No: </xforms:label>
<xforms:item>
<xforms:label></xforms:label>
<xforms:value>Yes</xforms:value>
</xforms:item>
<xforms:action ev:event="xforms-value-changed">
<xforms:setvalue ref="instance('instance')/YesNo" value="if ( instance('instance')/YesNo = 'Yes' ) then 'Yes' else 'No'" />
</xforms:action>
</xforms:select>
<br/>
<xforms:output ref="instance('instance')/YesNo">
<xforms:label>Yes/No:</xforms:label>
</xforms:output>
</xhtml:body>
</xhtml:html>
This example contains two possible solutions: First is standard boolean checkbox bound to boolean instance node with an action that set 'Yes'/'No' value for the second node. This solution works well but requires second data node which I cannot create due to schema (in the example above I could generally create second instance to store this value but in real project these checkboxes are in repeat block and I would have to create extra table of values for this which seems to be to much complicated), Second is select item with one and only value 'Yes' and action that tries to set 'No' value when empty value is set (unselected item). Unfortunatelly when this item is deselected it's unable to select it again (deselects automatically). Has any of you solution for such an issue?
Thanks in advance
回答1:
Hope this solves your problem..
<xhtml:html xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:f="http://orbeon.org/oxf/xml/formatting"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xhtml:head>
<xforms:model xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xs="http://www.w3.org/2001/XMLSchema" id="main-model">
<xforms:instance id="instance">
<main>
<Boolean value="true">Yes</Boolean>
</main>
</xforms:instance>
<xforms:bind ref="Boolean/@value" type="xforms:boolean" readonly="false()" />
<xforms:bind ref="Boolean" calculate="if(@value=true()) then 'Yes' else 'No'" readonly="false()" />
</xforms:model>
</xhtml:head>
<xhtml:body>
<xforms:input ref="instance('instance')/Boolean/@value">
<xforms:label>Boolean: </xforms:label>
</xforms:input>
<br/>
<xforms:output ref="instance('instance')/Boolean">
<xforms:label>Boolean:</xforms:label>
</xforms:output>
<br/>
<xforms:output ref="instance('instance')/Boolean/@value">
<xforms:label>Boolean/@value:</xforms:label>
</xforms:output>
<br/>
<br/>
</xhtml:body>
</xhtml:html>
In case if you are not allowed to use attributes to your xml node, then have the boolean values while user working on the form. On submit event, you can set the boolean values to Yes or No and push the data to external system.
来源:https://stackoverflow.com/questions/8883678/how-to-create-checkbox-item-resulting-with-yes-no-values-instead-of-true