1

I have a wrapper class with map inside, which i need to display in a visualforce page in a page block table with column headers. Also i wanted to build an export feature on this table so i should be rendering data in the table in Page Block Table. But i'm facing a challenge with column header in page block table , which is currently not working with map ( i used repeat component). Can anyone suggest me an alternative or a fix for this issue.

I'm attaching the wrapper class and the page block table for a reference.

Wrapper Class:

   public class historyWrapper
    {
        public History__c hs{get;set;}
        public Map<String,String> keyValueMap{get;set;}

        historyWrapper(History__c h)
        {
            hs = h;
            keyValueMap = new map<String,String>();

            for(String s: h.Stage_String__c.Split(';'))
            {
                keyValueMap.put(s.split(':')[0],s.split(':')[1]);        
            }
        }

}

Visual force Page:

<apex:pageBlockTable var="hs" value="{!historyWrp}">

            <apex:repeat var="key" value="{!hs.keyValueMap}">

            <apex:column >
                <apex:facet name="header">{!key}</apex:facet>
                {!hs.keyValueMap[key]}
            </apex:column>                

            </apex:repeat>        

</apex:pageBlockTable>

The variable historyWrp is a list of wrapper class historyWrapper. I hope it will avoid all confusion.

What i'm trying here is every wrapper record will have list of key value pairs(like new:10;Working:20; etc..) that i would need to display as column in visualforce page with value. The final page will be like New|Working as column header and 10|20 as values beneath it.

  • In the apex:column, you defined {!hs.keyValueMap} as variable KEY. And in the column value you put {!hs.keyValueMap[KEY]}. So that's Map[Map]. Is that right? Shouldn't it be just KEY? – sfdcFanBoy Apr 25 '16 at 6:49
  • The way you accessing variables is not clear. Can you set value property of apex:repeat into value="{!keyValueMap}" and check – highfive Apr 25 '16 at 7:00
1

works for me using account object:

public class AccountController {
public AccountWrapper accWrap {get;set;}

public AccountController(ApexPages.StandardController stdController) 
{
    Account a = (Account)stdController.getRecord();

    a.Name = 'new:10;Working:20;';

    accWrap  = new accountWrapper(a);
}

public class AccountWrapper
{
    public Account acc{get;set;}
    public Map<String,String> keyValueMap{get;set;}

    accountWrapper(Account a)
    {
        acc = a;
        keyValueMap = new map<String,String>();

        for(String s: a.Name.Split(';'))
        {
            keyValueMap.put(s.split(':')[0],s.split(':')[1]);        
        }
    }
}
}

VISUALFORCE PAGE

<apex:page standardController="Account" extensions="AccountController" >
<apex:pageblock >
<apex:pageBlockTable var="accWrap" value="{!accWrap}">

        <apex:repeat var="key" value="{!accWrap.keyValueMap}">

        <apex:column >
            <apex:facet name="header">{!key}</apex:facet>
            {!accWrap.keyValueMap[key]}
        </apex:column>                

        </apex:repeat>        

</apex:pageBlockTable>

  • 1
    But when you use with a list of wrapper class to iterate over the page block table the headers will disappear for the repeat component. E.g Instead of public AccountWrapper accWrap, try using public List<AccountWrapper> accWrap. You will notice that header will be missing for the repeat component. – george thomas Apr 27 '16 at 12:24
  • <apex:facet> component is supported under only specific parent components like pageBlockTable. It is not supported under a repeat tag.. – Benjamin Pirih Apr 27 '16 at 16:40

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.