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
Rajat MahajanRajat Mahajan 

Standard Alphabetic Ordering on VF Page

Hi All,

 

I have a custom Visual force page consisting of pageblock section with some information.

 

I wanted to have alphabetic ordering just the way it is done in the standard object page layouts, for the list i have in this pageblock section

 

Is there a way to do this?

Please help me out.

 

Thanks

Rajat.

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippets as reference:

 

========= VF page ====================
<apex:page>
	<apex:form >
		<apex:pageBlock title="Contact">

			<table>
				<tr>
				<apex:repeat value="{!SerchAlpabet}" var="nn">
					<td><apex:commandLink action="{!display}"  reRender="pg" ><apex:param value="{!nn}" name="sss"  assignTo="{!SearchVar}"/> {!nn}</apex:commandLink></td>
					</apex:repeat>
				</tr>
			</table>
			<apex:outputpanel id="pg">

				<div id="showdata">
					<apex:pageBlockTable value="{!ShowCon}" var="k">

					<apex:column headerValue="Name" value="{!k.name}"/> 
					<apex:column headerValue="Email" value="{!k.email}"/> 
					<apex:column headerValue="Mobile Phone" value="{!k.MobilePhone}"/> 
					<apex:column headerValue="Date of Birth" value="{!k.Birthdate}"/> 
					</apex:pageBlockTable>
					</div>
					<div id="nodata" style = "display:none;">
					<p><b>No record by selected letter.</b></p>
				</div>  
				<script>
					var sze='{!sizee}';
					sz=parseInt(sze);
					//  alert(sz);
					if(sz>0)
					{
						document.getElementById('showdata').style.display="block";
						document.getElementById('nodata').style.display="none";
					}
					else
					{
						document.getElementById('showdata').style.display="none";
						document.getElementById('nodata').style.display="block";
					}
				</script>   
			</apex:outputpanel>

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

</apex:page>

==================Apex Controller ==========



public class PagingDemoWithAlpabets 
{

	public List<String> SerchAlpabet{get;set;}
	public String SearchVar{get;set;}
	public list<contact> con{set;get;}
	public list<contact> ShowCon{set;get;}
	public integer sizee{get;set;}

	public PagingDemoWithAlpabets() 
	{ 
		SerchAlpabet=new List<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','All'};
		con=new List<Contact>();
		con=[SELECT name,email,MobilePhone,Birthdate from contact order by Name];
		SearchVar='All';
		ShowCon=new List<Contact>();
		for(Contact c:con)
		{
			if(SearchVar.equals('All'))
				ShowCon.add(c);
			else 
				if(c.Name.startsWith(SearchVar.toLowerCase())||c.Name.startsWith(SearchVar))
				ShowCon.add(c);
		}
		sizee=ShowCon.size(); 
		
	}

	public PageReference display() 
	{
		system.debug('___________________SearchVar_______________________________'+SearchVar);
		ShowCon.clear();
		for(Contact c:con)
		{
			if(SearchVar.equals('All'))
				ShowCon.add(c);
			else
				if(c.Name.startsWith(SearchVar.toLowerCase())||c.Name.startsWith(SearchVar))
				{
				   ShowCon.add(c);
				   system.debug('_______________if ______C__________________'+c);
				}   
		   
		}
		sizee=ShowCon.size(); 
		return null;
	}
}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

bvramkumarbvramkumar

Hi rajat,

 

Your question is not really clear. If you just want sort a list of items, you can use sort() method on the List in your controller code. Note that sort() works only on primitive data types.

 

Thanks

Vishnu

Andy BoettcherAndy Boettcher

You could also just do an ORDER BY in your initial SOQL statement...

 

-Andy

nagalakshminagalakshmi

hi,

 

How to do the column header sort in visual force page. That is which like sorting in list views... if we click on the column i need to sort the table values. Is it possible please help me..

 

Thanks,

Lakshmi

Andy BoettcherAndy Boettcher

nagalakshmi,

 

I have an article on my blog on how to do this with jQuery - check it out!

 

http://techman97.wordpress.com/2012/03/15/jquery-cloudstock-2012-art-1/

 

 

-Andy

Ajaysingh thakurAjaysingh thakur
Hi @Navatar_DbSup ,
How can i add the pagination technique in the above code
 
sachinarorasfsachinarorasf
Hi Rajat,

I have gone through your problem.Please try the below code.
 
VF Page:

<apex:page standardController="Account" recordSetVar="Accountvar" action="{!ViewData}" extensions="AccountListViewController">

<apex:sectionHeader title="My Accounts" subtitle="Account List View"/>
    <apex:form >
        <apex:pageBlock >
          <apex:pageMessages id="error" />

           <apex:panelGrid columns="7" id="buttons" >
           <!---<apex:pageBlockButtons>---->
                <apex:commandButton reRender="error,blocktable,buttons" action="{!Save}" value="Save"/>
                <apex:commandButton reRender="error,blocktable,buttons" action="{!Cancel}" value="Cancel"/>
                <apex:inputHidden />
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasprevious}" action="{!First}" value="First"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasprevious}" action="{!Previous}" value="Previous"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasnext}" action="{!Next}" value="Next"/>
                <apex:commandButton reRender="error,blocktable,buttons" disabled="{!!hasnext}" action="{!Last}" value="Last"/>
           <!---</apex:pageBlockButtons>--->
           </apex:panelGrid>

           <apex:pageBlockSection id="blocktable" >

                <apex:pageBlockTable value="{!AccountsortList}" var="t" rendered="{!NOT(ISNULL(AccountsortList))}" id="cmdsort">

                        <apex:column >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Name{!IF(ExpressionSort=='name',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                            <apex:outputLink value="/{!t.Id}" target="_blank">{!t.Name}</apex:outputLink>
                        </apex:column>


                        <apex:column headerValue="BillingState/Province" value="{!t.BillingState}"/> 
                        <apex:column headerValue="Phone" value="{!t.Phone}"/>
                        <apex:column headerValue="Type" value="{!t.Type}"/>                   
                        <apex:column headerValue="Account Owner Alias" value="{!t.Owner.Name}"/>
                        <apex:column headerValue="Website" value="{!t.Website}"/>

                    <apex:inlineEditSupport event="onClick"/>


                </apex:pageBlockTable>

           </apex:pageBlockSection>   


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

Apex Class:AccountListViewController

public class AccountListViewController{
public List<Account> AccountsortList {get; set;}
public String SortingExpression = 'name';
public String DirectionOfSort = 'ASC';

    public AccountListViewController(ApexPages.StandardSetController controller) {
        AccountsortList = new List<Account>();
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(AccountsortList);
    }

    public String ExpressionSort {
        get {
            return SortingExpression;
        }
        set {
            If(value == SortingExpression) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                SortingExpression = value;
            }
        }

    }

    public String getDirectionOfSort() {
        If(SortingExpression == Null || SortingExpression == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }

    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }

    public List<Account>getAccounts() {
        return AccountsortList;
    }

     public PageReference ViewData() {
        String FullSortExpression = SortingExpression + ' ' + DirectionOfSort;
        system.debug('SortingExpression:::::'+SortingExpression);
        system.debug(DirectionOfSort);

       String Queryitem = ' SELECT Id, Name, Phone, BillingCountry,BillingState, Type, Owner.Name, Website FROM Account WHERE Account.Name != Null ORDER BY ' + FullSortExpression +' Limit 1000';
       system.debug(Queryitem);

        AccountsortList = DataBase.query(Queryitem);
        system.debug(AccountsortList);
        return Null;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora