Activity Forums Salesforce® Discussions How to add select check box after displaying Lead and Contact records in visualforce page?

  • Yogesh

    Member
    November 19, 2019 at 6:39 am

    Hello,

    try this code below,I hope this will help you:-

    Visulforce page code:
    <apex:page controller="wrapperClassController">
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockButtons >
    <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="test"/>
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!contacts}" var="c" >
    <apex:column >
    <apex:inputCheckbox value="{!c.selected}"/>
    </apex:column>
    <apex:column value="{!c.con.Name}" />
    <apex:column value="{!c.con.Email}" />
    <apex:column value="{!c.con.Phone}" />
    </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock id="test">
    Total No of Selected Records :<apex:outputText value="{!value }"/>
    <apex:pageBlockTable value="{!SelectedContacts}" var="c" >
    <apex:column value="{!c.Name}" />
    <apex:column value="{!c.Email}" />
    <apex:column value="{!c.Phone}" />
    </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Apex Code:

    public class wrapperClassController {
    public List<cContact> contactList {get; set;}
    public List<Contact> selectedContacts{get;set;}
    public Integer value {get;set;}
    public List<cContact> getContacts() {
    if(contactList == null) {
    contactList = new List<cContact>();
    for(Contact c : [select Id, Name, Email, Phone from Contact limit 10]) {
    contactList.add(new cContact(c));
    }
    }
    return contactList;
    }
    public PageReference processSelected() {
    selectedContacts = new List<Contact>();
    for(cContact cCon : getContacts()) {
    if(cCon.selected == true) {
    selectedContacts.add(cCon.con);
    }
    }
    value = selectedContacts.size();
    System.debug('printingtcontc'+selectedContacts.size());
    return null;
    }
    public List<Contact> getSelectedContacts(){
    System.debug('printingtcontc inside get'+selectedContacts.size());
    if(selectedContacts.size()>0eturn selectedContacts;
    else return null;
    }
    public class cContact {
    public Contact con {get; set;}
    public Boolean selected {get; set;}
    public cContact(Contact c) {
    con = c;
    selected = false;
    }
    }
    }

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos