In my last post, I showed you how to add “Search” functionality to your related lists. To recap, this is what we built -
In this post, we will be aiming to make this better, and have it look like this :-
Take special note of the two highlighted aspects of this page – the list is showing more columns, has paging built in (next/prev), and also shows you which set of records you are viewing on each page. In the real world, such a basic implementation will have two issues :-
(A) how do I display more columns than just the “name”
(B) how do I implement paging.
To tackle the first problem, you can use the <apex:column> VF construct. This allows you to display a column from the recordset. An example is :-<apex:column value=”{!item.Phone}” />
This displays the “Phone” field for the recordset. Note that the <apex:column> can only be used inside a data set - <apex:dataTable> or <apex:pageBlockTable>.
For the second problem of paging, we need to take a different approach – we will use a standardSetController to define our set of data. The StandardSetController is a special salesforce class that has a lot of the functionality related to displaying sets of data built-in (paging is one of them, others are saving and updating records in mass, allowing multi-select, control over which page of data to display).
So here’s the code for the Controller. Note what each method/property is doing :-
searchContacts – is the actual method that runs the SOQL query to search contacts
searchResults – a List<Contact> list, that contains the records to be displayed
Next(), Previous(), getHasNext(), getHasPrevious(), getPageRangeText(), getShowPageRangeText() – all related to paging
setController – the set controller
- // create the custom controller extension class
- public class Contact_Searcher {
- // Since we are creating an extension to the account standard controller,
- // create an account object to hold the current account
- Account a;
- public Contact_Searcher(ApexPages.StandardController controller)
- {
- // Get the current account, and store it in the account object
- a = (Account) controller.getRecord();
- }
- public string searchValue
- {
- get
- {
- if(searchValue == null)
- searchValue = '';
- return searchValue;
- }
- set;
- }
- public List<Contact> searchResults
- {
- get
- {
- return (List<Contact>)setController.getRecords();
- }
- set;
- }
- public Boolean renderTable
- {
- get
- {
- if(renderTable == null)
- return false;
- return renderTable;
- }
- set;
- }
- public void Next()
- {
- setController.Next();
- }
- public void Previous()
- {
- setController.Previous();
- }
- public ApexPages.StandardSetController setController
- {
- get
- {
- if(searchValue == '')
- {
- List<Contact> contactList = new List<Contact>();
- return new ApexPages.StandardSetController(contactList);
- }
- else
- return setController;
- }
- set;
- }
- public Boolean getHasNext()
- {
- if(setController==null)
- return false;
- return setcontroller.getHasNext();
- }
- public Boolean getHasPrevious()
- {
- if(setController==null)
- return false;
- return setcontroller.getHasPrevious();
- }
- public String getPageRangeText()
- {
- if(setController == null)
- return '';
- else
- {
- Integer startP = ((setController.getPageNumber() - 1) * setController.getPageSize()) + 1;
- Integer endP;
- if(setController.getPageNumber()*setController.getPageSize() > setcontroller.getResultSize())
- endP = setcontroller.getResultSize();
- else
- endP = setController.getPageNumber()*setController.getPageSize();
- return 'Showing Records : ' + startP + ' - ' + endP + ' of total ' + setcontroller.getResultSize();
- }
- }
- public Boolean getShowPageRangeText()
- {
- if (setController==null)
- return false;
- if(setcontroller.getResultSize() > 0)
- return true;
- else
- return false;
- }
- public void searchContacts()
- {
- String finalSearchValue = '%' + searchValue + '%';
- List<Contact> contactList = new List<Contact>([select Id, Name, Title, Department, Phone, Email from Contact where Account.Id = :ApexPages.CurrentPage().getParameters().get('Id')
- and Name like :finalSearchValue]);
- setController = new ApexPages.StandardSetController(contactList);
- setController.setPageSize(3);
- renderTable = true;
- }
- }
And here is the Visualforce page :-
- <apex:page standardController="Account" extensions="Contact_Searcher">
- <apex:form >
- <apex:pageBlock title="Contact Search">
- <!--I got the <h2> tag with the CSS name by viewing the source of my salesforce page
- Another neat thing to note is the use of to add a white space -->
- <h2 class="maintitle">Enter Search String </h2>
- <apex:inputText id="searchBox" value="{!searchValue}" />
- <apex:commandButton id="submit" value="Search" action="{!searchContacts}" />
- </apex:pageBlock>
- <apex:pageBlock title="Search Results">
- <apex:pageBlockTable value="{!searchResults}" var="c" rendered="{!renderTable}">
- <apex:column value="{!c.Name}" />
- <apex:column value="{!c.Title}" />
- <apex:column value="{!c.Department}" />
- <apex:column value="{!c.Phone}" headerValue="Phone No." />
- <apex:column value="{!c.Email}" headerValue="Email Address" />
- </apex:pageBlockTable>
- </apex:pageBlock>
- <apex:commandButton id="btnPrev" value="Prev" action="{!Previous}" rendered="{!HasPrevious}" />
- <apex:commandButton id="btnNext" value="Next" action="{!Next}" rendered="{!HasNext}" />
- <br />
- <br />
- <apex:outputLabel id="opPageRangeText" styleClass="brandTertiaryBgr pbSubheader tertiaryPalette" rendered="{!ShowPageRangeText}" value="{!PageRangeText}" />
- </apex:form>
- </apex:page>
Run this code in your instance, and you’ve now got nice functionality on your related lists. Yes, you can re-use the same code for different types of standard and custom objects (some modifications will be required, especially to the SOQL query).
No comments:
Post a Comment