2

I am having a select option within a pageblocktable

How do i get the value of each select option into a controller? If there are ten rows then we would need the value of the select from the 10 rows. What do we set the value for the select option

On the onchange event in the select option i need to autopopulate other fields in the row. Any pointers on how we can achieve this?

Thanks

2 Answers 2

3

You'll need to provide a different value parameter for each selectlist, otherwise they will all write into the same variable and the last to do it will become the value for all of them.

This is a great use case for a wrapper class, as that way you colocate the selected value with the rest of the information being presented in the datatable. For example, if you have a list of account sobjects, you'd have a wrapper class that holds the account and the selected value:

public class AccWrap
{
   public Account acc {get; set;}
   public String selVal {get; set;}

   public AccWrap(Account acc)
   {
      this.acc=acc;
   }
}

Then you'd build a list of these for display:

public List<AccWrap> wrappers {get; set;}

public MyController()
{
   wrappers=new List<AccWrap>();
   List<Account> accs=[select id, Name from Account limit 10];
   for (Account acc : accs)
   {
      AccWrap wrap=new AccWrap(acc);
      wrappers.add(wrap);
   }
}

Then in the page, you'd use this collection to back the pageblocktable:

<apex:pageBlockTable value="{!wrappers}" var="wrap">
   <apex:column value="{!wrap.Name}"/>
   <apex:column headerValue="Choose">
      <apex:selectList value="{!wrap.selVal}"/>
         <apex:selectOptions value="{!myOptions}"/>
      </apex:selectList>
   </apex:column>

With regard to the onchange, you'd need to add an actionsupport to the selectlist to execute an action method when the onchange event happens. This would traverse the list of wrappers and populate any additional fields based on the value of 'selVal'.

2
  • Thanks Bob.. On the actionsupport.. Should i need to traverse through all of the wrapper records and update all the records. is there a way i just can update the changed record. The scenario is based on my selection in select option another field value gets defaulted and the user can change it. If i were to loop through all the records in the list and update the field then i would loose the changed value of the user.
    – Prady
    Dec 13, 2012 at 10:55
  • You can add a parameter to the onchange event that identifies the wrapper class. For instance, if it is wrapping an account you could pass the id through. Then you iterate the wrappers looking for the account that matches the id and update just that. Dec 13, 2012 at 14:02
1

Just a quick note to Bob's solution. If you iterate a list of sObjects and selectOption list is a picklist so the selected value will be automatically stored in a corresponding object. Apex:

public List<Account> accounts { get; set; }

Visualforce:

<apex:pabeBlockTable value="{!accounts}" var="acc">
    <apex:column headerValue="{!$ObjectType.Account.fields.Country__c.label}">
        <apex:inputField value="{!acc.Country__c}">
            <apex:actionSupport event="onchange" action="{!doSomething}" reRender="somePanel"/>
        </apex:inputField>
    </apex:column>

</apex:pabeBlockTable>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .