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
tarun jain 110tarun jain 110 

How we add pagination in flowing code

<apex:page controller="Checkbox_Class" Tabstyle="Account">
<apex:form>
<apex:pageBlock Title="Accounts with CheckBoxes"> <apex:pageBlockSection Title="List of Available Accounts">
<apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/> </apex:inputCheckbox></apex:facet> <apex:inputCheckbox value="{!a.selected}" id="checkedone"> <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/> </apex:inputCheckbox></apex:column> <apex:column headervalue="Account Name" value="{!a.acc.Name}" /> <apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />
<apex:column headervalue="Phone" value="{!a.acc.Phone}" />
</apex:dataTable> </apex:pageBlockSection>
<apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS"> <apex:dataTable value="{!SelectedAccounts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column headervalue="Account Name" value="{!s.Name}" />
<apex:column headervalue="Account Number" value="{!s.AccountNumber}" />
<apex:column headervalue="Phone" value="{!s.Phone}" />
</apex:dataTable> </apex:pageBlockSection> </apex:pageBlock>
</apex:form>
<script> function checkAll(cb) {
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{ if(inputElem[i].id.indexOf("checkedone")!=-1) inputElem[i].checked = cb.checked; }
} </script>
</apex:page>

Controller

public class Checkbox_Class {
 List<accountwrapper> accountList = new List<accountwrapper>();
List<Account> selectedAccounts = new List<Account>();
public List<accountwrapper> getAccounts()
{
for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 5])
accountList.add(new accountwrapper(a));
return accountList; }
public PageReference getSelected() {
selectedAccounts.clear();
for(accountwrapper accwrapper : accountList)
if(accwrapper.selected == true)
selectedAccounts.add(accwrapper.acc);
return null; }
public List<Account> GetSelectedAccounts()
{ if(selectedAccounts.size()>0)
return selectedAccounts;
else return null;
} public class accountwrapper
{
public Account acc{get; set;}
public Boolean selected {get; set;}
public accountwrapper(Account a)
{
acc = a; selected = false;
} } }
tarun jain 110tarun jain 110
I want to add pagination
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below Updated Code, 

Class
public class Checkbox_Class {
 List<accountwrapper> accountList = new List<accountwrapper>();
List<Account> selectedAccounts = new List<Account>();
 List<accountwrapper> AccountsPage{get;set;}
 
public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id, Name, AccountNumber, Phone from Account limt100]));
               
                con.setPageSize(5);
            }
            return con;
        }
        set;
    }
    
public List<accountwrapper> getAccounts()
{
AccountsPage= new List<accountwrapper>();
for (Account category : (List<Account>)con.getRecords())
{
            AccountsPage.add(new accountwrapper(category));
           
            }
return AccountsPage; 
}
public PageReference getSelected() 
{

selectedAccounts.clear();
for(accountwrapper accwrapper : AccountsPage)
if(accwrapper.selected == true)
selectedAccounts.add(accwrapper.acc);
return null;
 
}
public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

   
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

     
     public void first() {
         con.first();
     }

     
     public void last() {
         con.last();
     }

     
     public void previous() {
         con.previous();
     }

     
     public void next() {
         con.next();
     }

      
     public void cancel() {
         con.cancel();
     }

public List<Account> GetSelectedAccounts()
{ if(selectedAccounts.size()>0)
return selectedAccounts;
else return null;
} 
public class accountwrapper
{
public Account acc{get; set;}
public Boolean selected {get; set;}
public accountwrapper(Account a)
{
acc = a; 
selected = false;
} 
} 
}

Page: 
<apex:page controller="Checkbox_Class" Tabstyle="Account">
<apex:form >
<apex:pageBlock Title="Accounts with CheckBoxes"> <apex:pageBlockSection Title="List of Available Accounts">
<apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/> </apex:inputCheckbox></apex:facet> <apex:inputCheckbox value="{!a.selected}" id="checkedone"> <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/> </apex:inputCheckbox></apex:column> <apex:column headervalue="Account Name" value="{!a.acc.Name}" /> <apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />
<apex:column headervalue="Phone" value="{!a.acc.Phone}" />
</apex:dataTable> </apex:pageBlockSection>
    <apex:panelGrid columns="4">
    <apex:commandLink action="{!first}">First</apex:commandlink>
    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    <apex:commandLink action="{!last}">Last</apex:commandlink>    
    </apex:panelGrid> 
      
<apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS"> <apex:dataTable value="{!SelectedAccounts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column headervalue="Account Name" value="{!s.Name}" />
<apex:column headervalue="Account Number" value="{!s.AccountNumber}" />
<apex:column headervalue="Phone" value="{!s.Phone}" />
</apex:dataTable> </apex:pageBlockSection> </apex:pageBlock>

</apex:form>
<script> function checkAll(cb) {
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{ if(inputElem[i].id.indexOf("checkedone")!=-1) inputElem[i].checked = cb.checked; }
} </script>
</apex:page>

Hope this will work. 

Mark Best Answer if its work for you. 

Thanks
karthik
 
tarun jain 110tarun jain 110
Thanks karthik.
I am facing some issue in copy of  Account record in to custom object. How can we copy all Account recordin to custom object. Please provide code for that.