You need to sign in to do that
Don't have an account?
JosephJ
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>
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>
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
Controller
All Answers
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.
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 ?
I referred to https://developer.salesforce.com/forums?id=906F000000098QYIAY for Pagination and i simpley merged this with my Search functionality.
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
Controller