1. 分页apex class:
public with sharing class CCW_ContactPaginationController {
/*
* item in context from the page
*/
public String contextItem{get;set;}
/*
* set controller
*/
private ApexPages.StandardSetController setCon;
/*
* the contact ids selected by the user
*/
private Set<Id> selectedContactIds;
/*
* constructor
*/
public CCW_ContactPaginationController ()
{
//init variable
this.selectedContactIds= new Set<Id>();
//gather data set
this.setCon= new ApexPages.StandardSetController( [SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact] );
this.setCon.setpageNumber(1);
this.setCon.setPageSize(10);
}
/*
* handle item selected
*/
public void doSelectItem(){
this.selectedContactIds.add(this.contextItem);
}
/*
* handle item deselected
*/
public void doDeselectItem(){
this.selectedContactIds.remove(this.contextItem);
}
/*
* return count of selected items
*/
public Integer getSelectedCount(){
return this.selectedContactIds.size();
}
/*
* advance to next page
*/
public void doNext(){
if(this.setCon.getHasNext())
this.setCon.next();
}
/*
* advance to previous page
*/
public void doPrevious(){
if(this.setCon.getHasPrevious())
this.setCon.previous();
}
/*
* return current page of groups
*/
public List<CCWRowItem> getContacts(){
List<CCWRowItem> rows = new List<CCWRowItem>();
for(sObject r : this.setCon.getRecords()){
Contact c = (Contact)r;
CCWRowItem row = new CCWRowItem(c,false);
if(this.selectedContactIds.contains(c.Id)){
row.IsSelected=true;
}
else{
row.IsSelected=false;
}
rows.add(row);
}
return rows;
}
/*
* return whether previous page exists
*/
public Boolean getHasPrevious(){
return this.setCon.getHasPrevious();
}
/*
* return whether next page exists
*/
public Boolean getHasNext(){
return this.setCon.getHasNext();
}
/*
* return page number
*/
public Integer getPageNumber(){
return this.setCon.getPageNumber();
}
/*
* return total pages
*/
Public Integer getTotalPages(){
Decimal totalSize = this.setCon.getResultSize();
Decimal pageSize = this.setCon.getPageSize();
Decimal pages = totalSize/pageSize;
return (Integer)pages.round(System.RoundingMode.CEILING);
}
/*
* helper class that represents a row
*/
public with sharing class CCWRowItem{
public Contact tContact{get;set;}
public Boolean IsSelected{get;set;}
public CCWRowItem(Contact c, Boolean s){
this.tContact=c;
this.IsSelected=s;
}
}
}
2. VF Page:
<apex:page controller="CCW_ContactPaginationController">
<script type="text/javascript">
/*
* function to handle checkbox selection
*/
function doCheckboxChange(cb,itemId){
if(cb.checked==true){
aSelectItem(itemId);
}
else{
aDeselectItem(itemId);
}
}
</script>
<apex:form >
<!-- handle selected item -->
<apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="mpb">
<apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
</apex:actionFunction>
<!-- handle deselected item -->
<apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}" rerender="mpb">
<apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
</apex:actionFunction>
<apex:pageBlock title="CloudClickware Pagination Sample" id="mpb">
<!-- table of data -->
<apex:pageBlockTable title="Contacts" value="{!contacts}" var="c">
<apex:column >
<apex:facet name="header">Action</apex:facet>
<apex:inputCheckbox value="{!c.IsSelected}" οnchange="doCheckboxChange(this,'{!c.tContact.Id}')"/>
</apex:column>
<apex:column value="{!c.tContact.FirstName}"/>
<apex:column value="{!c.tContact.LastName}"/>
<apex:column value="{!c.tContact.Title}"/>
<apex:column value="{!c.tContact.Phone}"/>
<apex:column value="{!c.tContact.Email}"/>
</apex:pageBlockTable>
<!-- count of selected items -->
<apex:outputLabel value="[{!selectedCount} records selected]" />
<br/>
<!-- next, previous and page info -->
<apex:commandLink action="{!doPrevious}" rendered="{!hasPrevious}" value="Previous" />
<apex:outputLabel rendered="{!NOT(hasPrevious)}" value="Previous" />
<apex:outputLabel value=" (page {!pageNumber} of {!totalPages}) " />
<apex:commandLink action="{!doNext}" rendered="{!hasNext}" value="Next" />
<apex:outputLabel rendered="{!NOT(hasNext)}" value="Next" />
</apex:pageBlock>
</apex:form>
</apex:page>
3. 实现效果: