You need to sign in to do that
Don't have an account?
VF search page that allows you to sort result columns (I have the search function)
Hi, I have search page that I am using and I am trying to tweak the code to allow the columns displaying the results to be sorted (high to low or low to high or A-Z/Z-A.) I know there was a Trailhead that showed it for a SOQL database query, so I tried using that code here but it didn't seem to do anything (I am not an experienced programmer.) If there is an easy way to tweak this code to do this, let me know! Code is below.
Thanks,
Aron
APEX CODE
<apex:page standardController="Product2" extensions="prodsearchcontroller" showheader="false" sidebar="false">
<apex:form >
<apex:inputText value="{!searchstring}" label="Input"/>
<apex:commandButton value="Search records" action="{!search}"/>
<apex:commandButton value="Clear records" action="{!search}"/>
<apex:pageBlock title="Search Result">
<apex:pageblockTable value="{!prod}" var="a">
<apex:column headerValue="Name">
<apex:commandLink action="{! sortByName }"> //ADDED THIS LINE
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Name}
</apex:outputlink>
</apex:commandLink> //ADDED THIS LINE
</apex:column>
<apex:column headerValue="Product Family">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Family}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Product Code">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.ProductCode}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Description">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Description}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Notes">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Notes__c}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Dimensions">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Dimensions__c}
</apex:outputlink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
APEX CLASS
public with sharing class prodsearchcontroller {
private String sortOrder = 'Name';
public list <Product2> Prod {get;set;}
public string searchstring {get;set;}
public prodsearchcontroller(ApexPages.StandardController controller) {
}
public void search(){
string searchquery='select Name, Family, ProductCode, Description, Dimensions__C, Notes__C from Product2 where Name like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Description like \'%'+searchstring+'%\' OR Notes__C like \'%'+searchstring+'%\'';
Prod= Database.query(searchquery);
}
public void clear(){
prod.clear();
}
public void sortByName() { //ADDED THIS LINE
this.sortOrder = 'Name'; //ADDED THIS LINE
}
}
Thanks,
Aron
APEX CODE
<apex:page standardController="Product2" extensions="prodsearchcontroller" showheader="false" sidebar="false">
<apex:form >
<apex:inputText value="{!searchstring}" label="Input"/>
<apex:commandButton value="Search records" action="{!search}"/>
<apex:commandButton value="Clear records" action="{!search}"/>
<apex:pageBlock title="Search Result">
<apex:pageblockTable value="{!prod}" var="a">
<apex:column headerValue="Name">
<apex:commandLink action="{! sortByName }"> //ADDED THIS LINE
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Name}
</apex:outputlink>
</apex:commandLink> //ADDED THIS LINE
</apex:column>
<apex:column headerValue="Product Family">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Family}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Product Code">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.ProductCode}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Description">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Description}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Notes">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Notes__c}
</apex:outputlink>
</apex:column>
<apex:column headerValue="Dimensions">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.Dimensions__c}
</apex:outputlink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
APEX CLASS
public with sharing class prodsearchcontroller {
private String sortOrder = 'Name';
public list <Product2> Prod {get;set;}
public string searchstring {get;set;}
public prodsearchcontroller(ApexPages.StandardController controller) {
}
public void search(){
string searchquery='select Name, Family, ProductCode, Description, Dimensions__C, Notes__C from Product2 where Name like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Description like \'%'+searchstring+'%\' OR Notes__C like \'%'+searchstring+'%\'';
Prod= Database.query(searchquery);
}
public void clear(){
prod.clear();
}
public void sortByName() { //ADDED THIS LINE
this.sortOrder = 'Name'; //ADDED THIS LINE
}
}
In this case what you can do is in the searchquery string whre you need to mention how you wnat to sort it like for example:
Use ascending (ASC) or descending (DESC) order as per your requirement.