Is there a way to paginate the results from a webservice?

i am using a visualforce page which makes a callout to external webservice, get results and displayed in tabular format. i am using apex:pageBlockTable.

Questions:

a) Is that possible to paginate the records from a external webservice?

b) if so, then how the controller would be?

c)i am assuming pagination can be done only with standard and custom objects. is that true?

Answer

You can paginate anything you want. Observe:

Controller

public with sharing class pageController {
    public class Item {
        public String A { get; set; }
        public String B { get; set; }
        public String C { get; set; }
    }
    public Item[] items { get; set; }
    public Integer offset { get; set; }
    public pageController() {
       items = new Item[0];
       for(integer index = 0; index < 1000; index++) {
           item tempItem = new Item();
           tempItem.A = "A "+String.valueOf(index);
           tempItem.B = "B "+String.valueOf(index);
           tempItem.C = "C "+String.valueOf(index);
           items.add(tempItem);
       }
       offset = 0;
   }
   public void nextPage() {
       offset = Math.min(offset + 10, items.size() - 10);
   }
   public void previousPage() {
       offset = Math.max(0, offset - 10);
   }
   public void lastPage() {
       offset = items.size() - 10;
   }
   public void firstPage() {
       offset = 0;
   }
}

Page

<apex:page controller="pageController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!firstPage}" value="First" />
                <apex:commandButton action="{!previousPage}" value="Previous" />
                Starting From: {!offset+1}
                <apex:commandButton action="{!nextPage}" value="Next" />
                <apex:commandButton action="{!lastPage}" value="Last" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!items}" var="item" first="{!offset}" rows="10">
                <apex:column headerValue="A" value="{!item.A}" />
                <apex:column headerValue="B" value="{!item.B}" />
                <apex:column headerValue="C" value="{!item.C}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Note that this method only supports 1000 items on a normal page (Visualforce limits), but you could make the entire application client-side driven, potentially loading millions of rows in JavaScript and paging through them using jQuery pagination tables or writing your own JavaScript, etc.

See here that the data obviously doesn’t exist in the database at all; it was rendered on the fly.

Attribution
Source : Link , Question Author : Bforce , Answer Author : Community

Leave a Comment