0

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.

3

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();
  • i had gone through this link before. i had tried too. its working when i know exact name of component at the beginning, let update question on your answer @rahul – NITHESH K Oct 12 '17 at 14:15
  • Lets say even if its possible to do this in Visualforce page, you would still need to know the component name in advance; otherwise you would have run time exception. – Raul Oct 12 '17 at 14:24
  • please find updated question . – NITHESH K Oct 12 '17 at 14:26
  • my project requirement is like that. menu generated dynamically . in feature menu can be added, so when new menu is attached new component is created related to menu. menu name and component name is always same in my requirement. when menu is clicked by taking menu name as component name, i have to render component based on menu click . – NITHESH K Oct 12 '17 at 14:28
  • 1
    Refer this answer – Raul Oct 12 '17 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.