Skip to main content
I'm using a datatable in VF and I can normally get it to populate via my controller.  However, I need a dynamic variable in my SOQL to have the end user sort by the choice of headers in the table and I apparently keep passing NULL values to the string.  I'm a rookie with Apex and a novice with VF when it comes to working with Controllers, so I'm trying to figure out how to pass the myString value from VF to the SOQL in my Controller (code listed below).  Ideally I'd like to send this info via a commandlink in the column headers within in the datatable, but I'm fairly open to suggestions.  Thoughts?

Controller:

public class dataTableOli {

public String myString {get;set;}

String myquery =       

            'SELECT id,Opportunity.Product_Name__c,Opportunity.Account.Name,Opportunity.Account.Agency_Names__c,Opportunity.Account.Customer_Number__c,Opportunity.Account.Agency_Customer_ID__c,unitprice,opportunityid,Opportunity.StageName,Opportunity.Name,Opportunity.id,Opportunity.Previous_Expire__c,Opportunity.Previous_Amount__c,Opportunity.Product_Tier__c,Opportunity.Add_Print_Subscription__c,Name ' +

            'FROM OpportunityLineItem ' +

            'WHERE Opportunity.Account.Agency_Names__c=\'001j000000R7b8s\' and CALENDAR_YEAR(Opportunity.Previous_Expire__c) =2017 and CALENDAR_MONTH(Opportunity.Previous_Expire__c) =12  and Opportunity.Previous_Amount__c > 0 '+

            'ORDER BY '+MyString+ ' ASC Limit 10';

            

List<OpportunityLineItem> oli;

    public List<OpportunityLineItem> getoli() {

if(oli==NULL) 

   oli=Database.query(myquery);

      return oli;

}

   }

 
3 Antworten
  1. 9. Juni 2017, 13:02
    Thank you for the response, but I think I wasn't clear on what I was looking for, though we're close.  ASC or DESC isn't the issue, it's passing the column name to sort to the Controller.  In other words, if I have a column header of 'Name' (which is the name of the field), I'd like to be able to click on that header to resort by that field.  Immediately next to Name would be another field name in the column header.  Let's say it's Closedate.  I'd like to be able to click on that field to sort and so on for the remainder of the headers.  Note that I'm not looking for the ability to do multi-level sorts (e.g. Sort by Name first and then sort by Closedate), rather just looking to be able to click on one header in the header row to resort the entire table and clicking on another header will resort the entire table again.  Does that make sense?  

    My issue that I can't seem to pass the field name from VF to my Controller.  It keeps coming up as Null.
0/9000