function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
saimadhusaimadhu 

How to create a table with checkboxes

Hi,

I have a select option with some existing contact list.

here is my code:

<apex:outputpanel id="contract" >
<apex:pageblocksection title="Existing contract" >

<apex:pageblocksectionitem >
<apex:outputlabel value="Select the contact"/>
<apex:selectList value="{!existcontactName}" size="1" >
<apex:selectOptions value="{!contactlist}" />
</apex:selectList>

</apex:pageblocksectionitem>
</apex:pageblocksection> 

controller() is

public void updatecontactlist() {



contactName = [select Id,Name,StageName,CloseDate from contact where AccountId = :acctid.AccountId];
contactlist.add(new selectOption('',' - Select contact Name -'));
for (contact ot: contactName) {
contactlist.add(new selectOption(ot.Id,ot.Name+' , '+ot.StageName+' , '+ot.CloseDate));

}
}

 

now instead of select option i want to display a table with this id,name,stage,closedate and also a checkbox.

by default all check boxes are deselcte and if i click a chekbox then it should get that contact id.

how can i write for this.

any sample code please

 

 

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

Hi 

    I am posting my code from which you can get your code .....

      

<apex:page StandardController="Contact" extensions="Listcontactcontroller">

<script>
function confirmDelete() {
var isDelete = confirm("Are you sure you wish to Delete?");
if (isDelete) return true;
return false;
}
</script>

<style type="text/css">
.showline:hover
{
text-decoration: underline;
color: blue;
}
</style>

<apex:pageMessages />

<apex:form id="theForm">
<apex:pageBlock Title="ALL CONTACTS">
<apex:commandButton value="mass delete" action="{!massdelete}" style="color:blue;" id="massdelete" onclick="return confirmDelete()"/>
<apex:commandButton value="Save" action="{!tosave}" id="saveButton" />

<apex:outputPanel style="float:middle">
<apex:pageBlockTable value="{!Contacts}" var="c" cellpadding="2" border="1" rowClasses="odd,even" styleClass="tableClass" id="opp_table">
<apex:column headerValue="All contact Name" styleClass="showline">
<apex:inputCheckbox value="{!c.selected}">

<apex:actionSupport event="onclick" action="{!getSelected}" rerender="Selected_PBS"/>

</apex:inputCheckbox> &nbsp;
<b>
<apex:commandLink value="{!c.con.Name}" style="text-decoration:none;" action="{!Go}" title="for contact details">
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>

</apex:column>

<apex:column headerValue="Account Name" styleClass="showline">
<b>
<apex:commandLink value="{!c.con.Account.Name}" style="text-decoration:none;" action="{!redirect}" title="for contact details" target="blank" >
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>
</apex:column>

<apex:column headerValue="Contact No" >
<apex:outputfield value="{!c.con.MobilePhone}" />

</apex:column>

<apex:column headerValue="Email">
<apex:outputField value="{!c.con.Email}"/>
</apex:column>

<apex:column headerValue="Action">
<apex:commandButton value="Edit" title="to edit contact" id="editButton" action="/apex/pcontactdetail?Id={!c.con.id}" style="color:blue">
</apex:commandButton>&nbsp;&nbsp;
<apex:outputPanel styleClass="showline">
<b>
<apex:commandLink value="Delete" style="text-decoration:none;" action="{!todeletecontact}" onclick="return confirmDelete()">
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>

</apex:outputPanel>
</apex:column>
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton" hideOnEdit="massdelete"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

My controller is ...

 

public class Listcontactcontroller {
     
      //All variable decliration
        public Contact contact{get;set;}
        public Id Id{get;set;}
     
      
        List<contactwrapper> contactList = new List<contactwrapper>();
        List<Contact> selectedContacts = new List<Contact>();
    
        public Listcontactcontroller(ApexPages.StandardController controller) {
            Contact contact = new Contact();
        }
        
        
      //All contacts in a pageblock table
        public  List<contactwrapper> getContacts() {
      
            for(Contact c: [select Id,Name,Account.Name,MobilePhone,Email from Contact order by createdDate desc ])
                contactList.add(new contactwrapper(c));
                return contactList;
          }
      
            
       // selected contacts     
             
          public PageReference getSelected() {
    
             selectedContacts.clear();
             for(contactwrapper conwrapper: contactList) 
                 if(conwrapper.selected == true)
                 selectedContacts.add(conwrapper.con); 
       
                 return null;
        }
        
        
          public List<Contact> GetSelectedContacts(){
      
            if(selectedContacts.size()>0)
                return selectedContacts;
            else
                return null;
         } 
       
      //saving contact from inline edit..  
          public PageReference tosave() { 
              update selectedContacts;
              PageReference listcontact=new PageReference('/apex/listcontact');
              listcontact.setRedirect(true);
              return listcontact;
           }
       
    // deleting more then one contact in one time..
          public PageReference massdelete() {
    
              if(selectedContacts.size()>0){
                  delete selectedContacts;
           }
       
       
       
         else{
               ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one Contact.'));
               return null;
             }
        
      
          PageReference listcontact=new PageReference('/apex/listcontact');
          listcontact.setRedirect(true);
          return listcontact;
        
      }
             
    // Redirecting to Contact detail page
     public PageReference Go() {
   
         Id = ApexPages.currentPage().getParameters().get('conId');
         contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
         return (new ApexPages.StandardController(contact)).view();
       }
    // redirect to Account Page
     public PageReference redirect() {
   
         Id = ApexPages.currentPage().getParameters().get('conId');
         contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
         return (new ApexPages.StandardController(contact.Account)).view();
       }
    
   
    // deleting single contact
      public PageReference todeletecontact() {
    
          Id = ApexPages.currentPage().getParameters().get('conId');
          contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
          delete contact;
          PageReference listcontact=new PageReference('/apex/listcontact');
          listcontact.setRedirect(true);
          return listcontact;
       }
    
    // wrapper class
      public class contactwrapper
      {
          public Contact con{get; set;}
          public Boolean selected {get; set;}
          public contactwrapper(Contact c)
          {
               con = c;
              selected = false;
          }
    }
 }

 

Just copy and paste and see your requirement is inside this .. You need to customize 

 

 

Did this post answers your questions..if so please mark as solutions

 

Thanks

asish

   

All Answers

asish1989asish1989

Hi 

    I am posting my code from which you can get your code .....

      

<apex:page StandardController="Contact" extensions="Listcontactcontroller">

<script>
function confirmDelete() {
var isDelete = confirm("Are you sure you wish to Delete?");
if (isDelete) return true;
return false;
}
</script>

<style type="text/css">
.showline:hover
{
text-decoration: underline;
color: blue;
}
</style>

<apex:pageMessages />

<apex:form id="theForm">
<apex:pageBlock Title="ALL CONTACTS">
<apex:commandButton value="mass delete" action="{!massdelete}" style="color:blue;" id="massdelete" onclick="return confirmDelete()"/>
<apex:commandButton value="Save" action="{!tosave}" id="saveButton" />

<apex:outputPanel style="float:middle">
<apex:pageBlockTable value="{!Contacts}" var="c" cellpadding="2" border="1" rowClasses="odd,even" styleClass="tableClass" id="opp_table">
<apex:column headerValue="All contact Name" styleClass="showline">
<apex:inputCheckbox value="{!c.selected}">

<apex:actionSupport event="onclick" action="{!getSelected}" rerender="Selected_PBS"/>

</apex:inputCheckbox> &nbsp;
<b>
<apex:commandLink value="{!c.con.Name}" style="text-decoration:none;" action="{!Go}" title="for contact details">
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>

</apex:column>

<apex:column headerValue="Account Name" styleClass="showline">
<b>
<apex:commandLink value="{!c.con.Account.Name}" style="text-decoration:none;" action="{!redirect}" title="for contact details" target="blank" >
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>
</apex:column>

<apex:column headerValue="Contact No" >
<apex:outputfield value="{!c.con.MobilePhone}" />

</apex:column>

<apex:column headerValue="Email">
<apex:outputField value="{!c.con.Email}"/>
</apex:column>

<apex:column headerValue="Action">
<apex:commandButton value="Edit" title="to edit contact" id="editButton" action="/apex/pcontactdetail?Id={!c.con.id}" style="color:blue">
</apex:commandButton>&nbsp;&nbsp;
<apex:outputPanel styleClass="showline">
<b>
<apex:commandLink value="Delete" style="text-decoration:none;" action="{!todeletecontact}" onclick="return confirmDelete()">
<apex:param name="conId" value="{!c.con.id}" />
</apex:commandLink>
</b>

</apex:outputPanel>
</apex:column>
<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton" hideOnEdit="massdelete"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

My controller is ...

 

public class Listcontactcontroller {
     
      //All variable decliration
        public Contact contact{get;set;}
        public Id Id{get;set;}
     
      
        List<contactwrapper> contactList = new List<contactwrapper>();
        List<Contact> selectedContacts = new List<Contact>();
    
        public Listcontactcontroller(ApexPages.StandardController controller) {
            Contact contact = new Contact();
        }
        
        
      //All contacts in a pageblock table
        public  List<contactwrapper> getContacts() {
      
            for(Contact c: [select Id,Name,Account.Name,MobilePhone,Email from Contact order by createdDate desc ])
                contactList.add(new contactwrapper(c));
                return contactList;
          }
      
            
       // selected contacts     
             
          public PageReference getSelected() {
    
             selectedContacts.clear();
             for(contactwrapper conwrapper: contactList) 
                 if(conwrapper.selected == true)
                 selectedContacts.add(conwrapper.con); 
       
                 return null;
        }
        
        
          public List<Contact> GetSelectedContacts(){
      
            if(selectedContacts.size()>0)
                return selectedContacts;
            else
                return null;
         } 
       
      //saving contact from inline edit..  
          public PageReference tosave() { 
              update selectedContacts;
              PageReference listcontact=new PageReference('/apex/listcontact');
              listcontact.setRedirect(true);
              return listcontact;
           }
       
    // deleting more then one contact in one time..
          public PageReference massdelete() {
    
              if(selectedContacts.size()>0){
                  delete selectedContacts;
           }
       
       
       
         else{
               ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one Contact.'));
               return null;
             }
        
      
          PageReference listcontact=new PageReference('/apex/listcontact');
          listcontact.setRedirect(true);
          return listcontact;
        
      }
             
    // Redirecting to Contact detail page
     public PageReference Go() {
   
         Id = ApexPages.currentPage().getParameters().get('conId');
         contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
         return (new ApexPages.StandardController(contact)).view();
       }
    // redirect to Account Page
     public PageReference redirect() {
   
         Id = ApexPages.currentPage().getParameters().get('conId');
         contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
         return (new ApexPages.StandardController(contact.Account)).view();
       }
    
   
    // deleting single contact
      public PageReference todeletecontact() {
    
          Id = ApexPages.currentPage().getParameters().get('conId');
          contact =[select Id,Name,Account.Name,MobilePhone,Email from Contact where Id = :Id];
          delete contact;
          PageReference listcontact=new PageReference('/apex/listcontact');
          listcontact.setRedirect(true);
          return listcontact;
       }
    
    // wrapper class
      public class contactwrapper
      {
          public Contact con{get; set;}
          public Boolean selected {get; set;}
          public contactwrapper(Contact c)
          {
               con = c;
              selected = false;
          }
    }
 }

 

Just copy and paste and see your requirement is inside this .. You need to customize 

 

 

Did this post answers your questions..if so please mark as solutions

 

Thanks

asish

   

This was selected as the best answer
saimadhusaimadhu

Thank you so much