5

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.

3

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

  • Good answer but I don't want to add it myself on the page. I want it to be based on something that is entered in a field that I read in the page controller – Yanki Twizzy Oct 3 '14 at 23:23
  • @YankiTwizzy - I guess I do not understand. You can base the rendering on data entered into a field. If you want to add it after the page is generate in the users browser, then you will have to inject the HTML code in the DOM that would have been placed there by rendering the component. Seems easier just to have the component. - Unless you are asking about placing "Different" components based on the data entered – Eric Oct 3 '14 at 23:28
  • Yes I am asking about placing different components based on data entered. I could have up to 100 components and I want to load them based on the name that the user enters in a field that I access in the page controller – Yanki Twizzy Oct 3 '14 at 23:28
  • I think even with those instructions that this is the only real way to achieve what you want. You'd have to do all the mapping in the controller. – Matt Lacey Oct 4 '14 at 1:30
  • 4
    @YankiTwizzy - Components may not be the way to go then. Try looking into Dynamic Visualforce, it may get you what you need: developer.salesforce.com/page/Dynamic_Visualforce_Components – Eric Oct 4 '14 at 3:24

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.