问题
In my setup I have a @Named Bean, the class ObWithDate is a @Entity with a date field validFrom. Objects of this class are in a List<ObWithDate> of the bean. I want to update the ObWithDate immediately if a user changes a date. The p:dataTable thus is showing several p:calendar components:
<h:form id="fUser">
<p:dataTable id="dt" var="i" value="#{myBean.list}">
<p:column>
<p:calendar id="cValidFrom" value="#{i.validFrom}">
<p:ajax event="dateSelect" listener="#{myBean.up}" update=":fUser:dt"/>
</p:calendar>
</p:column>
</p:dataTable>
</h:form>
The code of the bean:
public void up(DateSelectEvent event)
{
logger.info("DateSelectEvent "+event.getDate());
// How to get the corresponding ObWithDate?
}
This is a subsequent question of Primefaces p:calendar with p:ajax value not updated (one step delay) but now targeting the concrete issue: How to get the corresponding list item in the ajax call inside the beans method?
回答1:
You could resolve #{i} (terrible variable name by the way) programmatically.
FacesContext context = FacesContext.getCurrentInstance();
ObWithDate obWithDate = (ObWithDate) context.getApplication().evaluateExpressionGet(context, "#{i}", ObWithDate.class);
// ...
An alternative is to use DataModel as value of the <p:dataTable> so that you can use DataModel#getRowData().
private transient DataModel<ObWithDate> model;
private DataModel<ObWithDate> getModel() {
if (model == null) {
model = new ListDataModel<ObWithDate>(list);
}
return model;
}
so that you can get it in the listener method as follows
ObWithDate obWithDate = model.getRowData();
// ...
See also:
- How can I pass selected row to commandLink inside dataTable?
回答2:
@Daniel please add this as an answer, at least this is a workaround
adding :)
It might be the direction (migth be not :)) somethign like that
UIComponent ui = (UIComponent) event.getSource();
ui.getParent().findComponent( ....
place a break point and try some in the "watch" of the eclipse...
来源:https://stackoverflow.com/questions/10276602/jsf-pcalendar-in-pdatatable-how-to-get-the-row-of-pajax-dateselect-event