2

I have a table which contains month wise revenue and goal for a filtered sales rep. What I want to achieve in front end is to create a horizontal table. i.e. months should be in columns and corresponding Revenue and goals should be displayed below them. How can I achieve this through apex (VF page) code?

Here is the snippet of my controller code:

global with sharing class UserStatsController {


public string regn {get; set;}
public string dist {get; set;}    
public string OSR  {get; set;}       

public List<User_Stats__c> Sales_Rep { get; set; }

public UserStatsController() {
    Sales_Rep = getSalesReps();
}

public PageReference save() {
    update Sales_Rep;
    return null;
}


public list <SelectOption> getValuesr()
{
    list <selectoption> lsreg = new List<selectoption> ();
    lsreg.add(new SelectOption('', '--None--'));
    Set<String> regionSet = new Set<String>();
    for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and Region__c != 1 and District__c != null and District__c != '0'])
    {
        String region = string.valueOf(Rgn.Region__c);
        if(!regionSet.contains(region)) lsreg.add(new selectoption(region, region));
        regionSet.add(region);
    }
    return lsreg;
}

public list <SelectOption> getValues()
{
    list <selectoption> ls = new List<selectoption> ();
    ls.add(new SelectOption('', '--None--'));
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    Set<String> distSet = new Set<String>();
    for (User_Stats__C dist : [SELECT District__c FROM User_Stats__c where Region__c =: regnInt and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0])
    {
        if(!distSet.contains(dist.District__c)) ls.add(new selectoption(dist.District__c, dist.District__c));
        distSet.add(dist.District__c);
    }
    return ls;
}

public list <SelectOption> getValuesosr()
{
    list <selectoption> lsosr = new List<selectoption> ();
    lsosr.add(new SelectOption('', '--None--'));
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    Set<String> repSet = new Set<String>();
    for (User_Stats__C srep : [SELECT Sales_Rep__c FROM User_Stats__c where Region__c =: regnInt and District__c =: dist and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0 ORDER BY Sales_Rep__c])
    {
        if(!repSet.contains(srep.Sales_Rep__c)) lsosr.add(new selectoption(srep.Sales_Rep__c, srep.Sales_Rep__c));
        repSet.add(srep.Sales_Rep__c);
    }
    return lsosr;
}

public List<User_Stats__c> getSalesReps(){
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    Sales_rep=[Select Id, Sales_Rep__c, Total_Rental_Revenue_Actual__c, Rental_Revenue_Goal__c From User_Stats__c where Year__c = '2013' and Month__c = '10' and Sales_Rep__c =: OSR and Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Goal__c != null];
    return Sales_Rep;
}


public class RowWrapper
{
 // the values (cells) making up this row
 public List<String> values {get; set;}

 // constructor

        public RowWrapper()
 {
  values=new List<String>();
 }

 // append a value (cell) to the row
 public void addValue(String value)
 {
  values.add(value);
 }
}

public List<User_Stats__c> getRevMonths()
{
 List<User_Stats__C> accs = new List<User_Stats__C>();     

  accs=[Select Id, MONTH__c, Total_Rental_Revenue_Actual__c, Rental_Revenue_Goal__c From User_Stats__c where Year__c = '2013' and Sales_Rep__c =: OSR and Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Goal__c != null];

 return accs;
}

public List<RowWrapper> getRowWrappers()
{
    List<RowWrapper> rows=new List<RowWrapper>();
    for (Integer idx=0; idx<3; idx++)
    {
        rows.add(new RowWrapper());
    }

  // iterate the accounts and populate the rows
  for (Integer idx=0; idx<getRevMonths().size(); idx++)
  {
   rows[0].addValue(getRevMonths()[idx].Month__c);
   rows[1].addValue(string.valueof(getRevMonths()[idx].Total_Rental_Revenue_Actual__c));
   rows[2].addValue(string.valueof(getRevMonths()[idx].Rental_Revenue_Goal__c));
  }

 return rows;
}    

VF table:

<table class="list" border="0" cellpadding="0" cellspacing="0">

<apex:repeat value="{!rowWrappers}" var="row">
   <tr>
     <apex:repeat value="{!row.values}" var="value">
       <td>
         {!value}
       </td>
     </apex:repeat>
   </tr>
</apex:repeat>

Thanks for the help.

3

There's a few ways to do this. The way I favour is to put the data into row/column wrapper classes, where each row contains an entry per month, and then iterate those on the page. I wrote a blog post on this a few years ago with example code:

http://bobbuzzard.blogspot.co.uk/2010/09/rotating-visualforce-table.html

|improve this answer|||||
  • Thanks Bob. I am going through your blog. Will try to take pointers from there and implement in my code. Will post here if I get stuck anywhere. Thanks again. – Robin Jan 17 '14 at 8:33
  • 1
    Hi Bob. I tried to add your code to my controller. I am getting an error on line List<RowWrapper>();. Any thoughts? let me know if I have made a mistake somewhere. – Robin Jan 17 '14 at 8:59
  • Hmm. Looks like some of the code has gone missing in the snippet section - you can download the code and page (including unit test) from: sites.google.com/site/kabsyntaxhigh/files/src.zip I'll fix up the snippet, but having the full file is probably more useful anyway. – Bob Buzzard Jan 17 '14 at 9:07
  • I changed the code. Now it is not giving any error..however, the table is not getting populated. Also, I have removed the headings row in the VF page as I was unsure of how to provide value for that. Any idea why table is not populating? – Robin Jan 17 '14 at 10:42
  • This is because you are declaring accs and rows as method variables which are initialised to empty lists, then checking if they are null before proceeding. They will never be null so the data is never populated. I had them as controller properties so that I cached them. If you want method properties, remove the checks "if (null==accs)" and "if (null==rows)" – Bob Buzzard Jan 17 '14 at 10:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.