I have following custom quote page and controller but the problem is sorting is not working properly, When i click on column for sorting ascending and descending it will not show me the list with sorted and automatically hidden the list of quote,
Visualforce Page :
<apex:page controller="MyPagingController" tabStyle="Quote">
<apex:sectionHeader title="List of Quotes"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="Search Quote">
<apex:outputLabel style="font-weight:Bold;" value="Quote Name : " />
<apex:inputText value="{!searchQuoteName}" />
<apex:outputLabel style="font-weight:Bold;" value="Quote Number : " />
<apex:inputText value="{!searchQuoteNumber}" />
<br/><br/>
<apex:commandButton value="Search" action="{!Search}" reRender="pageBlock"></apex:commandButton>
</apex:pageBlock>
</apex:form>
<apex:form >
<apex:pageBlock title="List of Quotes" id="pageBlock">
<apex:pageBlockButtons location="top" dir="LTR" >
<apex:commandButton value="Create New Quote" action="/apex/QuoteGroupCreate?editmode=False" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!quotes}" var="c" rendered="{!NOT(ISNULL(quotes))}" rows="{!PageSize}">
<apex:column width="15%">
<apex:facet name="header">
<apex:commandLink action="{!ViewData}" value="Quote Name{!IF(sortExpression=='name',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort">
<apex:param value="name" name="column" assignTo="{!sortExpression}" ></apex:param>
</apex:commandLink>
</apex:facet>
<apex:outputlink value="/apex/QuoteDetail?id={!c.Id}">{!c.name}</apex:outputlink>
</apex:column>
<apex:column width="35%" headerValue="Opportunity Name">
<apex:outputlink value="/{!c.OpportunityId}" target="_blank">{!c.Opportunity.Name}</apex:outputlink>
</apex:column>
<apex:column width="10%" value="{!c.QuoteNumber}">
<apex:facet name="header">
<apex:commandLink action="{!ViewData}" value="Quote Number{!IF(sortExpression=='QuoteNumber',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdNumberSort">
<apex:param value="QuoteNumber" name="column" assignTo="{!sortExpression}" ></apex:param>
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column width="10%" headerValue="Quote Amount" value="{!c.GrandTotal}"/>
<apex:column width="15%" value="{!c.createdDate}">
<apex:facet name="header">
<apex:commandLink action="{!ViewData}" value="Date{!IF(sortExpression=='createdDate',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdCreatedAtSort">
<apex:param value="createdDate" name="column" assignTo="{!sortExpression}" ></apex:param>
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column width="35%" headerValue="Created By">
<apex:outputField value="{!c.createdById}"/>
<!--<apex:outputField value="{!c.createdDate}"/>-->
</apex:column>
<apex:column width="35%" headerValue="Modified By">
<apex:outputField value="{!c.LastModifiedById}"/>
<!--<apex:outputField value="{!c.LastModifiedDate}"/>-->
</apex:column>
<apex:column width="8%" headerValue="View">
<apex:outputlink style="color:blue;" value="/apex/QuoteDetail?id={!c.Id}">View Detail</apex:outputLink>
</apex:column>
<apex:column width="5%" headerValue="Edit">
<apex:outputlink style="color:blue;" value="/apex/QuoteGroupCreate?id={!c.Id}">Edit</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
<div align="right" style="display:{!IF(AND(NOT(ISBLANK(quotes)),quotes.size>0),'block','none')}">
<br/>
<font size="1pt">Page #: <apex:outputLabel value="{!PageNumber}"/> out of <apex:outputLabel value="{!totalPageNumber}"/> </font>
<apex:commandButton value="Previous" action="{!previous}" disabled="{! NOT(hasPrevious)}" reRender="pageBlock" ></apex:commandButton>
<apex:commandButton value="Next" action="{!next}" reRender="pageBlock" disabled="{! NOT(hasNext)}" ></apex:commandButton>
</div>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class :
public class MyPagingController{
public transient List<Quote> quotes{get;private set;}
private String sortDirection = 'DESC';
private String sortExp = 'createdDate';
public final decimal pageSize {get;set;}
public Integer totalPageNumber {get;private set;}
public String searchQuoteName { get; set; }
public String searchQuoteNumber { get; set; }
public ApexPages.StandardSetController setCon{get;set;}
public Boolean hasNext {
get {
return setCon.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return setCon.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return setCon.getPageNumber();
}
set;
}
public void previous() {
setCon.previous();
quotes= (List<quote>)setCon.getRecords();
}
public void next() {
setCon.next();
quotes= (List<quote>)setCon.getRecords();
}
public String sortExpression {
get
{
return sortExp;
}
set
{
if (value == sortExp)
sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
else
sortDirection = 'ASC';
sortExp = value;
}
}
public String getSortDirection() {
if (sortExpression == null || sortExpression == '')
return 'ASC';
else
return sortDirection;
}
public void setSortDirection(String value) {
sortDirection = value;
}
public MyPagingController() {
pageSize = 20;
string queryString = 'Select Id, Name, Opportunity.Name, QuoteNumber, TotalPrice, CreatedDate, GrandTotal, CreatedById, LastModifiedById from Quote';
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize((Integer)pageSize);
quotes= (List<quote>)setCon.getRecords();
totalPageNumber=(Integer) (setCon.getResultSize()/pageSize).round(System.RoundingMode.CEILING);
}
public PageReference ViewData()
{
BindData();
return null;
}
private void BindData()
{
string sortFullExp = sortExpression + ' ' + sortDirection;
string searchQuoteName = searchQuoteName;
String searchQuery = '';
String s = '';
String squote = '';
if(searchQuoteName != null){
s = '\'%' + searchQuoteName + '%\'';
searchQuery = ' WHERE Name LIKE '+s;
}
if(searchQuery != ''){
if(searchQuoteNumber != null){
squote = '\'%' + searchQuoteNumber + '%\'';
searchQuery += ' AND QuoteNumber LIKE '+squote;
}
}else{
if(searchQuoteNumber != null){
squote = '\'%' + searchQuoteNumber + '%\'';
searchQuery = ' WHERE QuoteNumber LIKE '+squote;
}
}
string queryString = 'Select Id, Name, Opportunity.Name, QuoteNumber, TotalPrice, CreatedDate, GrandTotal, CreatedById, LastModifiedById from Quote '+searchQuery +' order by ' +sortFullExp+ ' Limit 10000';
System.debug('query *******' + queryString);
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize((Integer)pageSize);
quotes= (List<quote>)setCon.getRecords();
totalPageNumber=(Integer) (setCon.getResultSize()/pageSize).round(System.RoundingMode.CEILING);
}
public PageReference Search() {
BindData();
return null;
}
}
rerenderin your sort commandLinks.