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
Sam.arjSam.arj 

SelectList setter is not working in a custom component!

I am developing a custom component that has one SelectList and one Command button.

The selectlist is in multi-select mode so an array of string is prepared to capture user selection.

When the button is clicked, an output label will show the values.

 

Here is the code:

 

 

<apex:component controller="zTestComponent" >

<apex:outputPanel id="thepanel">

<apex:selectList value="{!items}" multiselect="true">
<apex:selectOptions value="{!options}"> </apex:selectOptions>
</apex:selectList>
<apex:commandButton value="submit" action="{!submitMethod}" rerender="thepanel"/>
<apex:outputLabel value="{!message}"></apex:outputLabel>

</apex:outputPanel>

</apex:component>

public class zTestComponent {

public List<SelectOption> options
{
get
{
List<SelectOption> op = new List<SelectOption>();
op.add(new SelectOption('1','test 1'));
op.add(new SelectOption('2','test 2'));
return op;
}
}

public List<string> items
{
get;
set;
}

public string message { get; set; }

public PageReference submitMethod() {
message = '';


if (items != null)
for(string item : items)
{
message += item + ',';
}

return null;
}
}

 

 When the button is clicked, the items variable is always null?!  Any one knows why?

 

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

We actually very recently discovered a bug with multi selectList and apex properties.  If you replace your items property with this:

 

public List<string> items = new List<String>(); public List<String> getItems() { return items; } public void setItems(List<String> items) { this.items = items; }

 

It should work.  Sorry about that. 

 

 

All Answers

jwetzlerjwetzler

We actually very recently discovered a bug with multi selectList and apex properties.  If you replace your items property with this:

 

public List<string> items = new List<String>(); public List<String> getItems() { return items; } public void setItems(List<String> items) { this.items = items; }

 

It should work.  Sorry about that. 

 

 

This was selected as the best answer
Sam.arjSam.arj

Thanks!

 

Another bug is if you set the size attribute via component attribute is generates an error in the design time, but when you add the component to VP page, everything is fine.

 

 

<apex:component controller="con">
<apex:attribute name="rows" type="integer" default="5" description=""/>

<apex:selectList id="LeftValues" multiselect="true" value="{!items}" style="width:100%" size="{!rows}">
<apex:selectOptions value="{!options}"></apex:selectOptions>
</apex:selectList>

</apex:component>

 

 

 

cinov8cinov8
What about using a selectList within a pageBlockTable? I have a list of rows where the user can select a value from a picklist in each row.  How do I get/set the selected picklist value for each row?
cinov8cinov8
p.s. and this is not a multi-picklist, each row only accepts one picklist value.