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
Reddy@SFDCReddy@SFDC 

How to retrieve only 5 records per page

Hi All, i am new to VisualForce so please help me.... here are my page and controller::::::::::

 

public class AccountSearchPGController {
  String searchText;
  List<Account> results;
  public String getSearchText() {
  return searchText;
  }
  public void setSearchText(String s) {
  searchText = s;
  }
  public List<Account> getResults() {
  return results;
  }
  public PageReference doSearch() {
  //results = (List<Account>)[FIND :searchText RETURNING Account(Name, Type, Phone)][0];
  results = [SELECT name,type,phone FROM Account];
  return null;
  }

}

////////////////////pagee///////////////////////////////////

 

<apex:page controller="AccountSearchPGController">
   <apex:form >
   <apex:pageBlock mode="edit" id="block">
   <apex:pageBlockSection title="Account Search Page">
     <apex:pageblockSectionItem >
     <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
        <apex:panelGroup >
         <apex:inputText id="searchText" value="{!searchText}"/>
         <apex:commandButton value="Search" action="{!doSearch}" rerender="block" status="status"/>
         </apex:panelGroup>   
     </apex:pageblockSectionItem>     
  </apex:pageblockSection>
  <apex:actionStatus id="status" startText="requesting..."/>
  <apex:pageBlockSection title="Results" id="results" columns="1">
    <apex:pageBlockTable rows="5" value="{!results}" var="l" rendered="{!NOT(ISNULL(results))}">
     <apex:column value="{!l.name}"/>
     <apex:column value="{!l.type}"/>
     <apex:column value="{!l.phone}"/>
    </apex:pageBlockTable>
</apex:pageBlockSection>

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

Navatar_DbSupNavatar_DbSup

Hi,

 

You need to use paging to work around on this. You have to create a custom function to implement the 5 records per page. You have to create a first, next option so that you can set 5 records per page. Initially set the flag variable to 5 and process the business logic according to that.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Nilesh ManeNilesh Mane

Try below query, it will return only 5 records.

 results = [SELECT name,type,phone FROM Account limit 5];

 

Is this helpful?

Prafull G.Prafull G.

You can use standardset methods for the paginations. You can set recordsize per page.

 

visit the below link for more details

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardsetcontroller.htm

 

Regards,

Chamil MadusankaChamil Madusanka

I have an example for pagnation and it can be set page size to 5. Here it is.

 

<apex:page controller="pagingControllerForUser">
    <apex:form >
        <apex:pageBlock >
            
            <apex:pageMessages id="pgm"/>
            
            <apex:pageBlockButtons >
                <apex:commandButton value="Search" action="{!Search}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="User Name"/>
                    <apex:inputText value="{!usr}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>

        </apex:pageBlock>

        <apex:pageBlock rendered="{!IF(AllSearchUsers.size > 0 , true , false)}">

            <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav2">
              Total Records Found: <apex:outputText rendered="{!IF(Con.resultSize==10000,true,false)}">10000 +</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!Con.resultSize}</apex:outputText>
                  <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>
                  <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>
                  <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>         
                  &nbsp;({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)})&nbsp;
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>         
                  <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;
                  <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>
                  <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>          
              </apex:outputPanel>
            
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!AllSearchUsers}" var="UR">
                    <apex:column headerValue="Name" value="{!UR.Name}"/>                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav">
              Total Records Found: <apex:outputText rendered="{!IF(Con.resultSize==10000,true,false)}">10000 +</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!Con.resultSize}</apex:outputText>
                  <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>
                  <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>
                  <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>         
                  &nbsp;({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)})&nbsp;
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>         
                  <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;
                  <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>
                  <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>          
              </apex:outputPanel>

        </apex:pageBlock>

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

 

public class pagingControllerForUser
{
    public List<User> AllSearchUsers
    {
        get
        {
            if(con != null)
                return (List<User>)con.getRecords();
            else
                return null ;
        }
        set;}
    
   // public User usr {get; set;}
    public String usr {get; set;}
    
    //Controller
    public pagingControllerForUser()
    {
        AllSearchUsers = new List<User>() ;
        //usr = new User() ;
    }
    
     public PageReference Search()
    {   
        if(usr != null)
        {
            con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id , name from User where name LIKE :('%'+usr+'%')]));
 
            // sets the number of records in each page set
            con.setPageSize(5);
        }
        else
        {
            con = null;
        }
        return null ;
    }
    
    //Instantiate the StandardSetController
    public ApexPages.StandardSetController con{get; set;}
    
    //Boolean to check if there are more records after the present displaying records
    public Boolean hasNext
    {
        get
        {
            return con.getHasNext();
        }
        set;
    }
 
    //Boolean to check if there are more records before the present displaying records
    public Boolean hasPrevious
    {
        get
        {
            return con.getHasPrevious();
        }
        set;
    }
 
    //Page number of the current displaying records
    public Integer pageNumber
    {
        get
        {
            return con.getPageNumber();
        }
        set;
    }

    //Returns the previous page of records
    public void previous()
    {
        con.previous();
    }
 
    //Returns the next page of records
    public void next()
    {
        con.next();
    }
}

 Just copy and paste. feel the pagination :)

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Ankit AroraAnkit Arora

Smart move Chamil :)

 

Here is the source :D

 

http://forceguru.blogspot.com/2011/04/pagination-in-salesforce.html

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Chamil MadusankaChamil Madusanka

Sorry Ankith. I couldn't find the link for that code and I didn't remember the code is from your blog. But I have this example once for my requirement. So I posted modified code for this post. I wanted to help this guy with good solution.