We have client requirement to add columns dynamically to Page block table,Based on user input,Number of columns to be added,we have to add that columns to table.We tried lot,and my community friend sharma also helped me in this scenario.I will explain this step by step,Actually with dynamic way we can't prioritize columns to add table.That is if you give 5 as input any 5 columns are adding to Table,to avoid that problem we need to place, field name, object Name, field value in custom settings.if user input is 5,then first 5 columns are added in this way.
1 . Creating Custom Settings.
Create custom settings and name it as "Custom fields" with fields fields FieldName,FieldValue,Object Name. While creating Custom setting ,Select Settings type as "List" and Visibility as "public".Create records in to custom settings like
Lastname, 1,Contact
phone,2,Contact
Email,3,Contact
Department,4,Contact.
2.Here Basic concept is dynamic binding the fields.
Here i did, first i query the fields list from custom settings, using describe object methods getting fields labels in to Map(o display headers for Table),Based on user input binding that number of fields from custom settings.
Controller:-
public class Demo {
public map<String , String> mapFieldToShow{get;set;}
public map<String , String> mapFieldToShow1{get;set;}
list<String> lstfield=new list<String>();
list<String> lstflabel=new list<String>();
public Integer noOfColumns // User input
{
get;
set
{
listFieldToShow = new List<String>();
// listFieldToShow.addAll(lstField);
for(integer i=0;i<value;i++) //value is user input.
listFieldToShow.add(lstFlabel[i]); // we are binding that number of fields.
}
}
public List<String> listFieldToShow {get;set;}
List<String> fieldLabels=new List<String>();
public Demo()
{
Contactfields__c objp=[select objectName__c from Contactfields__c limit 1];
SObjectType objToken = Schema.getGlobalDescribe().get(objp.objectName__c);
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fieldmap = objDef.fields.getMap();
for(Contactfields__c obj:[select fieldName__c from Contactfields__c order by Fieldvalue__c]) // query from custom settings.
lstfield.add(obj.fieldName__c);
listFieldToShow = new List<String>();
mapFieldToShow = new Map<String , String>();
mapFieldToShow1 = new Map<String , String>();
for(integer i=0;i<lstfield.size();i++){
SObjectField fieldToken = fieldmap.get(lstfield[i]);
DescribeFieldResult selectedField = fieldToken.getDescribe();
lstflabel.add(selectedfield.getLabel());
mapFieldToShow.put(selectedfield.getLabel() ,selectedfield.getLocalName());
mapFieldToShow1.put(selectedfield.getLabel() ,selectedfield.getLabel());
}
String squery='select '; // Building Dynamic Query.
for(integer i=0;i<lstfield.size()-1;i++)
squery=squery+''+lstfield[i]+',';
squery=squery+''+lstfield[lstfield.size()-1];
squery=squery +' '+'from Contact limit 20';
System.debug('Squeryyyyyyyy'+squery);
listTableResult =Database.query(squery);
}
public List<Contact> listTableResult {
get;set;
}
public PageReference Add()
{
return ApexPages.currentpage();
}
public List<Contact> getlistTableResult() {
return listTableResult;
}
public void setlistTableResult(List<Contact> Contacts) {
listTableResult = Contacts;
}
public PageReference save() { // to save updates.
upsert listTableResult;
return null;
}
}
Page:-
1 . Creating Custom Settings.
Create custom settings and name it as "Custom fields" with fields fields FieldName,FieldValue,Object Name. While creating Custom setting ,Select Settings type as "List" and Visibility as "public".Create records in to custom settings like
Lastname, 1,Contact
phone,2,Contact
Email,3,Contact
Department,4,Contact.
2.Here Basic concept is dynamic binding the fields.
Here i did, first i query the fields list from custom settings, using describe object methods getting fields labels in to Map(o display headers for Table),Based on user input binding that number of fields from custom settings.
Controller:-
public class Demo {
public map<String , String> mapFieldToShow{get;set;}
public map<String , String> mapFieldToShow1{get;set;}
list<String> lstfield=new list<String>();
list<String> lstflabel=new list<String>();
public Integer noOfColumns // User input
{
get;
set
{
listFieldToShow = new List<String>();
// listFieldToShow.addAll(lstField);
for(integer i=0;i<value;i++) //value is user input.
listFieldToShow.add(lstFlabel[i]); // we are binding that number of fields.
}
}
public List<String> listFieldToShow {get;set;}
List<String> fieldLabels=new List<String>();
public Demo()
{
Contactfields__c objp=[select objectName__c from Contactfields__c limit 1];
SObjectType objToken = Schema.getGlobalDescribe().get(objp.objectName__c);
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fieldmap = objDef.fields.getMap();
for(Contactfields__c obj:[select fieldName__c from Contactfields__c order by Fieldvalue__c]) // query from custom settings.
lstfield.add(obj.fieldName__c);
listFieldToShow = new List<String>();
mapFieldToShow = new Map<String , String>();
mapFieldToShow1 = new Map<String , String>();
for(integer i=0;i<lstfield.size();i++){
SObjectField fieldToken = fieldmap.get(lstfield[i]);
DescribeFieldResult selectedField = fieldToken.getDescribe();
lstflabel.add(selectedfield.getLabel());
mapFieldToShow.put(selectedfield.getLabel() ,selectedfield.getLocalName());
mapFieldToShow1.put(selectedfield.getLabel() ,selectedfield.getLabel());
}
String squery='select '; // Building Dynamic Query.
for(integer i=0;i<lstfield.size()-1;i++)
squery=squery+''+lstfield[i]+',';
squery=squery+''+lstfield[lstfield.size()-1];
squery=squery +' '+'from Contact limit 20';
System.debug('Squeryyyyyyyy'+squery);
listTableResult =Database.query(squery);
}
public List<Contact> listTableResult {
get;set;
}
public PageReference Add()
{
return ApexPages.currentpage();
}
public List<Contact> getlistTableResult() {
return listTableResult;
}
public void setlistTableResult(List<Contact> Contacts) {
listTableResult = Contacts;
}
public PageReference save() { // to save updates.
upsert listTableResult;
return null;
}
}
Page:-
<apex:page controller="Demo" tabStyle="Contact">
<apex:Form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!Add}" value="Add"/>
<apex:commandButton action="{!save}" value="save"/>
</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="Adding Columns Dynamically" columns="1">
<apex:PageBlockTable columns="{!listFieldToShow.size}" value="{!listTableResult}" var="rowItem">
<apex:repeat value="{!listFieldToShow}" var="colItem">
<apex:column >
<apex:facet name="header" >{!mapFieldToShow1[colItem]}</apex:facet>
<apex:inputField value="{!rowItem[mapFieldToShow[colItem]]}"></apex:inputField>
</apex:column>
</apex:repeat>
</apex:PageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:Form>
</apex:page>