Dynamically load a custom component in Visualforce page

I have a custom component called MyComponent that I could add to my visualforce page by using <c:MyComponent />. This is done in a static manner i.e. I need to have it in my visualforce page before it is loaded. Is there a way I can dynamically add components based on parameters in the page controller i.e. I want to be able to add <c:MyComponent2 /> if a certain parameter is set in the page controller without first having it in the visualforce page.

Answer

You can define it on your page when the page is developed, then user the rendered attribute to determine if it should be displayed

<apex:outPutPanel id="rerenderme" layout="block">

     <c:MyComponent2  rendered="{!PARAMETERS}"/>

</apexLoutPutPanel>

If not rendered on page load, use rerender="rerenderme" on a component that performs an action to serenader the output panel and display the component if the parameters dictate it to be rendered.

BUT: Components may not be the way to go per your comments below. Try looking into Dynamic Visualforce, it may get you what you need: https://developer.salesforce.com/page/Dynamic_Visualforce_Components

Attribution
Source : Link , Question Author : Yanki Twizzy , Answer Author : Eric

Leave a Comment