+ Start a Discussion
t.deepthi05t.deepthi05 

show more in vf page by querying only once

Hi,

I have list of records displaying with repeat tag in visualforce page.I want intially to show 5 records and then on button click i want display next 5 records along with the current records in the same  page until i have records in the list.

I have https callouts so i want to implemt the above functionality using java script

how to set the repeat size value to 5 intially and incerment it further using javascript


Ashish_SFDCAshish_SFDC
Hi , 


For this you can add a apex command link for "Show More" at the bottom of Page Block, and on click of that you can perform an action that refreshes the merge field of pageBlockTable with more data as desired , in the Last you can rerender that pageBlockTable.
You can pass the limit in the soql query .when click on the show more link call the action method and increase the limit and again execute the soql query and rerender you pageblock table.

See the thread below,

https://developer.salesforce.com/forums?id=906F0000000986iIAA


Regards,
Ashish
t.deepthi05t.deepthi05
Thanks for the reply.

In above example every time we icrement the limit of the page and each time clicking on the link we would run an soql

My requriment i just want to append new records to the existing page without runing an Soql.
VPROKVPROK
I would create 2 lists for that.
1 list will hold all of the values and the 2nd will hold 5 values to show on the page.
You will need a counter variable to process BIG list into smaller. In this case additional querries will be avoided. 
So, dynamically changing list, a counter, Full list and rerender functionallity for the list is required. 
I've done something simillar, but as an apex component(like a widget) and it worked absolutely fine.
VPROKVPROK
here is the code for this solution:
controller:
public with sharing class MultiPageList {
// properties
public list<Account> smList{get;set;}
public integer counter{get;set;} // page number
public list<Account> bgList{get;set;}

//ivars

//constants
private static final Integer pageSize = 5;

//constructor

public MultiPageList(){
	smList = new list<Account>();
	bgList = [SELECT Id, Name FROM Account];
	counter = 1;
	getPage(counter);
}

public void getPage(integer cnt){
	smList.clear();
	for(integer i=(cnt-1)*pageSize;i<cnt*pageSize;i++){
		if(bgList.size()-1>=i){
			smList.add(bgList.get(i));	
		}
	}

}

public pageReference nextPage(){
	if(bgList.size()>=counter*pageSize-2){
		counter++;
		getPage(counter);
	}
	return null;
}

public pageReference prevPage(){
	if (counter>1){
		counter--;
		getPage(counter);
	}
	return null;
}



}
page:
<apex:page controller="MultiPageList">
<apex:form >
    <apex:dataTable value="{!bgList}" var="acc" id="theBigTable">
         <apex:facet name="caption">Big List</apex:facet> 
        <apex:column >
            <apex:facet name="header">Id</apex:facet>
            <apex:outputText value="{!acc.Id}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">Name</apex:facet>
            <apex:outputText value="{!acc.Name}"/>
        </apex:column>
    </apex:dataTable>
    
    
    
    -----------------------SMALL LIST---------------------
    
        <apex:dataTable value="{!smList}" var="acc" id="theSmallTable">
         <apex:facet name="caption">Small List</apex:facet> 
        <apex:column >
            <apex:facet name="header">Id</apex:facet>
            <apex:outputText value="{!acc.Id}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">Name</apex:facet>
            <apex:outputText value="{!acc.Name}"/>
        </apex:column>
    </apex:dataTable>
    
       <apex:commandButton action="{!prevPage}" value="prevPage" id="prevButton" rerender="theSmallTable"/>
       
          <apex:commandButton action="{!nextPage}" value="nextPage" id="NextButton" rerender="theSmallTable"/>
    


</apex:form>


</apex:page>

mark my response as a best answer if it fits you.

Good luck :)

Ashish_SFDCAshish_SFDC
Hi , 


See the SOQL with pagination and Offset sample in the links below, 

The Joys of SOQL Pagination

https://developer.salesforce.com/blogs/tech-pubs/2012/06/the-joys-of-soql-pagination.html

Pagination on VisualForce Page using multiple soql queries.

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AE85IAG

SOQL Offset

https://developer.salesforce.com/blogs/developer-relations/2012/01/soql-offset-in-spring-12.html


Regards,
Ashish
VPROKVPROK
Ashish, do you think that making a querry to DB each time user clicks nexPage will be better? I would use this approach only if there are very many records in the DB, and even then I wouldn't make querries each time. I would take 1k records(for example) and process them with the solution I mentioned before. Simple logic, that will optimize the code. At least I think so.

What do you think?
No offence, just a discussion.

PS: anyway, I didn't knew about "OFFSET", so thanks! :)
Ashish_SFDCAshish_SFDC
Hi ,


You are right querying everytime wont be a Best Practice.

I would suggest you to use links below the table for next 25 records which will use Offset and get the next 25.

Also, See the below thread,

Show more link on bottom PageBlockTable

<apex:page controller="AddShowmoreInPageBlockTableController">
          <apex:sectionHeader title="Home" subtitle="Account"/>
             <apex:form>
                  <apex:pageBlock title="Recent Accounts" >
                          <apex:pageBlockTable value="{!accts}" var="account" id="table">
                                     <apex:column headerValue="Account Name">{!account.Name}</apex:column>
                                     <apex:column headerValue="Billing City">{!account.BillingCity}</apex:column>
                                     <apex:column headerValue="Phone">{!account.Phone}</apex:column>
                          </apex:pageBlockTable><br/>
                       <apex:commandLink value="Show 25 items" action="{!showmoreAccount}" reRender="table"  />
          <apex:commandLink value="Show 10 items" onclick="window.location.reload();" rendered="{!Enable=='edit2'}"/>
           </apex:pageBlock>
        </apex:form>
</apex:page>


 controller is..........
  public class AddShowmoreInPageBlockTableController {
         public String Enable{get;set;}
         public Account account{get;set;}
         public List<Account> accts{get;set;}
  
               public AddShowmoreInPageBlockTableController() {
                      accts = [select Id,Name,BillingCity,Phone from Account limit 10];
      
               }
           public PageReference showmoreAccount() {
                   accts =  [select Id,Name,BillingCity,Phone from Account limit 25]; 
                   Enable='edit2';
                   return null;
             }
    }

https://developer.salesforce.com/forums?id=906F0000000986iIAA


Regards,
Ashish
Valerijs ProkudinsValerijs Prokudins
Still it is querryin every time. You didn't get the idea, check my code. the logic there is tested and works in completed projects.
You can store quite a big list inside the controller and it will be OK for the view state(I made tests some time before)
We can take around 1k records into the big list, and read by 5 records each time we need to show more. If we get further than 1k records, only then we should create another querry.
t.deepthi05t.deepthi05
Thanks all for Your replies

I just wanted include on thing i have callouts and if i have more than 10 records in vf page it is displaying a callout expection.

That if i calling more than 10 at time

currently i am using standard set controller and using pagination to display the records which is avoiding me the exception.

But now i wanted to replicate pagination with show more option
Ashish_SFDCAshish_SFDC
Hi Deepthi, 


Thats hitting a Governor limit, you have to program to avoid hitting Limit. 

A single Apex transaction can make a maximum of 10 callouts to an HTTP request or an API call.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_timeouts.htm

See below for sample code, 

Apex Code Best Practices

https://developer.salesforce.com/page/Apex_Code_Best_Practices

Salesforce API 10 request limit

http://stackoverflow.com/questions/5859290/salesforce-api-10-request-limit


Regards,
Ashish