ForEach loop condition to fetch data from DB in correct sequence in a!checkboxField().

Certified Senior Developer

My requirement is to fetch the correct option for the respective question which is there in the database. So what condition i need to apply in choiceLabels() as am getting questions in the correct order only but only the option from the first row is getting repeated for all the questions. so i want first 4 data to go for 1st checkboxField() , next 4 to second checkboxField() & so on.

local!data:rule!MWJ_qe_forQuestions(),
local!option:split(rule!MWJ_qe_fetchQuizOptions(),";"),
local!choose:{1,2,3,4},


contents: {
a!forEach(
items:split(local!data,";"),
expression:
a!checkboxField(
label:fv!item,
labelPosition: "ABOVE",
choiceLabels:{ a!forEach(
items:index(split(local!option,","),{local!choose},0),

expression:fv!item,
)},
choiceValues: { a!forEach(
items:index(split(local!option,","),{local!choose},0),
expression:fv!item,

)},

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Associate Developer

    Check the code below. Basically you have to play with the index. Ideally, you should work more on the data model - maybe use a map so you could have different number of answers to a question.

    a!localVariables(
      local!data: {
        "Capital of France",
        "Capital of Germany",
        "Capital of Spain"
      },
      local!option: {
        "Paris",
        "Budapest",
        "Istanbul",
        "Munich",
        "Frankfurt",
        "Berlin",
        "Zurich",
        "Bucharest",
        "Washington",
        "Madrid",
        "Mexico City",
        "Malaga"
      },
      local!noOfAnswers:4,
      {
        a!forEach(
          items: local!data,
          expression: a!localVariables(
            local!currentQuestion: fv!index,
            a!checkboxField(
              label: fv!item,
              labelPosition: "ABOVE",
              choiceLabels: {
                a!forEach(
                  items: enumerate(local!noOfAnswers),
                  expression: index(
                    local!option,
                    if(
                      local!currentQuestion = 1,
                      fv!index,
                      fv!index + local!noOfAnswers * (local!currentQuestion - 1)
                    )
                  ),
                  
                )
              },
              choiceValues: {
                a!forEach(
                  items: enumerate(local!noOfAnswers),
                  expression: index(
                    local!option,
                    if(
                      local!currentQuestion = 1,
                      fv!index,
                      fv!index + local!noOfAnswers * (local!currentQuestion - 1)
                    )
                  ),              
                )
              }
            ),        
          )
        )
      }
    )

Reply
  • 0
    Certified Associate Developer

    Check the code below. Basically you have to play with the index. Ideally, you should work more on the data model - maybe use a map so you could have different number of answers to a question.

    a!localVariables(
      local!data: {
        "Capital of France",
        "Capital of Germany",
        "Capital of Spain"
      },
      local!option: {
        "Paris",
        "Budapest",
        "Istanbul",
        "Munich",
        "Frankfurt",
        "Berlin",
        "Zurich",
        "Bucharest",
        "Washington",
        "Madrid",
        "Mexico City",
        "Malaga"
      },
      local!noOfAnswers:4,
      {
        a!forEach(
          items: local!data,
          expression: a!localVariables(
            local!currentQuestion: fv!index,
            a!checkboxField(
              label: fv!item,
              labelPosition: "ABOVE",
              choiceLabels: {
                a!forEach(
                  items: enumerate(local!noOfAnswers),
                  expression: index(
                    local!option,
                    if(
                      local!currentQuestion = 1,
                      fv!index,
                      fv!index + local!noOfAnswers * (local!currentQuestion - 1)
                    )
                  ),
                  
                )
              },
              choiceValues: {
                a!forEach(
                  items: enumerate(local!noOfAnswers),
                  expression: index(
                    local!option,
                    if(
                      local!currentQuestion = 1,
                      fv!index,
                      fv!index + local!noOfAnswers * (local!currentQuestion - 1)
                    )
                  ),              
                )
              }
            ),        
          )
        )
      }
    )

Children