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
sfdcElephantsfdcElephant 

Adding Columns Dynamically to Datatable on Button click

Hai friends,

 

   I am trying to add columns to data table dynamically,but i am not getting my requirement please help me for this scenario,

 

  Scenario:-  We need to display all required fields in datatable on page load,after that we need o take input from page,as how many number of columns should add,here if u entered 5 ,then 5 columns should add on button click to datatable,then after if u enetered 5 then from remaining list of fields 5 fields should added to datatable as columns.

 

Please help me

 

Thanks in Advance

sfdc Elephant

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Hi,

 

For your help I created this example of dyanmic columns , I tried to cover yoru req in this

 

please recreate this in your org once and try to figure out the code how I achived it.

 

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>

 

I will be explaning it in more detail on my blog later in the day whenver I will get time. Will update you then. Please ask if you want to understand in above example.

All Answers

Shashikant SharmaShashikant Sharma

Hi,

 

For your help I created this example of dyanmic columns , I tried to cover yoru req in this

 

please recreate this in your org once and try to figure out the code how I achived it.

 

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>

 

I will be explaning it in more detail on my blog later in the day whenver I will get time. Will update you then. Please ask if you want to understand in above example.

This was selected as the best answer
SFBlueFishSFBlueFish

Hai Sharma,

 

 Thank u so much Sharma.......Its Awesome.........But it should be more dynamic.........I think.... Any way ....Great Effort...

 

 

Thank u

 

SfBluefish

Shashikant SharmaShashikant Sharma

Hi, I would request you to mark it as a solution if you find it as answer to your question dynamic no of columns in a pageblock table. You can ask any further question if there is any other issue in your req, the best would be you mark this and start a new thread by creating a new post for further queries.