orbeon

Forcing repeated grid iterations


We have a requirement to enter a value in a control (Repeating Grid Rows), then use that number to create the same number of Repeated Grid rows/iterations.

Furthermore, if that control number is adjusted up/down, then the Repeated Grid rows/iterations need to automatically adjust (even if that means deleting a row with data in it).

I can see how to force a maximum number of rows in the grid, but I don't understand how to automatically create/delete rows.

https://demo.orbeon.com/demo/fr/orbeon/builder/edit/ca9225d389f1bd3256275665e9ea2925ff09bef9?form-version=1


Solution

  • You can do this by adding code similar to the following in the source of your form. In this example, the control with the number is children-count, and the repeated grid is named children.

    For now, you'll need to use the action syntax in the form's source, but there is a Request for Enhancement #1658 to add a UI for this.

    <fr:listener version="2018.2" events="value-changed" controls="children-count"
                 actions="update-rows"/>
    <fr:action version="2018.2" name="update-rows">
        <fr:data-iterate ref="1 to xs:integer(//children-count - count(//children-iteration))">
           <fr:repeat-add-iteration repeat="children" at="end"/>
        </fr:data-iterate>
        <fr:data-iterate ref="1 to xs:integer(count(//children-iteration) - //children-count)">
           <fr:repeat-remove-iteration repeat="children" at="end"/>
        </fr:data-iterate>
    </fr:action>
    

    The animation below shows what happens if we only add row automatically, but don't remove them, and instead show a validation error when the specified number of children doesn't match the number of rows in the repeated grid.

    enter image description here