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
imran Rahmanimran Rahman 

Add pagination to my visualforce page

I have created a visualforce page that uses a custom controller to fetch data using SOQL and display them in my dataList.
At this moment in time I have manually limited the rows using the LIMIT string in SOQL but what I would like to do is show X number of items per page and then have pagination so users can navigate through the pages.

How can I modify my existing code to reflect this?

Visualforce:
<apex:page controller="NewsController">
....

<apex:dataTable value="{!newsitems}" var="article" id="theTable" rowClasses="odd,even" 
styleClass="tableClass" columnClasses="col-name, col-title, col-date, col-link">
           <apex:column>
                 <apex:facet name="header">#</apex:facet>
                 <apex:outputText value="{!article.name}"/>
           </apex:column>
           <apex:column>
                 <apex:facet name="header">Article</apex:facet>
                 <apex:outputText value="{!article.title__c}"/>
           </apex:column>
           <apex:column>
                 <apex:facet name="header">Publish Date</apex:facet>
                 <apex:outputText value="{!article.publish_date__c}"/>
           </apex:column>
           <apex:column>
                 <apex:facet name="header"></apex:facet>
                 <apex:outputLink value="/{!article.id}">View Article ></apex:outputLink>
           </apex:column>
     </apex:dataTable>
NewsController:
public class NewsController {

    public LIST<News__C> getNewsitems() {
        return [SELECT Id, Name, Title__C, Publish_Date__c FROM News__c ORDER BY LastModifiedDate DESC LIMIT 10];
    }

}

Vamsi KrishnaVamsi Krishna
Imran,
there are heaps of blog posts & articles on how to implement pagination in visualforce pages either using StandardSetController, OFFSET in SOQL, or with query & queryMore methods etc..

these links will give you a headstart...

https://developer.salesforce.com/page/Paginating_Data_for_Force.com_Applications
http://www.salesforcegeneral.com/salesforce-articles/use-soqls-offset-clause-to-paginate-your-visualforce-pages.html
https://developer.salesforce.com/blogs/tech-pubs/2012/06/the-joys-of-soql-pagination.html
http://cloudfollowsdotcom.wordpress.com/2012/12/27/soqloffset/
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I tried many but always struggled, the best thing i came along was:

http://blogforce9.blogspot.com.au/2013/11/pageblocktableenhanceradv-yet-another.html

Basically, you can install it into your salesforce org. It will then be a component inside your org, after this it is simply a matter of adding this tag to your page, i guess the best place would be under the apex:page tag:

<c:PageBlockTableEnhancerADV targetPbTableIds="pbtSur"  pageSizeOptions="5,10,15" defaultPageSize="5" enableExport="False"/>

where it says targetPbTableIds="pbtSur", just put your pageblocktable id in, exampel:

<apex:pageBlockTable value="{!account.Transactions__r}" var="Tra" cellPadding="4"  border="4" id="pbtSur">

The pageblocktable will morph into a new skin and you will be able to use pagination among other things, just remember to isntall the pageblocktable enhancer by following the links on the website. 

If this answer solves ytour problem, pelase mark as best answer.