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 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:
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:

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() {
        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++) {
                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) {

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">
         <apex:repeat value="{!row.values}" var="value">
             <apex:param assignTo="{!dateValue}" value="{!value}" name="dateVal"/>
             <apex:pageBlockTable value="{!Opportunities}" var="row">
                 <apex:column value="{!row.Id}"/>
                 <apex:column value="{!row.Name}"/>
                 <apex:column value="{!row.ExpectedRevenue}"/>
      <!--<apex:pageBlockTable value="{!Opportunities}" var="row">
          <apex:column value="{!row.Name}"/>

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 :) ).