65、salesforce的数据分页

<apex:page controller="PagingController">
    <apex:form >
        <apex:pageBlock title="Invoices">

            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!process}" value="Selected" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>
            <apex:pageMessages />

            <apex:pageBlockSection title="Goods -  Page {!pageNumber}" columns="1">
                <apex:pageBlockTable value="{!categories}" var="c">
                    <apex:column width="25px">
                        <apex:inputCheckbox value="{!c.checked}" />
                    </apex:column>
                    <apex:column value="{!c.invoice.Name}" headerValue="Name" />
                    <apex:column value="{!c.invoice.Invoice_Total1__c}" headerValue="Total" />
                    <apex:column value="{!c.invoice.Status__c}" headerValue="Status" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:panelGrid columns="4">
            <apex:commandLink action="{!first}">First</apex:commandlink>
                <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
                    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
                        <apex:commandLink action="{!last}">Last</apex:commandlink>
        </apex:panelGrid>

    </apex:form>
</apex:page>

Controller的代码

public with sharing class PagingController {
    List<categoryWrapper> categories {get;set;}
    
    //instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con{
        get{
            if(con==null){
                con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,Invoice_Total1__c,Status__c,Id from Invoice__c]));
                con.setPageSize(4);
            }
            return con;
        }
        set;
    }
    
    //returns a list of wrapper objects for the sObjects in the current page set
    public List<categoryWrapper> getCategories(){
        categories = new List<categoryWrapper>();
        for(Invoice__c invoice : (List<Invoice__c>)con.getRecords()){
            categories.add(new CategoryWrapper(invoice));
        }
        return categories;
    }
    
    
    //displays the selected items
    public PageReference process(){
        for(CategoryWrapper cw:categories){
            if(cw.checked){
                 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.invoice.Name));
            }
        }
        return null;
    }
    
    // indicates whether there are more records after the current page set.
     public Boolean hasNext {
         get {
             return con.getHasNext();
         }
         set;
     }
 
     // indicates whether there are more records before the current page set.
     public Boolean hasPrevious {
         get {
             return con.getHasPrevious();
         }
         set;
     }
 
     // returns the page number of the current page set
     public Integer pageNumber {
         get {
             return con.getPageNumber();
         }
         set;
     }
 
     // returns the first page of records
      public void first() {
          con.first();
      }
 
      // returns the last page of records
      public void last() {
          con.last();
      }
 
      // returns the previous page of records
      public void previous() {
          con.previous();
      }
 
      // returns the next page of records
      public void next() {
          con.next();
      }
 
      // returns the PageReference of the original page, if known, or the home page.
      public void cancel() {
          con.cancel();
      }
}

包装类的代码

public class CategoryWrapper {
    public Boolean checked{get;set;}
    
    public Invoice__c invoice{get;set;}
    
    public CategoryWrapper(){
        invoice = new Invoice__c();
        checked = false;
    }
    
    public CategoryWrapper(Invoice__c invoice){
        this.invoice = invoice;
        checked = false;
    }
}

最后实现的效果如下图所示

 

posted @ 2017-02-19 16:35  香港胖仔  阅读(538)  评论(0编辑  收藏  举报