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
chiranjeevitgchiranjeevitg 

Pagination for Pageblock table not working properly.

Hi,

 

I have created a search page for opportunity, after entering the search criteria the results will be displayed in Page block table. I have added the pagination for pageblock table. I'm displaying 10 results per page. When i click next link it will populate the next set of 10 records, but when i click previous link i'm seeing all the results in the list.

 

I have enclosed the code here.

 

 

public void search()
    {
	SearchRecVals.Clear();
    	//SortDir = 'asc';
    	List<Opportunity> OldRecs = [SELECT Id, Name, Query__c FROM Opportunity WHERE RecordTypeId =: '012800000007ALV'];
    	
    	try
    	{		
    		 
    		 qryVal = OldRecs[0].Query__c; 
    		 System.debug('Query----------------->>>>>>'+OldRecs[0].Query__c);
    		 OppRecvals = Database.query(OldRecs[0].Query__c + ' order by ' + sortField + ' ' + sortDir);
    		 //OppRecVals = DataBase.Query(OldRecs[0].Query__c + ' order by ' + sortField + ' ' + sortDir + ' limit 10');
    		 System.Debug('Searched Query--------->'+OldRecs[0].Query__c);  
    		 System.Debug('Size Of Oppvals---------->>>>>>'+OppRecvals.Size());
    		 
             //ListSize = 'Total number of records found: '+String.ValueOf(OppRecvals.Size());
    		 ListSize = String.ValueOf(OppRecvals.Size());
    	//try{	 
    		if(OppRecVals.Size()>10)
    		{
    			for(i=0;i<10;i++)
    			{
    				searchRecVals.add(OppRecVals[i]);
    			}
    		}
    		else
    		{
    			searchRecVals = OppRecvals;
    		}
    	}
    	catch(Exception e)
    	{
    		System.Debug('Exception------>:'+e);
    	}
}

 

 

 

Next Link Code

 

public pageReference next()
    {
    	Integer temp = 0;
    	if(OppRecVals.Size()>10)
    	{  
    		/* i's Current Value is 10 */
    		if(i<OppRecVals.Size())
    		{
    			SearchRecVals.Clear();
    			/* Fetching next 10 records by incrementing i = 10 to 20 */
    			i=i+10;
    			System.Debug('Value Of I---------------------->>>>>>>>>>>>>>>>>'+i);
    			/*so now i=20 so trying to fetchnext 10 records by iteratingb thru loop of j*/
    			for(j=j+10;j<i;j++)
    			{ 
    				/* j=10 now initially*/
    				/*10 is < suppose if total there are 20 records*/
  
    				if(j<OppRecVals.Size())
    				{
    					SearchRecVals.add(OppRecVals[j]);
    					temp++;
    				}
    				else
    				{
    					break;
    					j = j- 10;
    				}
    			}
    			getOppRecVals();
    		}//getOppRecVals();
    	}
    	
    	else
    	{
    		SearchRecVals = OppRecVals;
    		if((temp != 10) && (flag == 0))
    		{
    			j = j-temp + 10;
    			flag = 1;
    			system.debug('temp j:'+j);
    			
    		}
    		
    	}
    	return null;	
    }

 

 

 

Previous Link Code:

 

 

public Pagereference prev()
    {
	if(OppRecVals.Size()>10)
    	{
    		integer firstval,secval;    		
    		integer temp=j/10;
    		//SearchRecVals.Clear();
    		//i=i-10;
    		if(temp>0)
    		{
    			
    			firstval=(temp-1)*10;
    			secval=temp*10;
    			
    		}
    		else
    		{
    			
    			firstval=temp;
    			secval=10;
    		}
    		for(j=firstval;j<secval;j++)
    		{
    			
    			SearchRecVals.add(OppRecVals[j]);
    			if(OppRecVals.Size()==j)
    			{
    				break;
    			}
    			
    		}
    	}
    	return null;
    }

 

 

Please help me to solve this problem.

 

Thank you.

 

sravusravu

Are you using a standard controller or a custom controller?

chiranjeevitgchiranjeevitg
Custom Controller
sravusravu

Can you post the whole apex class and the visualforce page.......

One more suggestion is if you are retrieving data only from Opportunity object then you can use standard controller as Opportunity and in the controller extensions set controller.setPageSize(10); 

IN the visual force page you can directly call the {!Next} and {!Previous} actions using command buttons or links.