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
dgindydgindy 

Select list And Users

I tried to create a selectlist with a controller using output from the user table.
  I get a read only error with user table
 
Controller:
Code:
public LIST<User> getUsers()
    {
        list<User> usr = [select id,firstname,lastname from user];
        return usr;
    }

 
Page:
Code:
<apex:form id="theForm" >
     <apex:selectList id="Id" value="{!Users}" size="1" readonly="true" >
             <apex:selectoption itemValue="{!Users.Id}" itemLabel="{!Users.firstname} {!Users.lastname}"></apex:selectoption>
             </apex:selectList> 
</apex:form>

 
Any ideas?
jwetzlerjwetzler
There are a number of things wrong with the way you are trying to use selectList.  The value attribute that you are setting is returning a list of Users, the doc says it has to be a String (or in the case of a multiselect list, a String[]).   And you only have one selectOption, though I assume you actually want more than one option in your list, in which case you need selectOptions.  You got the readonly error because you don't have a setter for your value attribute, but you would have come across more errors as Id, firstname and lastname are not valid properties on a list of users.

I think this is more what you are trying to do.  The doc for selectList in the component reference is pretty much spot on with what I think you're attempting, so please take a look at it.


Code:
<apex:form id="theForm" >
<apex:selectList id="theSelectList" value="{!selectedUser}" size="1" >
<apex:selectoptions value="{!users}"></apex:selectoptions>
</apex:selectList>
</apex:form>

 
Code:
public String selectedUser {get;set;}

public List<SelectOption> userList;

public List<SelectOption> getUsers() {
if (userList == null) {
List<User> usrs = [select id,firstname,lastname from user];

userList = new List<SelectOption>();
for (User u : usrs) {
userList.add(new SelectOption(u.id, u.firstname + u.lastname));
}
}
return userList;
}

 So when you submit your form, whatever User is selected in your list will have its id stored in selectedUser.



Message Edited by jwetzler on 06-20-2008 03:54 PM
dgindydgindy
I'm trying to sort the list box.  Can't I Access it view list[0].ItemLabel?
mtbclimbermtbclimber
Sure you can order it in the controller. Check the selectoption class reference for the right properties/methods on that first.

Better yet can you just have the database return it ordered how you want it?