Find your content:

Search form

You are here

passing multiple values of same type in apex component

 
Share

I want to pass an array of string to an apex component. I want to something like this

<c:myComponent Status="Pending, Accepeted,..."  />

On the other hand I would like to have something like this

<apex:attribute name="Status" description="This is the value for the component."  type="String[]" required="true"/>

Am I thinking in right direction?


Attribution to: doga

Possible Suggestion/Solution #1

Passing an Array to a Component

Yep you're spot on, the following small example files demonstrate the passing of a string array into the component:

Page Controller - just exposes an array of strings to the page.

public class PageController
{
    public String[] getStrings()
    {
        return new String[]{'This', 'is', 'an', 'array', 'of', 'strings'};
    }
}


Visualforce Page - prints out the array line by line, then includes the component, passing it the array of strings as a parameter.

<apex:page controller="PageController">
    <apex:repeat value="{!Strings}" var="s">
        <h3><apex:outputText value="{!s}"/></h3><br/>
    </apex:repeat>
    <br/>
    <h3>Component:</h3>
    <br/>    
    <c:TestComponent inputStrings="{!Strings}"/>
</apex:page>


Visualforce Component - simply takes one parameter (array of strings), and displays another bound variable on the page:

<apex:component controller="ComponentController">
    <apex:attribute name="inputStrings" type="String[]" required="true" description="" assignTo="{!Strings}"/>
    <apex:outputText value="{!Joined}"/>
</apex:component>


Component Controller - returns the array of strings as a single string of semicolon delimited values:

public class ComponentController 
{
    public String [] strings {get; set;}

    public String getJoined()
    {
        String ret = '';

        for(String s : strings)
        {
            ret += s + ';';
        }

        return ret;
    }
}

Of course, if you want to choose the first option you presented (comma delimited list) just assign it to a string variable in the controller, and then use the split() method to break it back down into it's components.


Using a Component Without a Controller

Taking this a step further, if you don't want to use a controller in the component for some reason, you can still access the parameters passed in, though I not know of a 'nice' way of dealing with an array right now. You can use it as a string and cut it up as below, but there may well be a better way to access the individual elements.

<apex:component>
    <apex:attribute name="inputStrings" type="String[]" required="true" description=""/>

    <ul id="theList"></ul>

    <script type="text/javascript">
        var theStrings = '<apex:outputText value="{!inputStrings}"/>';
        var theListEntries = '';

        theStrings = theStrings.substring(1, theStrings.length - 1).split(', ');

        for(i = 0; i < theStrings.length; i++)
        {
            theListEntries += '<li>' + theStrings[i] + '</li>';
        }
        document.getElementById('theList').innerHTML = theListEntries;

    </script>
</apex:component>


When used in the page shown previously, the output looks like this:

Screenshot of the rendered page & component


Attribution to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4539

My Block Status

My Block Content