Monday 7 January 2013

Check Boxes for Selecting Records:-(01/07/13)


Hi Guys, Today i am going to explain, How the check boxes to use in Vf page, How to call in apex.

Here we have scenario, The user will select one (or more) accounts on the left, click “Show Selected Accounts” and the results will show in the right.


 VF page:-


<apex:page controller="thirdinterview">
  <apex:form >
        <apex:pageBlock title="Interview Three">
       
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="out"/>
            </apex:pageBlockButtons>
           <apex:pageBlockSection columns="2">
            <apex:pageBlockTable value="{!Accounts}" var="Acc" columnsWidth="150px,150px" align="left">
                <apex:column >
                  
                    <apex:inputCheckbox value="{!Acc.selected}"/>
                </apex:column>
               
                <apex:column value="{!Acc.con.Name}" />
                <apex:column value="{!Acc.con.Phone}" />
            </apex:pageBlockTable>
        
               <apex:pageBlockTable value="{!selectedAccounts}" var="Rec" id="out" align="right" title="Selected Accounts">
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!Rec.name}"/>
                    </apex:column>  
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!Rec.Phone}"/>
                    </apex:column>            
                </apex:pageBlockTable>  
          </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
   
</apex:page>

Class:-

public class thirdinterview
{
  public List<cAccount> accList {get; set;}
  public List<Account> selectedAccounts{get; set;}
//Adding the Records to inner class and to get the values for page block table.
  public List<cAccount> getAccounts(){
        if(accList == null){
            accList = new List<cAccount>();
            for(Account acc : [select Id, Name, Phone from Account limit 25]){
                  accList.add(new cAccount(acc));
            }
         }
       return accList;
   }  
//on button click it will show the list of records what we have selected.
  public PageReference processSelected(){    
        selectedAccounts= new List<Account>();
        for(cAccount cCon : getAccounts()) {
            if(cCon.selected == true){
                selectedAccounts.add(cCon.con);
            }
        }           
        return null;
   }
  //  Inner class for capture the records
  public class cAccount {
        public Account con {get; set;}
        public Boolean selected {get; set;}
        public cAccount(Account c) {
            con = c;
            selected = false;
        }
   }
    
}

3 comments:

  1. Very nice information on here, it's nice to see examples while I'm learning to do this. But I've been pondering on if it is possible to add "selectradio" tag and have it selectable instead of clicking the account name and still return a pagemessage. I can't seem to get the page to rerender when a radio is selected. I've also tried to do this using a wrapper class but I'm still getting the same issue. Any suggestions?

    ReplyDelete
  2. Excellent Example.
    Thank you.

    ReplyDelete
  3. how can i add pagenation to above code?
    can u plz help me

    ReplyDelete