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
JosephJJosephJ 

two values in pageblocktable ??

Hi All,

I have a requirement, where pagination and search functionality has to be done.I did it but there are 2 pageblock values as under

<apex:pageblocktable value="{!SearchResults}" /> // for Search functionality and
another value is  {!tasks} // for pagination. I cannot use both the values which will be incorrect way of coding.How can i achieve both the functionalities ?

I have tried a lot but i couldnt find out how to get it. :(  Experts i need your help here to achieve my goal . My code as under . Have given full code for better understanding .


public with sharing class TaskListController{
  
     // Pagination staets
    public PageReference save() {
        return null;
    }
    public PageReference edit() {
        return null;
    }
        private integer counter=0;  //keeps track of the offset
        private integer list_size=3; //sets the page size or number of rows
        public integer total_size; //used to show user the total size of the list
        public string selectedPage{get;set{selectedPage=value;}
    }
     public TaskListController() {
        total_size = [select count() from Task]; //set the total size in the constructor
        selectedPage='0';
    }
    public Task[] getTasks() {
       if (selectedPage != '0') counter = list_size*integer.valueOf(selectedPage)-list_size;
        try {
            //we have to catch query exceptions in case the list is greater than 2000 rows
                Task[] taskList = [select Id,Subject, Status, Description
                                     from Task
                                     order by Id
                                     limit :list_size
                                    offset :counter];                  
                return taskList;
       
        } catch (QueryException e) {                           
                ApexPages.addMessages(e);                  
                return null;
        }       
    }
   
    public Component.Apex.pageBlockButtons getMyCommandButtons() {
       
        //the reRender attribute is a set NOT a string
        Set<string> theSet = new Set<string>();
        theSet.add('myPanel');
        theSet.add('myButtons');
               
        integer totalPages;
        if (math.mod(total_size, list_size) > 0) {
            totalPages = total_size/list_size + 1;
        } else {
            totalPages = (total_size/list_size);
        }
         integer currentPage;       
        if (selectedPage == '0') {
            currentPage = counter/list_size + 1;
        } else {
            currentPage = integer.valueOf(selectedPage);
        }
        Component.Apex.pageBlockButtons pbButtons = new Component.Apex.pageBlockButtons();       
        pbButtons.location = 'top';
        pbButtons.id = 'myPBButtons';
        Component.Apex.outputPanel opPanel = new Component.Apex.outputPanel();
        opPanel.id = 'myButtons';
                               
        //the Previous button will alway be displayed
        Component.Apex.commandButton b1 = new Component.Apex.commandButton();
        b1.expressions.action = '{!Previous}';
        b1.title = 'Previous';
        b1.value = 'Previous';
        b1.expressions.disabled = '{!disablePrevious}';       
        b1.reRender = theSet;
       opPanel.childComponents.add(b1);       
                       
        for (integer i=0;i<totalPages;i++) {
            Component.Apex.commandButton btn = new Component.Apex.commandButton();
           
            if (i+1==1) {
                btn.title = 'First Page';
                btn.value = 'First Page';
                btn.rendered = true;                                       
            } else if (i+1==totalPages) {
                btn.title = 'Last Page';
                btn.value = 'Last Page';
                btn.rendered = true;                           
            } else {
                btn.title = 'Page ' + string.valueOf(i+1) + ' ';
                btn.value = ' ' + string.valueOf(i+1) + ' ';
                btn.rendered = false;            
            }
           if (   (i+1 <= 5 && currentPage < 5)
                || (i+1 >= totalPages-4 && currentPage > totalPages-4)
                || (i+1 >= currentPage-2 && i+1 <= currentPage+2))
            {
                btn.rendered = true;
            }
                                    
            if (i+1==currentPage) {
                btn.disabled = true;
                btn.style = 'color:blue;';
            } 
           btn.onclick = 'queryByPage(\''+string.valueOf(i+1)+'\');return false;';
           opPanel.childComponents.add(btn);
           if (i+1 == 1 || i+1 == totalPages-1) { //put text after page 1 and before last page
                Component.Apex.outputText text = new Component.Apex.outputText();
                text.value = '...';       
                opPanel.childComponents.add(text);
            }
         }
       //the Next button will alway be displayed
        Component.Apex.commandButton b2 = new Component.Apex.commandButton();
        b2.expressions.action = '{!Next}';
        b2.title = 'Next';
        b2.value = 'Next';
        b2.expressions.disabled = '{!disableNext}';       
        b2.reRender = theSet;
        opPanel.childComponents.add(b2);
               
        //add all buttons as children of the outputPanel               
        pbButtons.childComponents.add(opPanel); 
       return pbButtons;

    }   
   
    public PageReference refreshGrid() { //user clicked a page number       
        system.debug('**** ' + selectedPage);
        return null;
    }
    public PageReference Previous() { //user clicked previous button
        selectedPage = '0';
        counter -= list_size;
        return null;
    }
   public PageReference Next() { //user clicked next button
        selectedPage = '0';
        counter += list_size;
        return null;
    }
    public PageReference End() { //user clicked end
        selectedPage = '0';
        counter = total_size - math.mod(total_size, list_size);
        return null;
    }
    public Boolean getDisablePrevious() { //this will disable the previous and beginning buttons
        if (counter>0) return false; else return true;
    }
    public Boolean getDisableNext() { //this will disable the next and end buttons
        if (counter + list_size < total_size) return false; else return true;
    }
    public Integer getTotal_size() {
        return total_size;
    }
    public Integer getPageNumber() {
        return counter/list_size + 1;
    }

    public Integer getTotalPages() {
        if (math.mod(total_size, list_size) > 0) {
            return total_size/list_size + 1;
        } else {
            return (total_size/list_size);
        }
    } // Pagination ends

      // Search functionality starts here
        
       public apexpages.standardController controller{get;set;}
       public Task l;
       public List<Task> searchResults {get; set; }

      public string searchText
      {
       get
       {
         if (searchText==null) searchText = '';
         return searchText;
       }
      set;
       }
     public TaskListController(ApexPages.StandardController controller)
     {
        this.controller = controller;
        this.l = (Task) controller.getRecord();
      }

    public PageReference search()
    {
      if(SearchResults == null)
      {
        SearchResults = new List<Task>();
      }
     else
     {
        SearchResults.Clear();
     }

  String qry ='Select Id, Subject,Status from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status';
  SearchResults = Database.query(qry);
  searchResults.sort();
   //System.debug(SearchResults);
   return null;
    }
   }

  
 <!-- Page -->

 
<apex:page controller="TaskListController">
      <apex:form id="searchForm">
       <apex:PageBlock mode="edit">    
        <apex:actionFunction action="{!refreshGrid}" name="queryByPage" reRender="myPanel,myButtons" >
        <apex:param name="firstParam" assignTo="{!selectedPage}" value="" />
       </apex:actionFunction>
       <apex:dynamicComponent componentValue="{!myCommandButtons}"/>       
       <apex:outputPanel id="myPanel">
       <apex:pageMessages id="theMessages" />
       <apex:pageblockSection id="searchBlockSection">
          <apex:pageBlockSectionItem id="searchBlockSectionItem">
            <apex:outputLabel >Keyword</apex:outputLabel>
    <apex:panelGroup >
    <apex:inputtext id="searchTextBox" value="{!searchText}"> </apex:inputtext>
    <apex:commandButton Id="btnSearch" action="{!Search}" rerender="renderBlock" status="status" title="Search" value="Search">                    </apex:commandButton>
    </apex:panelGroup>
          </apex:pageBlockSectionItem>
    </apex:pageblockSection>
  
    <apex:actionStatus id="status" startText="Searching... please wait..."/>    
    <apex:pageBlocksection id="renderBlock" >
      <apex:pageblocktable value="{!SearchResults}" var="t" rendered="{!NOT(ISNULL(SearchResults))}" align="center">
            <apex:outputLink value="/{!t.Id}">{!t.Subject}</apex:outputLink>
            <apex:column value="{!t.Subject}"/>
            <apex:outputLink value="/{!t.Id}">{!t.Status}</apex:outputLink>
            <apex:column value="{!t.Status}"/>
            <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
      </apex:pageblocktable>   
  </apex:pageBlocksection>
   </apex:outputPanel>
   </apex:pageblock>
  </apex:form>
   <apex:enhancedlist type="Activity" height="800" rowsPerPage="50" customizable="False"/>
</apex:page>
Best Answer chosen by JosephJ
Dev.AshishDev.Ashish
James,

I have come up with following code to accomodate your requirements.
Showing page number and all can be easily done. Please let me know if this helps.

VF Page
<apex:page controller="PagingTasksController">
    <apex:form >
        <apex:pageBlock title="Tasks" id="pgBlock">
            <apex:inputText id="searchBox" value="{!searchText}"/>
            <apex:commandButton value="Search" reRender="pgTable,pgBlock" action="{!findTasks}"/>
            <apex:pageBlockTable value="{!Tasks}" var="tsk" id="pgTable" >
                <apex:column value="{!tsk.Subject}"/>
                <apex:column value="{!tsk.Status}"/>
                <apex:column value="{!tsk.Priority}"/>
                <apex:column value="{!tsk.OwnerId}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller 
public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 5;
    public List<Task> lstTasks;
    public String searchText {get;set;}  
    
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }
 	

 
    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
 		return tasks;
    }
    
    public void findTasks(){
        String qStr2 = 'Select count()  from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
    
    public void  queryTasks(){
        String qStr = 'Select OwnerId, Subject, Status, Priority  from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        tasks = Database.query(qStr);
    }
 
    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }
 
    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }
 
    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }
 
    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }
}


All Answers

Dev.AshishDev.Ashish
Hey James, is there any specific reason you can not do this user standardSetController way?
JosephJJosephJ
StandardSetController is not supported by Task object and hence had to go with Offset .When i did it with StandardSetController , it showed  "List Controllers are supported for Task" error and then then someone had also posted that it doesnt support and hence Offset .:(  Banging my head from last 1 day and still the same.
JosephJJosephJ
How about taking to two pageblocktable under pageblock ?
Dev.AshishDev.Ashish
Sorry James I am not able to understand your question properly. You have not used {!tasks} in your VF. Do you find issue in pagination?
Best place to use offset would in search method itself, increament offset with integer equal to record display limit on page and call it whenever next button is pressed.
I can try again to find issue with your code if you explain in more detail.
JosephJJosephJ
Pagination is working fine as well as Search. My query is when i use {!tasks} search will not work and will display all records based on pagination.

And when i'm using {!SearchResults } (which is already used) then pagination is not working. I want that both the Pagination and Search functionalities to work together, which is not.

Could you please help me with the modification ?
JosephJJosephJ
Hope you understood my question . You can even try in your org to get the modification.
JosephJJosephJ
Any pointers Dev.Ashish ? I'm stucked and not able to move forward.
JosephJJosephJ
I've found that for Pagination there is one query and for Search there is one query . I need to merge this out,any help ? 

I referred to https://developer.salesforce.com/forums?id=906F000000098QYIAY  for Pagination and i simpley merged this with my Search functionality.
Dev.AshishDev.Ashish
James,

I have come up with following code to accomodate your requirements.
Showing page number and all can be easily done. Please let me know if this helps.

VF Page
<apex:page controller="PagingTasksController">
    <apex:form >
        <apex:pageBlock title="Tasks" id="pgBlock">
            <apex:inputText id="searchBox" value="{!searchText}"/>
            <apex:commandButton value="Search" reRender="pgTable,pgBlock" action="{!findTasks}"/>
            <apex:pageBlockTable value="{!Tasks}" var="tsk" id="pgTable" >
                <apex:column value="{!tsk.Subject}"/>
                <apex:column value="{!tsk.Status}"/>
                <apex:column value="{!tsk.Priority}"/>
                <apex:column value="{!tsk.OwnerId}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller 
public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 5;
    public List<Task> lstTasks;
    public String searchText {get;set;}  
    
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }
 	

 
    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
 		return tasks;
    }
    
    public void findTasks(){
        String qStr2 = 'Select count()  from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
    
    public void  queryTasks(){
        String qStr = 'Select OwnerId, Subject, Status, Priority  from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        tasks = Database.query(qStr);
    }
 
    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }
 
    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }
 
    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }
 
    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }
}


This was selected as the best answer
JosephJJosephJ
HI Dev.Ashish, This is working for me . A BIG thanks and Bingo to you..I did inline edit also in it . Really appreciate your knowledge..