Find your content:

Search form

You are here

VF Value Element in an List/Array

 
Share

If I have multiple input fields on my page, can I assign their value to elements in a list/array?? Is this even possible and I don't understand the syntax?

Example:

Page

<apex:page controller="pageController">
  <apex:selectList value="{!array[0]}">
    <apex:selectOptions value="{!optionsList}" />
  </apex:selectList>
  <apex:selectList value="{!array[1]}">
        <apex:selectOptions value="{!optionsList}" />
      </apex:selectList>
  <apex:selectList value="{!array[2]}">
        <apex:selectOptions value="{!optionsList}" />
      </apex:selectList>
</apex:page>

Controller

public class pageController {
   public List<String> array {get; set;} // array of input values on page submit
   public List<SelectOption> optionsList {get; set;} // assigned in controller
}

Attribution to: jordan.baucke

Possible Suggestion/Solution #1

Yes that does seem possible. Except that the array needs to be initialized with some values in it. (And array is a reserved word).

With a few tweaks, what you've pasted above worked. When I clicked the commandButton, it correctly bound the selected values in each list to the elements in the list and displayed this on the vf page. Very handy I think !

<apex:page controller="pageController">
<apex:form>
<apex:pageBlock>
<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
  <apex:selectList value="{!arrayValue[0]}" size="1">
    <apex:selectOptions value="{!optionsList}" />
  </apex:selectList>
  <apex:selectList value="{!arrayValue[1]}" size="1">
        <apex:selectOptions value="{!optionsList}" />
      </apex:selectList>
  <apex:selectList value="{!arrayValue[2]}" size="1">
        <apex:selectOptions value="{!optionsList}" />
      </apex:selectList>

      <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>You have selected:</p>
                    <apex:dataList value="{!arrayValue}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>

      </apex:pageBlock>
      </apex:form>
</apex:page>

Apex Class (Controller):

public class pageController {
   public List<String> arrayValue {get; set;} // array of input values on page submit
   public List<SelectOption> optionsList {get; set;} // assigned in controller

   public pageController(){
       optionsList = new List<SelectOption>{};
       arrayValue = new List<String>{'','',''}; //INITIALISE LIST
       optionsList.add(new SelectOption('Hello', 'Hello'));
       optionsList.add(new SelectOption('World', 'World'));
       optionsList.add(new SelectOption('Java', 'Java'));
   }

   public PageReference test(){
       return null;
   }

   }

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3631

My Block Status

My Block Content