function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohan Raj 33Mohan Raj 33 

How to provide the alpha navigation bar and sorting function in same data table ?

Hi, to all Here I try to provide the sorting and pagination and alpha navigation bar in a single table,But Unfortunately I don't know to how to do that .
here My problem is 
My alpha navigation bar is not working.
I don't know to how to provide the navigaton bar result in the same table
.(as same used to the sorting)
My Controller is,
public class StandardPaginationSorting {

    // Variables required for Sorting.
    public String soql {get;set;}
    public List <Account> CandidateList1 = New List <Account>();
    public String soqlsort {get;set;}
    public List <Account> CandidateList2 = New List <Account>();
    public List<Account> acc {get; set;}

                // List used in to display the table in VF page.
                public List<Account> getCandidateList() {
                    // Passing the values of list to VF page.
                    return con.getRecords();
                    //all();
                }

                // instantiate the StandardSetController from a query locator
                public StandardPaginationSorting(ApexPages.StandardController controller){
                 con.getRecords();
                 all();
                }
                public ApexPages.StandardSetController con {
                    get {
                                                if(con == null) {
                                                                // String Query to have a list of cases for a respective End-user.
                                                                soql = 'SELECT Name, Website,BillingCountry, Phone, Type, Owner.Name FROM Account';

                                                                // Passing the String array to a list with Selected field sorting.
                                                                CandidateList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir ); 

                                                                // setting values of List in StandardSetController.
                                                                con = new ApexPages.StandardSetController(CandidateList1);

                                                                // sets the number of records in each page set
                                                                con.setPageSize(10);
                                                }
                                                return con;
        }
        set;
    }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
    public void first() {
        con.first();
    }

    // returns the last page of records
    public void last() {
        con.last();
    }

    // returns the previous page of records
    public void previous() {
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }

    // Method for Constructor is used for Test Class.
    public StandardPaginationSorting(){ 
        //all();     
    }

   //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = 'SELECT Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account'; 

                                // Adding String array to a List array
                                CandidateList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable
                                con = new ApexPages.StandardSetController(CandidateList2);

                                // Set Page Size to 5
                                con.setPageSize(10);

    }

    // the current sort direction. defaults to asc
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc';} return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    //the alpha bar navigation filter
    public PageReference ggg() {
        return null;
    }
    public PageReference eee() {
        return Null;
    }
    Public PageReference ddd() {
        return Null;
    }
    Public PageReference ccc() {
        return Null; 
    }
    Public PageReference bbb() {
        return Null;
    }
    
    string x;
    public PageReference fff() {
    x = 'f';
    acc.clear();
    String qry = 'SELECT  Name FROM Account' + 'WHERE Name LIKE \''+x+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string xx;
    public PageReference rrr() {
    xx = 'R';
    acc.clear();
    String qry = 'SELECT  Name FROM Account' + 'WHERE Name LIKE \''+xx+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string z;
    public PageReference mmm() {
    z = 'm';
    acc.clear();
    String qry = 'SELECT  Name FROM Account' + 'WHERE Name LIKE \''+z+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string y;
    public PageReference ooo() {
    y = 'o';
    acc.clear();
    String qry = 'SELECT  Name FROM Account' + 'WHERE Name LIKE \''+y+'%\' ORDER BY Name';
    acc= Database.query(qry);
        return null;
    }
    
    public void all() {
        acc = [SELECT Name FROM Account];
        //con = new ApexPages.StandardSetController(acc);
    }
    
    public void aaa() {
        acc.clear();
        acc = [SELECT Name FROM Account];
       // con = new ApexPages.StandardSetController(acc);
    }
    

}

My page is,
<apex:page standardController="Account" extensions="StandardPaginationSorting" showHeader="false" sidebar="false"> 

    <!-- CSS added to display alternate row colors and Center align Text in PageblockTable -->
    <!--<style type="text/css">
        .oddrow{background-color: #00FFFF; } 
        .evenrow{background-color: #7FFFD4; } 
        .textalign{text-align:center; } 
    </style>--->

    <apex:form id="form">
        <!-- Tabstyle attribute is used to assign the color scheme to the pageblock.Here Candidate Object color scheme is used for the pageblock-->
        
        <apex:pageBlock id="pgblock" tabStyle="Account">
            <!--<apex:pageBlockSection title="Candidate Details -  Page #{!pageNumber}" columns="1" collapsible="false">-->   
                <!-- Rowclasses attribute is used to define different CSS classes for different rows. 
                     Rules attribute is used: borders drawn between cells in the page block table.
                     Title attribute will be used as a help text when a user hovers mouse over the Page Block table.
                     Styleclass, HeaderClass attributes are used to Center align Table Text in Page Block table --->
            <right>
            <apex:toolbar id="toolbar" height="20" style="background-color:White;background-image:none;">
            <apex:toolbarGroup itemSeparator="line">
                <apex:commandLink value="A" Action="{!aaa}"/>
                <apex:commandLink value="B" Action="{!bbb}"/>
                <apex:commandLink value="C" Action="{!ccc}"/>
                <apex:commandLink value="D" Action="{!ddd}"/>
                <apex:commandLink value="E" Action="{!eee}"/>
                <apex:commandLink value="F" Action="{!fff}"/>
                <apex:commandLink value="G" Action="{!ggg}"/>
                <apex:commandLink value="H"/>
                <apex:commandLink value="I"/>
                <apex:commandLink value="J"/>
                <apex:commandLink value="K"/>
                <apex:commandLink value="L"/>
                <apex:commandLink value="M" Action="{!mmm}"/>
                <apex:commandLink value="N"/>
                <apex:commandLink value="O" Action="{!ooo}"/>
                <apex:commandLink value="P"/>
                <apex:commandLink value="Q"/>
                <apex:commandLink value="R" Action="{!rrr}"/>
                <apex:commandLink value="S"/>
                <apex:commandLink value="T"/>
                <apex:commandLink value="U"/>
                <apex:commandLink value="V"/>
                <apex:commandLink value="W"/>
                <apex:commandLink value="X"/>
                <apex:commandLink value="Y"/>
                <apex:commandLink value="Z"/>
                <apex:commandLink value="All" Action="{!all}"/>                
            </apex:toolbarGroup>
        </apex:toolbar>
        </right>
                <apex:outputPanel >
                <apex:pageBlockTable value="{!acc}" var="a">
                <apex:column value="{!a.Name}"/>                                    
                </apex:pageBlockTable>
                </apex:outputPanel>
                
                
                <apex:pageBlockTable value ="{!CandidateList}" var="CadList"  title="Click Column Header for Sorting"  styleclass="textalign" headerClass="textalign" >
                   
                    
                    <apex:column >
                        <apex:facet name="header">
                           <apex:commandLink value="Name" action="{!toggleSort}" rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                           </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Name}"/>
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Phone" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Phone" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Phone}"/>
                    </apex:column>

                  <!--  <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Name" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Email" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Email}"/>
                    </apex:column>-->

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Type" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Type}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="OwnerName" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Owner.Name" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Owner.Name}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Website" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Website" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CadList.Website}"/>
                    </apex:column>
                </apex:pageBlockTable>
            <!---</apex:pageBlockSection>---->

            <apex:panelGrid columns="6">
                <apex:commandLink action="{!first}">First</apex:commandlink>
                <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
                <apex:commandLink action="{!last}">Last</apex:commandlink>
                <apex:inputText value="{!pageNumber}"> PageNumber</apex:inputText> of 4
            </apex:panelGrid>

        </apex:pageBlock>
    </apex:form>
</apex:page>

Please help me to solve this issue and For answer's thanks in advance.
Best Answer chosen by Mohan Raj 33
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

You should strongly consider using the <apex:enhancedList> tag, which does basically all of that for you. It has field based and alphabetic sorting built in.

MarkUp:
<apex:page tabStyle="Account" sidebar="false">
    <apex:enhancedList height="500" type="Account"/>
</apex:page>
Result:

User-added image

Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue which results in helping others who are really in need of it.

Best Regards,
Nagendra.P