function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth ManiSiddharth Mani 

Dynamic columns and rows as columns in pageblock or datatable

I have a pageblock table and its the most basic version at that. The requirement is to have dynamic columns based on the values of a particular field value and other columns grouped below this one.For eg. my data is like this:

User-added image

I want the output as:

User-added image

And similarly if there are more accounts/fields, they should be grouped and displayed accordingly. Could someone please guide as to how to proceed on this?
James LoghryJames Loghry
This is a structure, in general, that pageblock tables and data tables don't handle well.  You'll need to get creative with wrapper classes and apex:repeat statements in order to get this particular layout working.
I've been working on an example for you, but it's tough going.  You're better off generating the table, tr, and td tags yourself, or looking into a third party library like datatables.net that may be able to do this sort of grouping for you.

If you're still interested in going the pageBlockTable route, however, this link will come in handy for the dynamic columns:
http://salesforce.stackexchange.com/questions/245/dynamic-number-of-columns-in-a-pageblocktable
Siddharth ManiSiddharth Mani
Hello James. Thanks for the reply. After some research, I was able to get some part of the code working using the Opportunity standard object as a reference. Below is the page and controller for the same:

Controller:
public class OpportunityPageController {
    List<Opportunity> opps = new List<Opportunity>();
    List<RowWrapper> rows = new List<RowWrapper>();
    public Date dateValue{get;set;}
    // retrieves the list of accounts backing the page
    public List<Opportunity> getOpportunities() {
        return [SELECT Id, Name, Account.Name, ExpectedRevenue, CloseDate from Opportunity WHERE OwnerId = :UserInfo.getUserId() AND ExpectedRevenue > 20000 AND StageName != 'Closed'];
    }
    
    public List<Opportunity> getOpps() {
        system.debug('dateValue.....'+dateValue);
        return [SELECT Id, Name, Account.Name, ExpectedRevenue, CloseDate from Opportunity WHERE OwnerId = :UserInfo.getUserId() AND ExpectedRevenue > 20000 AND StageName != 'Closed'
                AND CloseDate = :dateValue];
    }
    

        // retrieves the list of row wrappers
        public List<RowWrapper> getRowWrappers() {
                rows=new List<RowWrapper>();
                rows.add(new RowWrapper());
                system.debug('before loading'+rows);
            
                // iterate the accounts and populate the rows
                for (Integer idx=0; idx<getOpportunities().size(); idx++) {
                    rows[0].addValue(getOpportunities()[idx].CloseDate);
                }
                system.debug('after loading'+rows);
        return rows;
        }
        
        public class RowWrapper {
            public List<Date> values {get; set;}

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

            public void addValue(Date value) {
                values.add(value);
            }
        }
}


VF Page:
<apex:page Controller="OpportunityPageController" sidebar="false" showHeader="false">
  <apex:pageBlock >
  
 <table class="list" border="0" cellpadding="0" cellspacing="0">  
     <apex:repeat value="{!rowWrappers}" var="row">
       <tr>
         <apex:repeat value="{!row.values}" var="value">
           <td>
             <apex:param assignTo="{!dateValue}" value="{!value}" name="dateVal"/>
             {!value}
             <apex:pageBlockTable value="{!Opportunities}" var="row">
                 <apex:column value="{!row.Id}"/>
                 <apex:column value="{!row.Name}"/>
                 <apex:column value="{!row.ExpectedRevenue}"/>
             </apex:pageBlockTable>
           </td>
         </apex:repeat>
       </tr>
    </apex:repeat>
  </table>
   
      <!--<apex:pageBlockTable value="{!Opportunities}" var="row">
          <apex:column value="{!row.Name}"/>
      </apex:pageBlockTable>-->
  </apex:pageBlock>
</apex:page>


The issue I am facing now is that all the columns have the same set of data in them and not filtered by date (of course I understand that I havent filtered anywhere in my query or anything). But am unable to understand as to where I should apply the filter in the code to display only the records of that particular date in the table and not others. All this is using standard objects and controllers, so you can directly copy and use the code to see how its working (Please dont get confused with the names - Code is from my personal org and so everything happed at the spur of the moment :) ).