You need to sign in to do that
Don't have an account?
sort the records
Hello, everyone!
I have a trouble with sorting in VF page controller. The problem is: the sort doesn't work. The records are sorted in the way i set in constructor. And i need to sort using the selected field from a list
------------------------------VF Page--------------------------------------------
<apex:page standardController="BShop__c" standardStylesheets="true" showHeader="true" sidebar="false" extensions="tablesorting">
<apex:form >
<br><h1>click <apex:commandLink value="HERE" action="https://c.ap1.visual.force.com/apex/searchSheet" /> to pass to the search page</h1></br>
</apex:form>
<apex:form >
<br></br>
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Name</apex:outputLabel>
<apex:inputField id="Name" value="{!BShop__c.Name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >ReleaseDate</apex:outputLabel>
<apex:inputField id="ReleaseDate" value="{!BShop__c.ReleaseDate__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Type</apex:outputLabel>
<apex:inputField id="Type" value="{!BShop__c.Type__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >AdditionDate</apex:outputLabel>
<apex:inputField id="AdditionDate" value="{!BShop__c.AdditionDate__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Price</apex:outputLabel>
<apex:inputField id="Price" value="{!BShop__c.Price__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Available</apex:outputLabel>
<apex:inputField id="Available" value="{!BShop__c.Available__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Total</apex:outputLabel>
<apex:inputField id="Total" value="{!BShop__c.Total__c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!quickSave}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
<apex:form >
<apex:pageBlock >
<apex:selectList value="{!selectedField }" size="1">
<apex:selectOption itemValue="BShop__c.Name" itemLabel="Name"/>
<apex:selectOption itemValue="BShop__c.Type__c" itemLabel="Type"/>
<apex:selectOption itemValue="BShop__c.ReleaseDate__c" itemLabel="The Date of Release"/>
</apex:selectList>
<apex:commandButton value="Sort Table" action="{!sortMethod}"/>
<apex:pageblocktable value="{!tablesort}" var="rec">
<apex:column value="{!rec.Name}"/>
<apex:column value="{!rec.Type__c}"/>
<apex:column value="{!rec.Price__c}"/>
<apex:column value="{!rec.Total__c}"/>
<apex:column value="{!rec.ReleaseDate__c}"/>
<apex:column value="{!rec.AdditionDate__c}"/>
<apex:column value="{!rec.Available__c}"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------apex class-----------------------------
Public with sharing class tablesorting {
public list<BShop__c> tablevar{get;set;}
Public string selectedField {get;set;}
public list<BShop__c> tablesort{get;set;}
public tablesorting(ApexPages.StandardController BShop){
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Name ];
}
public void sortMethod(){
if(selectedField == 'Name')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Name ];
else if(selectedField == 'Type__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Type__c ];
else if(selectedField == 'ReleaseDate__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY ReleaseDate__c ];
}
}
your item values should exatcly match the strings compared in the sortmethod.
they are just strings... they need not be api names.. you will need api names in your query.. just make sure they are both same... you can debug in your method to see this..
apex:selectOption itemValue="BShop__c.Name" itemLabel="Name"/>
<apex:selectOption itemValue="BShop__c.Type__c" itemLabel="Type"/>
<apex:selectOption itemValue="BShop__c.ReleaseDate__c" itemLabel="The Date of Release"/>
public void sortMethod(){
if(selectedField == 'Name')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Name ];
else if(selectedField == 'Type__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Type__c ];
else if(selectedField == 'ReleaseDate__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY ReleaseDate__c ];
}
All Answers
your item values should exatcly match the strings compared in the sortmethod.
they are just strings... they need not be api names.. you will need api names in your query.. just make sure they are both same... you can debug in your method to see this..
apex:selectOption itemValue="BShop__c.Name" itemLabel="Name"/>
<apex:selectOption itemValue="BShop__c.Type__c" itemLabel="Type"/>
<apex:selectOption itemValue="BShop__c.ReleaseDate__c" itemLabel="The Date of Release"/>
public void sortMethod(){
if(selectedField == 'Name')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Name ];
else if(selectedField == 'Type__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY Type__c ];
else if(selectedField == 'ReleaseDate__c')
tablesort = [SELECT Name, Type__c, Price__c, Total__c, ReleaseDate__c, AdditionDate__c, Available__c FROM BShop__c ORDER BY ReleaseDate__c ];
}
Thanks a lot!!!! It works!
And one more question not exactly on this problem.
At my VF page there are two blocks with buttons "save" and "cancel", but in the code there is just one. Why is it so? Can u tell me, please?