Contents[Hide]

Send data to pageBlockTable which allows editing fields values and then save results.

In this example we want to collect data which will be displayed by apex:pageBlockTable where isActive filed is editable and then we want to save result of editing.

Right flow of passing data to visual force page is:
- Create global variable
- Fill in values in constructor
- Pass values by Getter method.

Do not fill in values in getter method as it is one way direction (pushing data to page) so save action will not be able to read changed values.
Read more about that.

Controller:

public with sharing class editOrderController {

    /****************************************/
    /***** Controller global variables ******/
    /****************************************/
    ApexPages.StandardController stdController; // Standard controller
    List<ptt_cm_Package__c> cmPackageObj = new List<ptt_cm_Package__c>();

    // Automatic get set
    public String selectedPackageId {get; set;}
   
    /****************************************/
    /**** Custom controller constructor *****/
    /****************************************/
    
    public as_editOrderController(ApexPages.StandardController controller) {
        this.stdController = controller;
        this.cmOrderObj = (ptt_cm_Order__c)stdController.getRecord();
                
        // Get a list of packages assigned for this order                
        cmPackageObj = [SELECT id, Name, Is_Active__c, Index__c, Package_Item__c, Package_Item_Code__c
                        FROM cm_Package__c
                        WHERE Order__c = :cmOrderObj.Id];                
        }     
    }
    
    /************** Getters *****************/
    /*** Passing values to VS Page fields ***/
    /****************************************/

    public List<ptt_cm_Package__c> getPackagesTableContent() {                                      
        return this.cmPackageObj;
    }

    /************** Buttons *****************/
    /** Actions invoked by custom buttons ***/
    /****************************************/

    public PageReference doSavePackagesState () {
        update cmPackageObj;
        return null;
    }

    public PageReference doDeletePackage() {
        Database.Delete(selectedPackageId);
        return null;
    }

Visualforce:

 <apex:pageBlock title="Packages" id="PackagesList">

            <!-- Package buttons -->
            <apex:pageBlockButtons location="top" >
                    <apex:commandButton value="Add" action="{!doAddPackage}"/>
                    <apex:commandButton id="SaveActiveState" value="Save" action="{!doSavePackagesState}" reRender="PackagesList"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockTable id="packagesList" value="{!packagesTableContent}" var="package">
                <!-- Action column -->                
                <apex:column headerValue="Action" width="20px" >                 
                    <a href="javascript:if (window.confirm('Are you sure?')) DeletePackage('{!package.Id}');" style="font-weight:bold">Del</a>
                </apex:column>
            
                <!-- Package column -->
                <apex:column headerValue="Package" value="{!package.ptt_CM_Package_Item__c}" width="100px"/>
                
                <!-- Code column -->
                <apex:column headerValue="Code" value="{!package.ptt_Package_Item_Code__c}" width="100px"/>
                
                <!-- Index column -->
                <apex:column headerValue="Index" value="{!package.ptt_Index__c}" width="100px"/>
                
                <!-- Active selection column, data which are updated during inline edit -->
                <apex:column headerValue="Active" width="50px">            
                    <apex:outputField value="{!package.ptt_Is_Active__c}">
                        <apex:inlineEditSupport showOnEdit="SaveActiveState"  />
                    </apex:outputField>
                </apex:column>
            </apex:pageBlockTable>
 
        </apex:pageBlock>
        
        <apex:actionFunction action="{!doDeletePackage}" name="DeletePackage" reRender="PackagesList" >
           <apex:param name="packageId" value="" assignTo="{!selectedPackageId}"/>
        </apex:actionFunction>

Now save button will corectly save changed values.

   
© Paweł Woźniak