Reputation: 2678
Why is my List<Date>
not getting converted? The <p:selectOneMenu>
items pattern looks like
Thu Mar 01 00:00:00 BRT 2012
instead of the desired "MM/yyyy".
<p:selectOneMenu value="#{report003.selectedMes}">
<p:ajax update="pesquisaThomas" listener="#{report003.loadPesquisa()}" />
<f:selectItem itemLabel="[Todos]" itemValue="" />
<f:convertDateTime pattern="MM/yyyy" />
<f:selectItems value="#{report003.pesquisaMeses}" />
</p:selectOneMenu>
Upvotes: 3
Views: 2399
Reputation: 722
Try this:
managed bean
private SimpleDateFormat sdf;
(...)
@PostConstruct
public void init(){
sdf = new SimpleDateFormat("MM/yyyy");
}
public SimpleDateFormat getSdf(){
return sdf;
}
public void setSdf(SimpleDateFormat sdf){
this.sdf = sdf;
}
xhtml
<p:selectOneMenu value="#{report003.selectedMes}">
<p:ajax update="pesquisaThomas" listener="#{report003.loadPesquisa()}" />
<f:selectItem itemLabel="[Todos]" itemValue="" />
<f:selectItems value="#{report003.pesquisaMeses}"
var="mes" itemValue="#{mes}"
itemLabel="#{myMB.sdf.format(mes)}" />
Upvotes: 0
Reputation: 1108732
The conversion applies to the item value only, not to the item label. The label is just presented using the default Date#toString()
pattern.
Your best bet is either creating a List<SelectItem>
instead of List<Date>
wherein you convert the item labels yourself,
List<Date> availableDates = ...;
List<SelectItem> selectItems = ...;
SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy");
for (Date availableDate : availableDates) {
selectItems.add(new SelectItem(availableDate, sdf.format(availableDate)));
}
or using <f:selectItems var>
with an EL function in the itemValue
. For example, the OmniFaces of:formatDate()
(or homegrow one).
<f:selectItems value="#{bean.availableDates}" var="date"
itemValue="#{date}" itemLabel="#{of:formatDate(date, 'MM/yyyy')}" />
This problem is by the way not specific to <p:selectOneMenu>
, you would have exactly the same problem when using the standard <h:selectOneMenu>
.
Upvotes: 4