I am trying to populate a list of answers. I have a list of questions and an empty array of answers that I pass to a visualforce page. The code in my visualforce page is:

<apex:variable var="index" value="{!-1}"/>

            <apex:repeat value="{!sow.Questions}" var="q">

                <apex:outputtext style="font-size:16px" value="{!q}"/><br/>
                <apex:variable value="{!index+1}" var="index"/> <!--increment--> 

                <td><apex:inputTextarea style="width: 400px; height: 100px" value="{!sow.Answers[index]}" id="ans" /></td><br/><br/><br/>


The problem I am having is that when I save this, it will only save at the final index. I think because it runs asynchronously, that the controller will only see answers[2] but I may be wrong. Is there a better way to do this? Thank you


apex:variable doesn't always work well in these situations. Ideally, you should make an index list manually:

public Integer[] getIndexes() {
  Integer size = answers.size();
  Integer[] results = new Integer[size];
  for(Integer index = 0; index < size; index++) {
    results[index] = index;
  return results;

Which you then use in your iterator:

<apex:repeat value="{!sow.indexes}" var="index">
  ... {!sow.questions[index]} ... {!sow.answers[index]} ...

This eliminates the need for the apex:variable, which can be tricky to use correctly in some situations.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.