1

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 #:&nbsp;<apex:outputLabel value="{!PageNumber}"/>&nbsp;out of&nbsp;<apex:outputLabel value="{!totalPageNumber}"/>&nbsp;&nbsp;&nbsp;&nbsp;</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;
    }
}
  • You need to include rerender in your sort commandLinks. – David Cheng Sep 15 '17 at 14:44
0

I did some modification on your code. Problem your facing Because of the <apex:commandLink> was not rendering the page-block inside <apex:facet /> tag. I have replace it with normal html tag <a> , Onclick <a> i am calling action function.

Visual-force page:

<apex:page controller="MyPagingController" tabStyle="Quote"> 
    <apex:sectionHeader title="List of Quotes"></apex:sectionHeader> 
    <apex:form >                
        <apex:actionFunction name="actionFunc" action="{!sortData}" reRender="pageBlock" >
                 <apex:param value="" name="expr" />      
        </apex:actionFunction>

        <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: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"> 
                         <a onclick="actionFunc('Name');false;">Quote Name{!IF(sortExpression=='Name',IF(sortDirection='ASC','▼','▲'),'')}</a>
                    </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"> 
                        <a onclick="actionFunc('QuoteNumber');false;">Quote Number{!IF(sortExpression=='QuoteNumber',IF(sortDirection='ASC','▼','▲'),'')}</a>
                    </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"> 
                           <a onclick="actionFunc('createdDate');false;">Date{!IF(sortExpression=='createdDate',IF(sortDirection='ASC','▼','▲'),'')}</a>
                    </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 #:&nbsp;<apex:outputLabel value="{!PageNumber}"/>&nbsp;out of&nbsp;<apex:outputLabel value="{!totalPageNumber}"/>&nbsp;&nbsp;&nbsp;&nbsp;</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>

Controller :

public class MyPagingController{
    public transient List<Quote> quotes{get;private set;}    
    public String sortDirection { get; set;}
    public String sortExpression {get;set;}
    public decimal pageSize {get;set;} // Modified by Nail David because pageSize is unknown property for visualforce page
    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 void genrateResult(String 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);
        system.debug('******** quotes size' + quotes.size());
    }

    public MyPagingController()  {
        pageSize = 20; // Added by Nail David
        sortDirection ='ASC';   
        sortExpression ='createdDate';
        String sortFullExp = sortExpression  + ' ' + sortDirection;    
        string queryString = 'Select Id, Name, Opportunity.Name, QuoteNumber, TotalPrice, CreatedDate, GrandTotal, CreatedById, LastModifiedById from Quote order by ' +sortFullExp+ ' Limit 10000';
        genrateResult(queryString);
    }

    public void sortData(){        
        String parameterValue = ApexPages.currentPage().getParameters().get('expr');
        system.debug('parameterValue ****************'+ parameterValue);

            if( sortExpression.equalsIgnoreCase(parameterValue.trim()) && sortDirection.endsWithIgnoreCase('ASC')){
                sortDirection='DESC';
            }
            else {
                sortExpression=parameterValue.trim();
                sortDirection='ASC';
            }
        viewData();
    }

    private void viewData()
    {
        String searchQuery = '';        
        String sortFullExp = sortExpression  + ' ' + sortDirection;            
        if(string.isNotBlank(searchQuoteName)){
            String tempStr = '\'%' + searchQuoteName + '%\''; 
            searchQuery = ' AND Name LIKE '+ tempStr; 
        }

        if(string.isNotBlank(searchQuoteNumber)){
            String tempStr = '\'%' + searchQuoteNumber + '%\''; 
            searchQuery += ' AND QuoteNumber LIKE '+ tempStr;            
        }

        if(String.isNotBlank(searchQuery)) {
            searchQuery= ' Where ' + searchQuery.substring(4,searchQuery.length());
        }

        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);
        genrateResult(queryString);       
    }

    public void search(){
        sortDirection ='ASC';   
        sortExpression ='createdDate';
        viewData();
    }
}
  • @nail-david let me know if you face any issue . – NITHESH K Sep 18 '17 at 6:54
  • Thanks Nithesh, when i create a new quote and then fo this list page it does not comes up with first page but anyways i will do it myself. thank you for your kind help. :) – Nail David Sep 19 '17 at 6:00
  • @NailDavid Your welcome.. – NITHESH K Sep 19 '17 at 6:34
  • Done, i have 'DESC' in place of ASC in constructor. is that correct way to view quote which created recently ? – Nail David Sep 19 '17 at 7:45
  • @NailDavid Its always depend on the requirement , but when comes to date people refer new data to see first. – NITHESH K Sep 19 '17 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.