I have repeat within a repeat to create a poll question and answer text fields in a form.
At the top level the repeat is to create a question to create the questions.
There is an answer repeat within this question repeat used to create an answer text field to create the answer options.
Like this
[Add question Button]
1. Question (repeat1)
[Add answer Button]
Answer (repeat2)
Answer (repeat2)
Answer (repeat2)
At the moment I am using a variable “Question” and “Answer” to progress the value of the variables +1 to add the repeat rows.
This is all fine at the question level, so every-time the “Question” variable + 1 it creates a new question text field row. Within this row is another repeat which allows you add Answer options.
However, when you add second question “question” variable progresses = “2” it works but the problem is that it pre-populates the “Answers” because of course it is using the same “Answers” variable so it will just add the number of Answer rows based on what that variable was set in question 1.
My question.
Other than adding additional variables for each answer set for each question, is there a way to create a a variable or similar option on the go so I can create independent answer sets without having to hard code a variable for ever answer set??
I think what I am asking is can I set a dynamic variable at each “Question” repeat level based on the row index as the assigned name?
Am a bit short of time to look at the full details of your example, but if I understand correctly, this is something I do with two data stores rather than with variables…
So adding a new question is done with a flow which creates the new question data store item and the appropriate number of answer data store items for your needs, each of which reference the $id or a manually created id of the respective question.
I can explain some more if you need it when am at my computer rather than replying on a little phone screen!
It’s the end of the day, so I may have some of the syntax a bit wrong! But here are the key points:
Datastores for questions and answers
In this simple example, question_id/answer_id are just the next sequential number from the existing count. You may need a .count()+1 depending how you work and something more sophisticated to insert and delete questions/answers.
You are updating the answer datastore on the value of question_id AND answer_id, and the only way to do that right now is to combine the two into another field question_answer. So if question_id=5 and answer_id=2 then question_answer=“5_2”.
Each datastore’s question/answer text is updated using dmx-on:update
So part of your design will be how you manage the question_id... and it will depend on what functions you have such as the ability to insert / delete / manage the order of questions... the most important thing is that it is unique. I sometimes have a variable outside of the repeats called next_question_id which is questions.data.sort(question_id).last(1)[0].question_id + 1 (or something like that...) to return me the highest question_id plus one.
This variable you need as you have a fieldname called question_id in both questions and answers... so it is to make sure you are using the value in the outer loop.
I hope that all makes sense and answers your questions!
Antony.
Well I mentioned flows before because sometimes, when the design gets “real world”, it can be easier to put the on-click commands in a flow rather than listing them directly in the on-click entry for the button. You have all the power of if-the-else, variables etc to play with.
So basically the flow just allows you to do more clever things in your on-click.
But as far as my simple example is concerned, doing them directly in the on-click works just fine as there are only 1-2 commands to execute.
If I understand this correctly, you can also just create a variable inside the repeat, and use that variable as the repeat expression for answer.
Not sure why is there a need for data store here.
Each variable created inside the question repeat, will be unique in its own scope.
Naming of input elements will have to be done carefully… so that it creates a repeatable structure on the server side when sent from POST.
There, just use nested repeat steps to insert into DB.