1

I want that both the Pagination and Search functionalities to work together, which is not working.My query is when i use {!tasks} value in pageblocktable,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 tried a lot but still no joy.Only code snippets shown for Pagination.Experts help needed for any modification

 <apex:page controller="TaskListController">

 <apex:form id="searchForm">
  <apex:PageBlock mode="edit">     
     <apex:actionSupport action="{!refreshGrid}" name="queryByPage" reRender="myPanel,myButtons" >
        <apex:param name="firstParam" assignTo="{!selectedPage}" value="" />
     </apex:actionSupport>
       <apex:dynamicComponent componentValue="{!myCommandButtons}"/>        
       <apex:outputPanel id="myPanel">
       <apex:pageMessages id="theMessages" />
      </apex:outputPanel>
    <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:pageblock>
  </apex:form>
   <apex:enhancedlist type="Activity" height="800" rowsPerPage="50"   customizable="False"/>
  </apex:page>

Apex code:

public with sharing class TaskListController{

 // Pagination 
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 {
         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';

    btn.onclick = 'queryByPage(\''+string.valueOf(i+1)+'\');return false;';
       opPanel.childComponents.add(btn);
      }

    //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;
   }

  // Search functionality 

   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;
   }
  }
4
  • 1
    From a quick look, you seem to have one query for the paging and one for the search. You will need to merge these into a single query that does both things.
    – Keith C
    Commented Jul 17, 2014 at 12:10
  • Not sure how will that work ! I'm struggling.
    – JJoseph
    Commented Jul 17, 2014 at 12:23
  • Then I suggest you keep Googling until you find an example that is very close to your problem and just adapt it. Here is one - not saying its the best - salesforcecode.blogspot.ie/2013/08/….
    – Keith C
    Commented Jul 17, 2014 at 12:30
  • Have a look at here and get the idea - salesforce.stackexchange.com/questions/30351/…
    – highfive
    Commented Jul 17, 2014 at 14:13

1 Answer 1

2

Use the advice from comments. You have to merge both queries into one. I even don't see any use of {!tasks} in code of your page.

public Task[] getTasks() {
   if (selectedPage != '0') counter = list_size*integer.valueOf(selectedPage)-list_size;
    try {
         Task[] taskList = [select Id,Subject, Status, Description from Task 
where Subject like '%' :searchText '%' OR Status like '%' searchText '%' Order By Subject,Status limit :list_size offset :counter];                  
          return taskList;
        } catch (QueryException e) {                           
            ApexPages.addMessages(e);                  
            return null;
    }       
}

and use {!tasks} instead of {!SearchResults }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .