passing parameters to visualforce component

When we call a visualforce component we generally need to pass parameters to that component to get it working.

Let us see how we can pass parameters to a visualforce component.


In the following example i have demonstrated passing following to the vf component:

1.String
2.List of strings
3.List of records (sObject/any object)

                                                Visualforce component



                                 Controller


The received parameter should be assigned to a similar data type variable. For this use attribute: "assignto"
Its important to mention the data type when you receive your parameter in component. 

<apex:attribute name="InputString" description="Received variable of data type string"  assignTo="{!Received_String}" type="String" required="true"/>


A simple string will be "string", list of strings would be string[] and sObject List would be sObject[].



Note that the received parameters are not available in the constructor hence 1st debug statement in constructor will show null values.However, received parameters can be accessed in a method as demonstrated in the example.



Calling visualforce component in a visualforce page.






                      Example visualforce page



                                 Example controller

5 comments:

  1. How to pass List to the Visualforce Custom Component

    ReplyDelete
  2. I mean how to pass List of record Ids to the Visualforce Component

    ReplyDelete
    Replies
    1. i have demonstrated passing list of strings above, id's can be stored as strings, so you can try in same manner as demonstrated above... if you want to pass list of sobject then mention it as sobject[], eg. account[]

      Delete
    2. Passing list of records is not working though I write the code same like your's. Could you please help me?

      Delete
  3. Hi
    Is it Possible to Pass List from apex batch to apex component??
    Thanks

    ReplyDelete