You need to sign in to do that
Don't have an account?

Dynamically display a Component on Visualforce page.
I am trying to create a VF page on which custom components are displayed which are stored in Custom setting or metadata object. So that I can give controll to end user which component they want to see.
To achieve this I choosed to work with apex:DynamicComponent, I tried different ways but the only i am succesfull when I user {!piechart} for component value.
<apex:page controller="DashboardCTRL" sidebar="false" >
<apex:repeat value="{!count}" var="i" >
<apex:dynamicComponent componentValue="{!cmps[i]}"/>
</apex:repeat>
*********** or ******************
<apex:repeat value="{!cmps}" var="cmp" >
<apex:dynamicComponent componentValue="{!cmp}"/>
</apex:repeat>
*************************************
</apex:page>
********************* Controller ******************************************
public class DashboardCTRL {
public ApexPages.Component piechart { get; set; }
public Map<String,ApexPages.Component> cmps{get;set;}
public List<integer> count{get;set;}
public DashboardCTRL() {
cmps = new Map<String, ApexPages.Component>();
count = new list<integer>();
/* Type t = Type.forName('PieChartCtrl');
System.debug('t is :'+ t);
if(t != null) {
IComponentProvider cprovider = (IComponentProvider)t.newInstance();
nudebt__Dashboard_Settings__c usersettings = nudebt__Dashboard_Settings__c.getInstance(UserInfo.getUserId());
Map<string,Schema.SObjectField> settingFields = Utility.getsObjectFieldMap('nudebt__Dashboard_Settings__c');
List<ApexPages.Component> DashboardList = new List<ApexPages.Component>();
integer i = 0;
for(string k: settingFields.keySet()){
string fname = settingFields.get(k).getDescribe().getName();
System.debug('fieldname '+ fname);
if(fname.startsWith('nudebt__cmp')){
System.debug('component name is'+ usersettings.get(fname));
Type t1 = Type.forName((string)usersettings.get(fname));
System.debug('t is :'+ t1);
if(t1 != null) {
IComponentProvider cprovider = (IComponentProvider)t1.newInstance();
DashboardList.add(cprovider.provideComponent());
this.piechart = cprovider.provideComponent();
cmps.put(String.valueOf(i),this.piechart);
count.add(i);
i++;
}
}
}
}
}
To achieve this I choosed to work with apex:DynamicComponent, I tried different ways but the only i am succesfull when I user {!piechart} for component value.
<apex:page controller="DashboardCTRL" sidebar="false" >
<apex:repeat value="{!count}" var="i" >
<apex:dynamicComponent componentValue="{!cmps[i]}"/>
</apex:repeat>
*********** or ******************
<apex:repeat value="{!cmps}" var="cmp" >
<apex:dynamicComponent componentValue="{!cmp}"/>
</apex:repeat>
*************************************
</apex:page>
********************* Controller ******************************************
public class DashboardCTRL {
public ApexPages.Component piechart { get; set; }
public Map<String,ApexPages.Component> cmps{get;set;}
public List<integer> count{get;set;}
public DashboardCTRL() {
cmps = new Map<String, ApexPages.Component>();
count = new list<integer>();
/* Type t = Type.forName('PieChartCtrl');
System.debug('t is :'+ t);
if(t != null) {
IComponentProvider cprovider = (IComponentProvider)t.newInstance();
nudebt__Dashboard_Settings__c usersettings = nudebt__Dashboard_Settings__c.getInstance(UserInfo.getUserId());
Map<string,Schema.SObjectField> settingFields = Utility.getsObjectFieldMap('nudebt__Dashboard_Settings__c');
List<ApexPages.Component> DashboardList = new List<ApexPages.Component>();
integer i = 0;
for(string k: settingFields.keySet()){
string fname = settingFields.get(k).getDescribe().getName();
System.debug('fieldname '+ fname);
if(fname.startsWith('nudebt__cmp')){
System.debug('component name is'+ usersettings.get(fname));
Type t1 = Type.forName((string)usersettings.get(fname));
System.debug('t is :'+ t1);
if(t1 != null) {
IComponentProvider cprovider = (IComponentProvider)t1.newInstance();
DashboardList.add(cprovider.provideComponent());
this.piechart = cprovider.provideComponent();
cmps.put(String.valueOf(i),this.piechart);
count.add(i);
i++;
}
}
}
}
}
Why you did not use "rendered" attribute of vf standard compoenent like panel or pageblok. Upon selection from use control render relavent vf componenent.

To make it more dynamic , End user will have capability to subscribe to components we publish. We dont want to change this page everytime we publish a new component.