Friday, May 3, 2013

Dynamic Columns in Salesforces Visualforce Tables

As we already know that in Production we can not modify the Apex classes and Trigger.So we hate to think always in such way that how to control the apex from the configuration things/From Setup.

Recently we came across a situation where we need to control the number of columns displayed in visual force table.To control the number of columns from the set up we have multiple options available.

Solutions

1.Make use of the Field sets to control the columns in table.

2. Create a custom setting for filed that you want to handle as a Boolean variable to decide whether to add part of the table or not.

Using Custom Settings

1. Create a list custom setting along with 3 fields in it with data type as boolean (In my case I want to handle only this 3 fields for multiple tables)


2. Goto manage and create a one record for one table and select that check box true if you want to show in table vice versa
VisaulForce page
<apex:page controller="DynamicColums">
<apex:form >
<apex:pageBlock mode="edit" title="Configurable DataTable Demo">
  <apex:pageBlockSection >
    Select <apex:selectList value="{!selPicklist}"> 
             <apex:selectOptions value="{!picklistval}" />
    </apex:selectList>
     <apex:pageBlockTable value="{!accounts}" var="account" >
       <apex:column headerValue="Name" rendered="{!showName}">
     <apex:outputField value="{!account.name}"/>
     </apex:column>
     <apex:column headervalue="Type" rendered="{!showType}">
       <apex:outputField value="{!account.type}" />
     </apex:column>
     <apex:column headervalue="Phone" rendered="{!showPhone}">
       <apex:outputField value="{!account.Phone}" />
     </apex:column>
   </apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:
public class DynamicColums {

  public Boolean showPhone {get; set;}
  public Boolean showType {get; set;}
  public Boolean showName {get; set;}
  public String selPicklist{get;set;}

  public List<Account> accounts {get; set;}

  public DynamicColums(){
    //Get reference to the custom setting
    ColumDisplay__c columnSettings = ColumDisplay__c.getInstance('cs2');

    //Set individual variables based on custom setting fields
    showPhone = columnSettings.showPhone__c;
    showType = columnSettings.showType__c;
    showName = columnSettings.showName__c;
    accounts = [SELECT Name, Type, Phone from Account];
    
  }
  
   Public List<SelectOption> getpicklistval()
   {
    List<SelectOption> temp = new List<SelectOption>();
    for(Account a : [SELECT Name, Type, Phone from Account where type!=null] )
    {
     temp.add(new SelectOption(a.type,a.type));
    }
   return temp;
}
}


OutPut:





Thanks for visiting enjoy...!

No comments:

Post a Comment