------------Controller----------------- public class wrapperControllerClassExample1 { //Our collection of the class/wrapper objects wrapAccount public List wrapAccountList {get; set;} public List selectedAccounts{get;set;} public wrapperControllerClassExample1(){ if(wrapAccountList == null) { wrapAccountList = new List(); for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) { // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList wrapAccountList.add(new wrapAccount(a)); } } } public void processSelected() { selectedAccounts = new List(); for(wrapAccount wrapAccountObj : wrapAccountList) { if(wrapAccountObj.selected == true) { selectedAccounts.add(wrapAccountObj.acc); } } } // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value public class wrapAccount { public Account acc {get; set;} public Boolean selected {get; set;} public wrapAccount(Account a) { acc = a; selected = false; } } }", "answerCount": 6, "upvoteCount": 0, "datePublished": "2015-02-15T09:05:50.000Z", "author": { "@type": "Person", "name": "Developer Forums User (Inactive)", "url": "https://trailblazers.salesforce.com/profileView?u=0054V00000H86IwQAJ", "affiliation": { "@type": "Organization", "name": "Salesforce Developer Forums" } }, "acceptedAnswer": { "@type": "Answer", "text": "Short answer is that there isn't any code that changes it. When you click on one of the checkboxes in the browser, you are changing the value of the selected property in the View State ( https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State ) on your browser. When you call the processSelected action, your browser sends the updated View State back to the controller, now with one or two wrapAccount objects with their selected property set to true. There's probably some JS loaded with the page that actually manipulates the View State objects, but I haven't heard of anyone accessing/manipulating it. Does that help?", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4D5RSAV", "datePublished": "2015-02-16T19:14:06.000Z", "author": { "@type": "Person", "name": "Developer Forums User (Inactive)", "url": "https://trailblazers.salesforce.com/profileView?u=0054V00000H86IwQAJ", "affiliation": { "@type": "Organization", "name": "Salesforce Developer Forums" } } }, "suggestedAnswer": [] } } Skip to main content

I was just trying to understand wrapper classes and then happen to see the below code. Understood everything almost but one thing I am not able to understand is; if I select one or two Account records by clicking on the checkbox (and not all account records through the topmost checkbox), then how the value of the Boolean variable "Selected" is becoming True from false for the selected account records?

See the below Visualforce code and controller class:

-----------VF------------------------------

<apex:page controller="wrapperControllerClassExample1" sidebar="false">

    <script type="text/javascript">

        function selectAllCheckboxes(obj,receivedInputID){

            var inputCheckBox = document.getElementsByTagName("input");

            for(var i=0; i<inputCheckBox.length; i++){

                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){

                    inputCheckBox[i].checked = obj.checked;

                }

            }

        }

    </script>

    <apex:form >

        <apex:pageBlock >

            <apex:pageBlockButtons >

                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2"/>

            </apex:pageBlockButtons>

 

            <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">

 

                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">

                    <apex:column >

                        <apex:facet name="header">

                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>

                        </apex:facet>

                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>

                    </apex:column>

                    <apex:column value="{!accWrap.acc.Name}" />

                    <apex:column value="{!accWrap.acc.BillingState}" />

                    <apex:column value="{!accWrap.acc.Phone}" />

                </apex:pageBlockTable>

 

                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">

                    <apex:column value="{!c.Name}" headerValue="Account Name"/>

                    <apex:column value="{!c.BillingState}" headerValue="Billing State"/>

                    <apex:column value="{!c.Phone}" headerValue="Phone"/>

                </apex:pageBlockTable>

 

            </apex:pageblockSection>

        </apex:pageBlock>

    </apex:form>

 

</apex:page>

------------Controller-----------------

public class wrapperControllerClassExample1 {

 

    //Our collection of the class/wrapper objects wrapAccount

    public List<wrapAccount> wrapAccountList {get; set;}

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

 

    public wrapperControllerClassExample1(){

        if(wrapAccountList == null) {

            wrapAccountList = new List<wrapAccount>();

            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {

                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList

                wrapAccountList.add(new wrapAccount(a));

            }

        }

    }

 

    public void processSelected() {

    selectedAccounts = new List<Account>();

 

        for(wrapAccount wrapAccountObj : wrapAccountList) {

            if(wrapAccountObj.selected == true) {

                selectedAccounts.add(wrapAccountObj.acc);

            }

        }

    }

 

    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value

    public class wrapAccount {

        public Account acc {get; set;}

        public Boolean selected {get; set;}

 

        public wrapAccount(Account a) {

            acc = a;

            selected = false;

        }

    }

}

6 respuestas
  1. 16 feb 2015, 19:14
    Short answer is that there isn't any code that changes it.

    When you click on one of the checkboxes in the browser, you are changing the value of the selected property in the View State (https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State) on your browser.

    When you call the processSelected action, your browser sends the updated View State back to the controller, now with one or two wrapAccount objects with their selected property set to true.

    There's probably some JS loaded with the page that actually manipulates the View State objects, but I haven't heard of anyone accessing/manipulating it.

    Does that help?