I have requirement to display component dynamically, I get the component name in list. Using list name I have to render the component, for now I have taken one component (i.e. component1
).
I have render that component inside a Visualforce page. Code is as below. Please advise.
Visualforce Component
<apex:component> <div> <h1>Component 1 </h1> </div> </apex:component>
Visualforce page
<apex:page controller="cntr" > <c:{!compName} /> </apex:page>
Controller
public class cntr { public String compName { get; private set; } public cntr() { compName = "component1" } }
Updated
Considering your answer, i tried Same on component1.
- Keeping “component1” same.
Apex Page
<apex:page controller="comcntr"> <apex:dynamicComponent componentValue="{!createComponent}"/> </apex:page>
controller
public class comcntr { public comcntr() { } public Component.c.component1 getCreateComponent() { Component.c.component1 myDy = new Component.c.component1(); return myDY; } }
this working for me now .
But my requirement is like, inside the controller i have the property called compName it is holding my component name. i wanted render that component.
public class comcntr { public String compName='component1'; public comcntr() { } public Component.c.component1 getCreateComponent() { Component.c.component1 myDy = new Component.c.component1(); return myDY; } }
By taking comName value as component name, i have to return the the component. Can you suggest how can achieve this? My compName is always changes depending on requirement.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I guess you cannot make a component dynamic in Visualforce but you have an option to do it by apex.
Refer- Creating and Displaying Dynamic Components
Dynamic Custom Components
Using custom components dynamically works exactly the same as the
standard Visualforce components. Just change the namespace to that of
the custom component. Your custom components are in the c namespace,
so you can create one dynamically like this:Component.c.MyCustomComponent myDy = new Component.c.MyCustomComponent();As a convenience for your own
components, you can omit the namespace, like so:Component.MyCustomComponent myDy = new Component.MyCustomComponent();If you are using components provided by
a third party in a package, use the namespace of the package provider:Component.TheirName.UsefulComponent usefulC = new Component.TheirName.UsefulComponent();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0