0

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:

enter image description here

<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?

Community
  • 1
  • 1
Thor
  • 6,607
  • 13
  • 62
  • 96
  • 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... – Daniel Apr 23 '12 at 08:53
  • @Daniel please add this as an answer, at least this is a workaround – Thor Apr 23 '12 at 12:03

2 Answers2

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Indeed, `#{i}` is terrible, but only used for the shortened example for SOF ;-) Although I don't exactly understand _why_, it's working. Thanks for your support! – Thor Apr 23 '12 at 12:37
1

@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...

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Although BalusC provided a more elegant way, it's possible to get the row index of `ui.getParent().getId()` with this solution. – Thor Apr 23 '12 at 12:46