function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
B2000B2000 

How Do I Pass String[] Variable in a VF Page Component

How do I pass a list of strings values (not a controller variable) in the VF Page component tag to the controller?
VF Page: <c:testComponent strList="1,2,3" />
Component: <apex:component controller="testCont"> <apex:attribute name="strList" type="String[]" assignTo="{!listStrings}" required="true" description="" />
testCont Controller: public String[] listStrings {get;set;}
I have tried strList="1,2,3", strList="[1,2,3]", strList="{1,2,3}" and also with single quotes around each element.
I get this VF Page error message: Content cannot be displayed: Cannot convert the value of '{!listStrings}' to the expected type.
Thanks
Best Answer chosen by B2000
pconpcon
I've never seen or used the Strings[] before in a component attribute, but you can do the following to get this fake it for your component and still meet your requirement of having the visualforce page call in with a static list

Component
<apex:component controller="TestList">
    <apex:attribute name="strList" type="String" assignTo="{!combinedStrings}"
        required="true" description="" />
    <apex:repeat value="{!strings}" var="s">
        <apex:outputText >++{!s}++</apex:outputText>
    </apex:repeat>
</apex:component>

Component Controller
public class TestList {
    public String combinedStrings {
        get;
        set;
    }
    
    public List<String> strings {
        get {
            if (combinedStrings == null) {
                return new List<String>();
            }
            
            return combinedStrings.split(',');
        }
    }
    
    public TestList() {}
}

Visualforce Page
<apex:page >
    <c:TestList strList="A,2,Q" />
</apex:page>

All Answers

pconpcon
Do you have a requirement that the list has to be passed via plaintext to the component?  Is this something you could do from a list inside your controller?

Controller
public List<String> getStringList() {
    return new List<String>{'1', '2', '3'};
}

VisualForce Page
<c:testComponent strList="{!stringList}" />
B2000B2000

Thanks for your response. Yes I do have a requirement to pass an actual plaintext array to the component.  The values can't be hard coded in the controller as you have suggested.  I either need to pass them via plaintext or is there a way to pass an array using Javascript using a getter variable in the controller without having to use an action such as on click:
Example <script>var someArray = ["1","2","3"];</script>  public String[] someArray {get;set;}
pconpcon
I've never seen or used the Strings[] before in a component attribute, but you can do the following to get this fake it for your component and still meet your requirement of having the visualforce page call in with a static list

Component
<apex:component controller="TestList">
    <apex:attribute name="strList" type="String" assignTo="{!combinedStrings}"
        required="true" description="" />
    <apex:repeat value="{!strings}" var="s">
        <apex:outputText >++{!s}++</apex:outputText>
    </apex:repeat>
</apex:component>

Component Controller
public class TestList {
    public String combinedStrings {
        get;
        set;
    }
    
    public List<String> strings {
        get {
            if (combinedStrings == null) {
                return new List<String>();
            }
            
            return combinedStrings.split(',');
        }
    }
    
    public TestList() {}
}

Visualforce Page
<apex:page >
    <c:TestList strList="A,2,Q" />
</apex:page>
This was selected as the best answer
B2000B2000
Thanks!   I appreciate your time and solution.