If in your repeated grid you have a field named first-name
, the expression count($first-name)
used in a control outside of the repeated grid returns the number of rows in your repeated grid. Assuming your checkbox field is named checkbox
, your expression would look like:
if ($checkbox/string() = 'true')
then count($first-name)
else 0
You can find the source of a test form that shows this in action below, and at runtime you get something like:
<xh:html xmlns:sql="http://orbeon.org/oxf/xml/sql"
xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:exf="http://www.exforms.org/exf/1-0"
xmlns:saxon="http://saxon.sf.net/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:fb="http://orbeon.org/oxf/xml/form-builder"
xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xf="http://www.w3.org/2002/xforms">
<xh:head>
<xh:title>Counting the number of rows in a repeated grid</xh:title>
<xf:model id="fr-form-model" xxf:expose-xpath-types="true" xxf:analysis.calculate="true">
<!-- Main instance -->
<xf:instance id="fr-form-instance" xxf:exclude-result-prefixes="#all" xxf:index="id">
<form>
<section-1>
<grid-1>
<checkbox question-identifier="">true</checkbox>
<count question-identifier=""/>
</grid-1>
<grid-2>
<grid-2-iteration>
<first-name question-identifier=""/>
</grid-2-iteration>
</grid-2>
</section-1>
</form>
</xf:instance>
<!-- Bindings -->
<xf:bind id="fr-form-binds" ref="instance('fr-form-instance')">
<xf:bind id="section-1-bind" name="section-1" ref="section-1">
<xf:bind id="grid-1-bind" ref="grid-1" name="grid-1">
<xf:bind id="count-bind" name="count" ref="count" xxf:whitespace="trim"
calculate="if ($checkbox/string() = 'true')
then count($first-name)
else 0"
readonly="false()"/>
<xf:bind id="checkbox-bind" ref="checkbox" name="checkbox" type="xf:boolean"/>
</xf:bind>
<xf:bind id="grid-2-bind" ref="grid-2" name="grid-2"
relevant="$checkbox/string() = 'true'">
<xf:bind id="grid-2-iteration-bind" ref="grid-2-iteration" name="grid-2-iteration">
<xf:bind id="first-name-bind" ref="first-name" name="first-name" xxf:whitespace="trim"/>
</xf:bind>
</xf:bind>
</xf:bind>
</xf:bind>
<!-- Metadata -->
<xf:instance id="fr-form-metadata" xxf:readonly="true" xxf:exclude-result-prefixes="#all">
<metadata>
<application-name>a</application-name>
<form-name>a</form-name>
<title xml:lang="en">Counting the number of rows in a repeated grid</title>
<description xml:lang="en"/>
<created-with-version>2019.1-SNAPSHOT PE</created-with-version>
<library-versions>
<orbeon>1</orbeon>
<app>2</app>
</library-versions>
</metadata>
</xf:instance>
<!-- Attachments -->
<xf:instance id="fr-form-attachments" xxf:exclude-result-prefixes="#all">
<attachments/>
</xf:instance>
<!-- All form resources -->
<xf:instance xxf:readonly="true" id="fr-form-resources" xxf:exclude-result-prefixes="#all">
<resources>
<resource xml:lang="en">
<checkbox>
<label>Show repeated grid</label>
<hint/>
</checkbox>
<count>
<label>Count</label>
<hint/>
</count>
<first-name>
<label>First name</label>
<hint/>
</first-name>
<section-1>
<label>Untitled Section</label>
</section-1>
</resource>
</resources>
</xf:instance>
<xf:instance xxf:readonly="true" id="grid-2-template" xxf:exclude-result-prefixes="#all">
<grid-2-iteration>
<first-name/>
</grid-2-iteration>
</xf:instance>
</xf:model>
</xh:head>
<xh:body>
<fr:view>
<fr:body xmlns:xbl="http://www.w3.org/ns/xbl" xmlns:p="http://www.orbeon.com/oxf/pipeline"
xmlns:oxf="http://www.orbeon.com/oxf/processors">
<fr:section id="section-1-section" bind="section-1-bind">
<xf:label ref="$form-resources/section-1/label"/>
<fr:grid id="grid-1-grid" bind="grid-1-bind">
<fr:c y="1" x="1" w="6">
<fr:checkbox-input id="checkbox-control" bind="checkbox-bind">
<xf:label ref="$form-resources/checkbox/label"/>
<xf:hint ref="$form-resources/checkbox/hint"/>
<xf:alert ref="$fr-resources/detail/labels/alert"/>
</fr:checkbox-input>
</fr:c>
<fr:c y="1" x="7" w="6">
<xf:input id="count-control" bind="count-bind">
<xf:label ref="$form-resources/count/label"/>
<xf:hint ref="$form-resources/count/hint"/>
<xf:alert ref="$fr-resources/detail/labels/alert"/>
</xf:input>
</fr:c>
</fr:grid>
<fr:grid id="grid-2-grid" bind="grid-2-bind" repeat="content" min="1"
template="instance('grid-2-template')"
apply-defaults="true"
fb:initial-iterations="first">
<fr:c x="1" y="1" w="6">
<xf:input id="first-name-control" bind="first-name-bind">
<xf:label ref="$form-resources/first-name/label"/>
<xf:hint ref="$form-resources/first-name/hint"/>
<xf:alert ref="$fr-resources/detail/labels/alert"/>
</xf:input>
</fr:c>
<fr:c x="7" y="1" w="6"/>
</fr:grid>
</fr:section>
</fr:body>
</fr:view>
</xh:body>
</xh:html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…