+ Start a Discussion
SFBlueFishSFBlueFish 

Adding Columns to Datatable dynamically

Hai friends,

 

       Can you any body help me to add Columns to Datatable on Vfpage Dynamically.I have to display all records in Data table on vfpage. we can add rows dynamically to datatable,but i need to add columns to datatable.Please help me in this scenario.

 

Thank u,

Haribabu

Shashikant SharmaShashikant Sharma

For this you have to use dynamic binding

 

Let me give you structure , you can use it as per your req

 

 
<apex:PageBlockTable columns="{!mapHeader.size}" value="{!listItems}" var="rowItem">
                <apex:repeat value="{!listFieldToShow}" var="colItem">
                    <apex:column >
                        <apex:facet name="header" >{!mapHeader[colItem]}</apex:facet>
                        <apex:outputLabel value="{!rowItem[mapFieldToShow[colItem]]}"></apex:outputLabel>
                    </apex:column>
                </apex:repeat>
 </apex:PageBlockTable>

 mapHeader : It is the map that has Field API Name as Key and Hear for table column for this field

listItems : List of Items that you want to show in table

mapFieldToShow :  Map has Field API Name As Key and Field Api name as Value also, both key and value are field API name

 

Use this will definitly solve your issue. I have used it

 

 

I just created a example :

 

Controller : 

 

public class dynamicColumns {


    public map<String , String> mapFieldToShow{get;set;}
    
    
    public Integer noOfColumns 
        { 
            get; 
            set
                {
                    listFieldToShow = new List<String>(); 
                    if(value == 1)
                        {
                            listFieldToShow.add('FirstName');
                        }
                    else if(value == 2)
                        {
                            listFieldToShow.add('FirstName');
                            listFieldToShow.add('LastName');
                        }
                    else if(value != null && value > 2)
                        {
                            listFieldToShow.add('FirstName');
                            listFieldToShow.add('LastName');
                            listFieldToShow.add('Email');
                        }
                        
                } 
        }
    
    public List<String> listFieldToShow {get;set;}
    
    public dynamicColumns()
        {
            listFieldToShow = new List<String>();
            listFieldToShow.add('FirstName');
            mapFieldToShow = new Map<String , String>();
            mapFieldToShow.put('FirstName' , 'FirstName');
            mapFieldToShow.put('LastName' , 'LastName');
            mapFieldToShow.put('Email' , 'Email');
            listTableResult = [Select FirstName, Lastname, Email from Contact limit 20];
        }
    public List<Contact> listTableResult {get;set;}

    public PageReference refreshTable() 
    {
        return ApexPages.currentpage();
    }

}

 VFP

 

<apex:page controller="dynamicColumns">
 
     <apex:Form>
          <apex:pageBlock>
                <apex:pageBlockButtons location="top">
                    <apex:commandButton action="{!refreshTable}" value="Refresh Table"/>
                </apex:pageBlockButtons>
                
                <apex:pageBlockSection >
                    <apex:pageBlockSectionItem>
                        <apex:outputLabel value="No of Colums">
                        </apex:outputLabel>
                        <apex:inputText value="{!noOfColumns}" />
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
                
                <apex:pageBlockSection collapsible="true" title="Search Result" columns="1">
            
            <apex:PageBlockTable columns="{!listFieldToShow.size}" value="{!listTableResult}" var="rowItem">
               
                <apex:repeat value="{!listFieldToShow}" var="colItem">
                    <apex:column >
                        <apex:facet name="header" >{!mapFieldToShow[colItem]}</apex:facet>
                        <apex:outputLabel value="{!rowItem[mapFieldToShow[colItem]]}"></apex:outputLabel>
                    </apex:column>
                </apex:repeat>
                
            </apex:PageBlockTable>
            
        </apex:pageBlockSection>
          </apex:pageBlock>
     </apex:Form>
</apex:page>

 Give no of colums 1, 2 or 3 and refresh table.

sfdcElephantsfdcElephant

Hai Sharma,

 

  I too working opn same scenario, Can you please provide complete code  for that, i will thankful to u sharma,please

 

 

Thanks in Advance

Elephant

Mitesh SuraMitesh Sura

Shashikant,

 

Thanks for posting this. You said you have used it. Can you please help me? I can display columns, but I cannot get column header to work, it is always blank. 

 

below is snippet of my VF page

 <apex:pageBlock >
   <apex:pageBlockTable value="{!wObjs}" var="wObj"> 
     <!-- Fields -->
     <apex:repeat value="{!wObj.wFields}" var="f">
        <apex:variable value="{!f.name}" var="ColName"/> 
	<apex:column headerValue="TEST">
       	   <apex:inputField value="{!wObj.sObj[f.Name]}" required="true" /> 			        		
	</apex:column>
     </apex:repeat>                     
  </apex:pageBlockTable>
</apex:pageBlock>

I tried with<apex:variable>, <apex:facet>, hard coded "TEST" too, but nothing seems to work. <inputField> works fine though.

I have spent almost 2 days in getting headerValue populated dynamically, now I give up. 

 

Thank you for your time.

 

regards

ISVForce Partner

NatrajNatraj

Hi,

 

This is what i am using for dynamic columns.

 

The column header will display if and only if you have atleast 1 record.

 

<apex:pageBlocktable value="{!Internship}" var="intern">
           
                   <apex:column >
                        <apex:facet name="header">
                            <apex:outputText value="Internship Name"/>
                        </apex:facet>
                        <apex:outputLink value="/{!intern.Id}">{! HTMLENCODE(intern.Name)}</apex:outputLink>

                    </apex:column>
                   
                      <apex:repeat var="f" value="{!$ObjectType.CustomObject__c.FieldSets.FIELDSETNAME}">
    
                          <apex:column value="{!intern[f]}"/>
      
                          </apex:repeat>
                   
                  
            </apex:pageBlocktable>