How to keep form data when adding or removing rows from a repeater

I have a repeater with a check box and text field in each row.

I also have add and delete buttons to manage the number of rows in the repeater.

If I check the box or enter a value in the text field, then add or delete a row, all the data (which has been typed into the text fields) in the repeater get’s erased.

How do I keep the data in the completed fields while also adding rows?

I feel like I’m missing something simple here as this is surely pretty basic functionality.

name-list-widget.rp (50.0 KB)

Hi!

When you perform any command on the repeater (add row, add filter, add sort, etc.), the repeater will redraw itself. When it does, every row in the repeater goes back to its default state: i.e., the way the row was originally constructed when you created it.

The only way to change the appearance of a row when the repeater redraws itself is to use OnItemLoad to set widget values in the row. For example, the default repeater has the command Set text of (rectangle) to [[Item.column0]]. If it weren’t for that command, every row of the repeater would appear as the row was created in Axure: basically a stack of blank rectangles.

So let’s just look at the checkbox example. If you create a checkbox that is unchecked in the repeater row, that repeater will display as unchecked in every row when the repeater initializes. It will also revert to unchecked for every row when you add a row (even if you checked it), because adding a row causes the repeater to redraw itself, setting all widgets to their default values.

So just as you set a text label’s value OnItemLoad to a value in the dataset, you’ll need to set the selected value of the checkbox based on a value stored in the dataset.

What you’ll need to do is, upon checking or unchecking the checkbox, store the selected state of the checkbox in the repeater’s dataset, and then, in OnItemLoad, set the checkbox’s selected state based on that value you stored.

So add a column called ‘isChecked’ to the dataset, and set the value for every row to ‘false’. In OnItemLoad, say:

OnItemLoad
  If [[Item.isChecked]] equals true
    set selected of (checkbox) to true

(You don’t have to handle the false case, because the checkbox is already unchecked by default in your repeater row.)

Then you’d add the following code to the checkbox in the repeater row:

OnSelected
  Update Rows Set (column) isChecked to true for This (row) in repeater

OnUnselected
  Update Rows Set (column) isChecked to false for This (row) in repeater

You would do the same for the field value (assuming you have a dataset column called ‘fieldValue’) by adding this command to the field:

OnLostFocus
  Update Rows Set (column) fieldValue to [[This.text]] for This (row) in Repeater

You would then add a command to OnItemLoad setting the text of the field to [[Item.fieldValue]]

This article should provide a solid understanding of how the repeater works, and has a number of examples.

Here’s a sample file with the solution above: saveValuesWhenAddingRow.rp (55.1 KB)

1 Like

Thanks very much for that, it works perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.