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