5

I have visual force page which shows the related list.

public ApexPages.StandardSetController setCon{
    get{
        if(setCon == null){
            size = 5;
            string queryString = 'Select Id, Contact__c, Contact__r.Name, Submitted_at__c, Interview_Availability__c, Notice_Available__c , Stage__c, Ideally_looking_for__c from Candidate__c WHERE Job__c =\''+ApexPages.currentPage().getParameters().get('Id')+'\' order by Contact__r.Name';
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
            setCon.setPageSize(size);
            noOfRecords = setCon.getResultSize();
        }
        return setCon;
    }set;
}

I want to display the total no of page in the visual force page. Is there any method available for it?

eg: if it has 12 records and it should say 3 page found.. Any ideas?

4

Decimals also have a round() method that you could use:

size = 5.0;

...

system.debug((setCon.getResultSize()/size).round(System.RoundingMode.CEILING));

Make sure that your size variable is declared as a decimal. If you divide two integers, the fractional component of the number will be dropped, and your calculation will be one page short.


If you do use the Math.ceil() methods, keep in mind that the return type will either be decimal or double, depending on your input. You might want to cast the return value as an integer:

size = 5.0;
system.debug((Integer)Math.ceil(c.getResultSize()/size));

Also note that the setPageSize() method requires an Integer value as it's input. You could also cast your variable Double size as in integer when you use it as a parameter for that method:

setCon.setPageSize((Integer)size);

In summary, do this:

public Integer size {get; set;}
public Integer NumberOfPages {get; set;}

public ApexPages.StandardSetController setCon{
    get{
        if(setCon == null){
            size = 5;
            string queryString = 'Select Id, Contact__c, Contact__r.Name, Submitted_at__c, Interview_Availability__c, Notice_Available__c , Stage__c, Ideally_looking_for__c from Candidate__c WHERE Job__c =\''+ApexPages.currentPage().getParameters().get('Id')+'\' order by Contact__r.Name';
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
            setCon.setPageSize(size);
            noOfRecords = setCon.getResultSize();
            NumberOfPages = (noOfRecords/(Decimal)size).round(System.RoundingMode.CEILING));
        }
        return setCon;
    }set;
}
| improve this answer | |
2

There isn't a function that directly specifies the number of pages. However, all you need to do is divide the total record count by page size and round up (Math.ceil, for example). It'd be nice if this was built in, but it's trivial to calculate yourself.

| improve this answer | |
  • or <apex:outputText value=”Page {!PageNumber} of {!CEILING(ResultSize / PageSize)}”/> – cropredy Nov 28 '15 at 18:16
0

You can use getPageNumber() to current pages number. and for the total page you can store value in custom variable. Something like

(noOfRecords/size).round(System.RoundingMode.UP)

You consider that either noOfrecord or size must be decimal. I just checked this with DC and it is working as expected

Display this on your VF page. it will solve your problem.

| improve this answer | |
  • There will be no fraction to round up if you are dividing two integers. You need to use Decimal arithmetic. – martin Nov 28 '15 at 6:12
  • Yes I tried that before itself. my page size is 5. so if there is 7 records returned, I am using the ceil function still it return the pageCount as 1.. Any suggestins? – Vetriselvan Manoharan Nov 28 '15 at 6:12
0

I am using following snippet because I got some weird results using samples from the other answers here.

Decimal resultSize = setcontroller.getResultSize();
return resultSize.Divide(setcontroller.getPageSize(), 0, System.RoundingMode.UP);

For example my resultsize was 17 and my pagesize was 5. (this equals 3.4) Salesforce was always returning 3, so that is completely wrong. Following post on Salesforce Developers forum also talks about this.

See: Developer Forum post

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.