Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 2 min read

Support

Motivation

You have a list of items and you want a simple way of conditionally displaying fields within a repeated group.

Method

We will do this is two ways. First we will use a bind expression in the model. The second method of doing this will be to use a group element with a ref attribute.

Sample Data

xmlns="">

yes
display 1

no
display 2

yes
display 3

Sample Repeat

nodeset="instance('my-data')/group" id="my-repeat">

Group # value="count(preceding-sibling::*) +1">

ref="code">
Display Input Field:

Yes
yes

No
no

ref="field" >
Conditional Display:

Solution Using Relative Binds

In this example we use a relative statement to indicate that the field should only be displayed if the code value is "yes". Note that the context of the relevant expression is the result of each nodeset item.

id="field"
nodeset="instance('my-data')/group/field"
relevant="../code = 'yes'"/>

Solution Using Group Element

We can use the ref attribute to conditionally display any field within a repeat. We do this by starting the group ref attribute with "." to give it the current context. Then we add a predicate to turn on or off that entire group. In this case if the code is "yes" the input field will be displayed. If it is not the field will be hidden.

ref=".[code='yes']">
ref="field">
Conditional Display:

Complete Example

xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms">

Binds in Repeats
type="text/css">
@namespace xf url("http://www.w3.org/2002/xforms");
body {{
font-family:Helvetica, sans-serif;
}}

id="my-data">
xmlns="">

yes
display 1

no
display 2

yes
display 3

no
display 4

id="field"
nodeset="instance('my-data')/group/field"
relevant="../code = 'yes'"/>

Test of binding rules within a repeat
nodeset="instance('my-data')/group" id="my-repeat">

Group # value="count(preceding-sibling::*) +1">

ref="code">
Display Input Field:

Yes
yes

No
no

ref="field" >
Conditional Display:

value="index('my-repeat')">
Current Selected Group:

Where this page came from

This page was imported from Wikibooks. From “XForms” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

XForms

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in XForms

CC BY-SA 4.0
When you are done setting the months the control is set back to the hidden mode. ![XForms Control Visible]( XForms Control Visible (Image: The original uploader was Dmccreary at English Wikibooks ., CC BY-SA 3.0) ## Sample Code xmlns=" xmlns:xf=" xmlns:ev=" Show/Hide Control type="text/css"> @namesp
CC BY-SA 4.0
John Fred Sue Here is an example of this rule: id="triggerDisplay" nodeset="display-delete" relevant="count(/data/person) > 1"/> The above uses the XPath count() function with the greater than operator. But since it must be escaped you must use the ">" instead of ">". Or alternatively the delete but
CC BY-SA 4.0
For example, suppose you had a set of fields related to shipping supplies to remote staff in your IT department. The billing address could not be changed, only who an item was shipped to. The following single line in the model would make all of the sub-elements of BillToAddress read-only: nodeset="/
CC BY-SA 4.0
XForms, Select and Group
CC BY-SA 4.0
*/ /* This line ensures all the separate input controls appear on their own lines */ xf|input, xf|select, xf|select1, xf|textarea {display:block; margin:5px 0;} /* Makes the labels right aligned in a 200px wide column that floats to the left of the input controls. */ xf|input > xf|label, xf|select >
CC BY-SA 4.0
Use the **xforms-value-changed** event to trigger a submission that puts possible values in a list. When the user select a value from the suggestion list it will replace the text value in the field. We will also add a bind rule so that the selection view will only be visible if there is at least one