Skip to main content
added 1 character in body
Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

You can build the SelectOption objects in your controller:

public Id selectedUserId {get; set;}

public List<SelectOption> getUserSelectOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (User u : [Select idId, Name from User order by Name]Name limit 1000]) {
        options.add(new SelectOption(u.Id, u.Name));
    }
    return options;
}

and so the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1">
    <apex:selectOptions value="{!userSelectOptionuserSelectOptions}"/>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

You can build the SelectOption objects in your controller:

public Id selectedUserId {get; set;}

public List<SelectOption> getUserSelectOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (User u : [Select id, Name from User order by Name]) {
        options.add(new SelectOption(u.Id, u.Name));
    }
    return options;
}

and so the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1">
    <apex:selectOptions value="{!userSelectOption}"/>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

You can build the SelectOption objects in your controller:

public Id selectedUserId {get; set;}

public List<SelectOption> getUserSelectOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (User u : [Select Id, Name from User order by Name limit 1000]) {
        options.add(new SelectOption(u.Id, u.Name));
    }
    return options;
}

and so the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1">
    <apex:selectOptions value="{!userSelectOptions}"/>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

added 535 characters in body
Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

As youYou can build the SelectOption objects in your controller property returns a list:

public List<User>Id usersselectedUserId {
 get; set;}

public List<SelectOption> getgetUserSelectOptions() {
    List<SelectOption> options = new returnList<SelectOption>();
    for (User u : [Select id, Name from User order by Name];Name]) {
    }    options.add(new SelectOption(u.Id, u.Name));
    }
 
public Id selectedUserId {get; set;return options;
}

your Visualforce needs to iterate over that list and output an option per entryso the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1"  >size="1">
    <apex:repeatselectOptions value="{!users}" var="u">
        <apex:selectOption itemLabel="{!u.Name}" itemValue="{!u.IduserSelectOption}" />
    </apex:repeat>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

As you controller property returns a list:

public List<User> users {
    get {
        return [Select id, Name from User order by Name];
    }
}
 
public Id selectedUserId {get; set;}

your Visualforce needs to iterate over that list and output an option per entry:

<apex:selectList value="{!selectedUserId}" size="1"  >
    <apex:repeat value="{!users}" var="u">
        <apex:selectOption itemLabel="{!u.Name}" itemValue="{!u.Id}" />
    </apex:repeat>
</apex:selectList>

You can build the SelectOption objects in your controller:

public Id selectedUserId {get; set;}

public List<SelectOption> getUserSelectOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (User u : [Select id, Name from User order by Name]) {
        options.add(new SelectOption(u.Id, u.Name));
    }
    return options;
}

and so the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1">
    <apex:selectOptions value="{!userSelectOption}"/>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

As you controller property returns a list:

public List<User> users {
    get {
        return [Select id, Name from User order by Name];
    }
}

public Id selectedUserId {get; set;}

your Visualforce needs to iterate over that list and output an option per entry:

<apex:selectList value="{!selectedUserId}" size="1"  >
    <apex:repeat value="{!users}" var="u">
        <apex:selectOption itemLabel="{!u.Name}" itemValue="{!u.Id}" />
    </apex:repeat>
</apex:selectList>