Default to previous value

I see how you can set a default value but is there a way to preset an item
value to the value in the previous iteration of the form? (slow moving data)

-steve-

I see how you can set a default value but is there a way to preset an item
value to the value in the previous iteration of the form? (slow moving data)

default answer from previous form: no. but, you can do this through
the property manager. the javarosa core is ready for it but you'd have
to implement a sqlite3 backed property manager instead of the simple
one we have in collect.

default answer from previous repetition: no. though you can provide a
default for all repetitions.

Hi @yanokwa

To your knowledge, this is still not possible?
I try this, but throw a Cycle detected

Thank you

See the documentation on dynamic defaults in repeats.

The expression in a calculation column should not be used to set a default. Even if the user modifies the value of Q1, you will get the calculated value in your submissions. See the documentation.

Thank you @LN ,
I learn about XLSForm every day!

This is very clear !

So, this example work like a charm :

And my second step is to have a default value (for the first repeating group) from a root form calculation.
Seems to work fine - maybe a comment?

[Screen in working ... ]

Jonathan

Glad the concept works!

Please make sure that user-editable fields like your Q1 use default and not calculation. In your second example, previous_value is a calculate so its expression can be in calculation. However, Q1 is user-editable so the expression you want to use as a default should be in the default column if you want the user to be able to override it. I recommend you change the image in your post so someone else can benefit from your example! Agreed we could add this to the docs as well. :+1:

Thank you for your remark.
But if I do this , it doesn't work :frowning:


Neither the default nor the previous value

Current example :
repeat_previous.xlsx (22.1 KB)

I see, the default is set immediately on form load, before the calculation has a chance to be evaluated. Is the intent for ROOT_DEFAULT to be a user-provided value or is it a known value that can be encoded in the form? If it's a known value, then it could be put directly in the if. I think you probably want a user-specified value, though.

The way I'd want to address this is to have no repeat instances until the user explicitly asks to add one. I don't believe there's a way to do that in XLSForm. The only way I can think to do it is to edit the XML form definition and remove the REPEAT instance without jr:template="" which is not very friendly to do.

The other approach that comes to mind which I hesitate to mention is to use once(${ROOT_DEFAULT}) in the calculation column and ${REPEAT}[position() = position(current()/..) - 1]/Q1 in the default column. once() means the expression is only evaluated if the field is blank. If Q1 is required, that will only be the case for that first repeat instance which is in the form definition. As soon as ROOT_DEFAULT is set by the user, that first repeat instance's value will be set exactly once.

I say I hesitate to mention it because it feels uncomfortable to me to use two different ways to get defaults and I know we did not consider the use of once and defaults together. That said, it does respect the way each construct is supposed to work so it should be safe.

You can't use once() for the other part of the default because the ${Q1} reference currently leads to a cycle error.