Find your content:

Search form

You are here

Get value from Selectoption in a pageblockTable

 
Share

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


Attribution to: Prady

Possible Suggestion/Solution #1

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'.


Attribution to: Bob Buzzard

Possible Suggestion/Solution #2

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>

Attribution to: Sergej Utko
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5391

My Block Status

My Block Content