Is it possible to preselect one of the options from the select menu?
I have this UI Component:
<h:selectOneMenu value="#{authenticateController.country}">
<f:selectItems value="#{constants.countrySelectMenu}" />
</h:selectOneMenu>
The values of #{constants.countrySelectMenu} are a list of country ID - country name pairs.
Is there a way to render the list with a preselected value or at least is there a work-around to get this done?
Just preset the property behind <h:selectOneMenu value> with the desired value. You can do it in for example the bean's (post)constructor, action(listener) method, etc.
In your specific example, thus e.g.
public class AuthenticateController {
private String country;
#PostConstruct
public void init() {
country = "NL";
}
// ...
}
It works exactly the same way for all other UIInput components like <h:inputText> and so on. The input component's value itself simply represents the (pre)selected/checked/filled value.
See also:
How to populate options of h:selectOneMenu from database?
Related
I'm converting from RichFaces to PrimeFaces 11.0 and replacing a custom component based on the old rich:suggestionBox with p:autoComplete. This works perfectly in most code where the value refers to a POJO property. However I'm finding difficulty getting p:autoComplete to set the value of a reference from an ArrayList of POJOs.
class OrderItem {
...
public List<Contact> getContacts() {
return contacts;
}
...
}
On a view, we want to be able to allow autoCompletes on multiple contacts per orderItem. The orderItem.contacts list is prepopulated with null placeholders by the time this is rendered:
<ui:repeat id="assignabilityContacts" value="#{orderItem.contacts}" var="_contact" varStatus="idx">
...
<p:autoComplete value="#{orderItem.contacts[idx.index]}"
id="existingContact"
binding="#{contactGroupManager.contactAutoComplete}"
converter="#{baseEntityConverter}"
completeMethod="#{contactGroupManager.autocomplete}"
forceSelection="true"
var="row" itemValue="#{row}" itemLabel="#{row.toString()}"
minQueryLength="2"
maxResults="10"
>
<f:attribute name="orderItem" value="#{orderItem}" />
</p:autoComplete>
The signature of the complete method is...
public List<Contact> autocomplete(Object suggest) {
}
And the converter follows Bauke Scholtz pattern for entities:
Implement converters for entities with Java Generics
As defined above, the value of orderItem.contacts[idx.index] is being set to a string, corresponding to the toString() of the selected pojo. The converter is never called.
If I change the value to a property of class Contact on another backing bean, the converter is called and the property is set to the selected Contact entity. This is the correct behavior.
value="#{contactSelector.selectedContact}"
Is it possible that autoConverter is not picking up the entity class from the List, and are there any suggested workarounds for this?
Is it possible to preselect one of the options from the select menu?
I have this UI Component:
<h:selectOneMenu value="#{authenticateController.country}">
<f:selectItems value="#{constants.countrySelectMenu}" />
</h:selectOneMenu>
The values of #{constants.countrySelectMenu} are a list of country ID - country name pairs.
Is there a way to render the list with a preselected value or at least is there a work-around to get this done?
Just preset the property behind <h:selectOneMenu value> with the desired value. You can do it in for example the bean's (post)constructor, action(listener) method, etc.
In your specific example, thus e.g.
public class AuthenticateController {
private String country;
#PostConstruct
public void init() {
country = "NL";
}
// ...
}
It works exactly the same way for all other UIInput components like <h:inputText> and so on. The input component's value itself simply represents the (pre)selected/checked/filled value.
See also:
How to populate options of h:selectOneMenu from database?
I want to create selectOneRadio list and selectCheckboxMenu which items get from related bean objects.
For non selected list it is working well.But how can i provide these lists with some item(s) selected by default.
My current selectCheckboxMenu code is like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDay}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
it should look like this when page loaded and user haven't done anything yet.
EDIT
Day Class :-
public class Day{
private String name;
private String value;
//getters and setters
}
Value attribute of selectCheckboxMenu component should get the your default values.
On bean side you should write a getter for selectedDayList and all daylist.
Example:
#PostConstruct
public void init() {
dayList= new ArrayList<String>();
dayList.add("Mon");
dayList.add("Tue");
dayList.add("Wed");
dayList.add("Thu");
dayList.add("Fri");
dayList.add("Sat");
dayList.add("Sun");
selectedDayList= new ArrayList<String>();
selectedDayList.add("Tue");
selectedDayList.add("Wed");
}
public List<String> getDayList()
{
return dayList;
}
public List<String> getSelectedDaylist()
{
return selectedDayList;
}
.xhtml page should be like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDaylist}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
Good Luck!
Just add default values in the selectedDay list (or array) in init method (with #PostConstruct annotation). These values should have same value as corresponding itemValue attribute (in your case this is day.value).
As said in the title, i'd like to change a component property identified by an id in a jsf page from a managed bean. Here is my jsf code :
<p:calendar value="#{eventBean.beginDate}" id="from" pattern="dd/MM/yyyy HH:mm" required="true"/>
It's a PrimeFaces component. At the initialization of the page, i've got an empty field that display by clicking in a calendar. Choosing a date fill the field with the selected value. My question is : how to fill the field with the current date at the initialization of my jsf page ? I dunno if there is a possibility by using PrimeFaces calendar component properties (i've try several things that didn't work) and i'd like to know if that's possible using managed bean.
Thank you !
Just set the property during bean's (post)construction.
private Date beginDate;
public EventBean() {
// Here, in constructor.
eventDate = new Date();
}
#PostConstruct
public void init() {
// Or here, in a #PostConstruct method.
// This is invoked after any dependency and managed property injection.
eventDate = new Date();
}
Note that this approach is not specific to the calendar property. It applies to all kinds of properties.
You need to update/define default values at the bean. In this case you can define the value in the constructor; if the value depends of injected attributes you need to use a #PostConstruct method.
Is it possible to preselect one of the options from the select menu?
I have this UI Component:
<h:selectOneMenu value="#{authenticateController.country}">
<f:selectItems value="#{constants.countrySelectMenu}" />
</h:selectOneMenu>
The values of #{constants.countrySelectMenu} are a list of country ID - country name pairs.
Is there a way to render the list with a preselected value or at least is there a work-around to get this done?
Just preset the property behind <h:selectOneMenu value> with the desired value. You can do it in for example the bean's (post)constructor, action(listener) method, etc.
In your specific example, thus e.g.
public class AuthenticateController {
private String country;
#PostConstruct
public void init() {
country = "NL";
}
// ...
}
It works exactly the same way for all other UIInput components like <h:inputText> and so on. The input component's value itself simply represents the (pre)selected/checked/filled value.
See also:
How to populate options of h:selectOneMenu from database?