Using ActionSupport and Param in a PageBlockTable

First let’s lay out an example.  We are going to have a Visualforce page that is going to show a list of opportunities. When the user selects a new stage for an opportunity in the table, then the page should call a specific action back on the controller.  In doing so the opportunity ID for that row should also be passed back.

Most of my thoughts and descriptions are written in as comments in the code.

Let’s look at some controller code first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class opportunityStageController {
     //This is the opportunityID that will be sent back when the Stage drop-down list is changed
     public string opportunityID{get;set;}
     //This is the list of opportunities to be displayed in the table.  This will be loaded in the constructor
     public list<opportunity> oppList{get;set;}
   public opportunityStageController(){
            //Constructor
           //Populate oppList as needed.  Can use a query string parameter to pass in an Account ID
           oppList = [Select ID, Name, StageName From Opportunity Limit 10];
   }
   public List<selectoption> getOppStages() {
        List<selectoption> options = new List<selectoption>();
        
        //This is some generic code to retrieve the current stage names using
        //  Dynamic Apex in case the Stage Names change in the future
        Schema.DescribeFieldResult F = Opportunity.StageName.getDescribe();
    List<schema.picklistentry> typeList = F.getPicklistValues();
            
    for (Schema.PicklistEntry TypeItem : typeList){
          options.add(new SelectOption(TypeItem.getValue(),TypeItem.getLabel()));
        }
        
        return options; 
    }
   public pageReference updateOpportunity(){
      //Update your opportunity as needed
      list<opportunity> myOppList = new list<opportunity>();
      myOppList = [Select ID, Name, StageName From Opportunity Where ID = :o pportunityID]
      //Make changes to opportunity as desired
      if(myOppList.isEmpty() == false){
           update myOppList[0];
      }
      return null;
   }  
}
//Ignore these extra Tags:
</opportunity></opportunity></schema.picklistentry></selectoption></selectoption></selectoption></opportunity>

Before you write your page write your unit tests.  Then you will know items will display and update properly before you even build your page.

Here is the Visualforce Page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<apex:page controller="opportunityStageController ">
  <apex:messages style="color:red"></apex:messages>
   <apex:form>
      <apex:pageblock title="Opportunity List">
            //This will display that the StageStatus is changed. 
            // The 'status' attribute on the actionSupport tag is referencing this
            <apex:actionstatus id="stageStatus" starttext=" Opportunity Being Updated…" stoptext=" Update Complete!"></apex:actionstatus>
            //The {!oppList} references the oppList list on the controller. 
            // oppItem is the specific opportunity used to populate each row of the table
           <apex:pageblocktable value="{!oppList}" var="oppItem" id="oppTable">
               <apex:column headervalue="Name">
                    <apex:outputtext value="{!oppItem.Name}"></apex:outputtext>
                </apex:column>
                <apex:column headervalue="Stage Name">
            <apex:selectlist value="{!oppItem.StageName}" multiselect="false" size="1">
                                 //Look at the getOppStages code in the controller. 
                                 //  It is code that does not hard code the Stage Names
                <apex:selectoptions value="{!OppStages}"></apex:selectoptions>
                                //So when the drop-down list changes the 'onchange' event will fire which
                                //  will cause the 'updateOpportunity' method to be called on the controller
                <apex:actionsupport event="onchange" action="{!updateOpportunity}" rerender="oppTable" status="stageStatus">
                                         //The opportunityID string will be populated on the controller with the id of the opportunity: "!oppItem.id"
                    <apex:param name="opportunityID" assignto="{!opportunityID}" value="{!oppItem.id}"></apex:param>
                </apex:actionsupport>
            </apex:selectlist> 
        </apex:column>
          </apex:pageblocktable>
      </apex:pageblock>
  </apex:form>
</apex:page>

I hope this gives you a good starting point with passing a value back from a specific row in a table.

Happy Coding!

This entry was posted in Technology and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>