+ Start a Discussion
Kiran Kumar 77Kiran Kumar 77 

Pagination using Offset

Hi Iam trying to do paination using offset keyword next button is working but previous button in not working..


<apex:page controller="trpaginationoffsetContrller">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!ConList}" var="con">
                    <apex:column value="{!con.Name}"/>
                    <apex:column >
                        <table  width="100%">                
                  <apex:repeat value="{!ConList}" var="ContactSelec">
                  <tr><td>{!ContactSelec.FirstName}</td> <td>{!ContactSelec.LastName }</td></tr>
                  </apex:repeat>
                  </table>
                    </apex:column>
                   
                </apex:pageBlockTable>
            </apex:pageBlockSection>
           
            <apex:commandButton value="Previous" action="{!Previous}"/>
           
           
            <apex:commandButton value="Next" action="{!Next}"/>
           
        </apex:pageBlock>
    </apex:form> 
</apex:page>

--------------------------------------------------------------------------------------------

public class trpaginationoffsetContrller {
    public List<Contact> ConList {get;set;}
    public Integer Counter = 0;
    public trpaginationoffsetContrller(){
       
        //ConList = new List<Contact>
        ConList = [select Id, Name,FirstName, LastName  from Contact ORDER BY name Limit 10 OFFSET 0];
    }
    public void Next(){
        Counter ++;
        //ConList = new List<Contact>
        ConList = [select Id, Name,FirstName, LastName from contact ORDER BY name
                   Limit 10 OFFSET 10
                  ];
        update ConList;
    }
    public void Previous(){
        counter --;
        conList = [select Id, Name,FirstName, LastName from Contact ORDER BY name limit 50 OFFSET 10]; 
        update ConList;
    }
}
and can you help me Account Names diplay its related contacts only in VF page
Shabbir ShaikShabbir Shaik
Hi Kiran Kumar,

Please use Pagereference Method for Pagination and to display Account name use query like this,

select Id, Name,FirstName, LastName,Account.Name  from Contact ORDER BY name Limit 10 OFFSET 0

Try this.....
sfdcgurussfdcgurus
<apex:page controller="PagingController">
    <apex:form >
        <apex:pageBlock title="Accounts" id="pgBlock">
            <apex:pageBlockTable value="{!Accounts}" var="acc" id="pgTable">
                <apex:column value="{!acc.Name}"/>
                <apex:column value="{!acc.BillingCity}"/>
                <apex:column value="{!acc.BillingState}"/>
                <apex:column value="{!acc.Phone}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


==================

public with sharing class PagingController {

    public PagingController (){
        CountTotalRecords= [select count() from Account];
    }

    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 5;
    public list lstAccount;

    public list getAccounts(){
        lstAccount = new list();
        lstAccount = [Select id, Name, BillingCity, BillingState, Phone from Account order by Name limit :QueryLimit offset :OffsetSize];
        return lstAccount;
    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        return null;
    }
}


Aftab Alam 5Aftab Alam 5
what is use of below 2 line in above code-

    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}