0

i need sort by fields are default ascending order.we are get any record four fields into pick list form then once we are change or select fields how to display record list sorting order into below pick list form into same visual force page without using the custom button.Let me know if any idea... I did this way but i need without using custom button

public class ContactsListController 
{
Public List<account> accList{get;set;}
Public string selectedField {get;set;}
 Public ContactsListController(){
  accList = [select name,accountnumber,createddate from account limit 100];
 }
 Public pagereference sortMethod(){
  if(selectedField == 'createddate')
     accList = [select name,accountnumber,createddate  from account where id IN:accList order by createddate ASC limit 100 ];
  else if(selectedField == 'Name')
     accList = [select name,accountnumber,createddate  from account where id IN:accList order by name ASC limit 100 ];
  else if(selectedField == 'AccountNumber')
    accList = [select name,accountnumber,createddate  from account where id IN:accList order by Accountnumber ASC limit 100 ];
 return null;
 }
}

Page

<apex:page controller="ContactsListController">
  <apex:form >
  <apex:pageBlock >
   <apex:panelgrid columns="2">
    <apex:selectList value="{!selectedField }" size="1">
     <apex:selectOption itemValue="createddate" itemLabel="createddate "/>
     <apex:selectOption itemValue="Name" itemLabel="Account Name"/>
     <apex:selectOption itemValue="AnnualNumber" itemLabel="Account Number"/>
     </apex:selectList>
  <apex:commandButton value="Sort Table" action="{!sortMethod}" reRender="pgblcktbl"/> 
   </apex:panelgrid>
  </apex:pageBlock>
  <apex:pageBlock id="pgblcktbl">
    <apex:pageblocktable value="{!accList}" var="rec" >
     <apex:column value="{!rec.createddate}"/>
     <apex:column value="{!rec.Name}"/>
     <apex:column value="{!rec.accountnumber}"/>
   </apex:pageblocktable>
  </apex:pageBlock>
 </apex:form>
</apex:page>
  • Can you include the code you've currently got? What specific part of your code isn't doing what you want? – Nick Cook Oct 12 '16 at 5:39
  • @Nick replace my code let me know any idea – Mahitha Oct 12 '16 at 5:42
  • Are you saying that it works as expected, but you want it to work without having to click the button? That is, it works just by changing the picklist value? – Nick Cook Oct 12 '16 at 5:47
  • @Nick yes i need without having to click button sorting list records order is working in ASC – Mahitha Oct 12 '16 at 5:51
  • Have you tried using the actionRegion on the picklist field? – Nick Cook Oct 12 '16 at 6:12

Your Answer

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

Browse other questions tagged or ask your own question.