我有一个基于List<>显示各种实体的数据表。当我选择一个单元格进行编辑时,我希望能够以某种方式获取实体以便更新它。 当然有event.getRowIndex,然后我可以使用List<>,但这并不总是方便的。是否有另一种方法可以从CellEditEvent中获取实体?
答案 0 :(得分:25)
一种方法是以编程方式EL评估当前<p:dataTable var>
。
给出
<p:dataTable value="#{bean.entities}" var="entity">
你可以按照以下方式获得
public void onCellEdit(CellEditEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
Entity entity = context.getApplication().evaluateExpressionGet(context, "#{entity}", Entity.class);
// ...
}
另一种方式,如果你对CellEditEvent
参数不感兴趣,那么将通过将当前迭代的实体作为参数传递来完全覆盖CellEditEvent
参数:
<p:ajax event="cellEdit" listener="#{bean.onCellEdit(entity)}" />
与
public void onCellEdit(Entity entity) {
// ...
}
请注意,您无法保留CellEditEvent
并传递其他参数。否则显然会给出这个答案。
答案 1 :(得分:9)
我一直在努力解决这个问题,我并不喜欢依赖var名称,所以我找到了这个解决方案:
public void onCellEdit(CellEditEvent event) {
Entity entity =(Entity)((DataTable)event.getComponent()).getRowData();
}
注意,实体更新后可以直接合并到DB中,也可以获得旧值。
PS:谢谢你@BalusC的一切:)
答案 2 :(得分:0)
我喜欢@ user1928596的答案,所以我对它进行了一些扩展,以获得单元格所代表的精确数据点,并仅更新它。这会将数据表列标题文本耦合到后端代码,但我不知道更好的方法。
结果令我感到惊讶的是,当我编辑数据表中的数据时,支持bean中的数据也会发生变化。我不需要cellEditEvent.getNewValue(),因为视图中的数据以某种方式绑定到辅助bean中的数据。我以为它只是显示器。 onCellEdit()方法末尾的log语句旨在显示Event对象的旧值和新值,但它只显示新值。
这是可单元格编辑的数据表:
这里是显示代码:
<p:dataTable id="facilitatorAdminEvents" var="event" value="#{testBean.facilitatorEvents}" editable="true" editMode="cell">
<p:ajax event="cellEdit" listener="#{testBean.editEvent}" update="facilitatorAdminEvents" />
<p:column headerText="Event Name"><p:cellEditor><f:facet name="output"><h:outputText value="#{event.name}"></h:outputText></f:facet><f:facet name="input"><p:inputText value="#{event.name}" style="width:100%" /></f:facet></p:cellEditor></p:column>
<p:column headerText="Start Date"><p:cellEditor><f:facet name="output"><h:outputText value="#{event.startdate}"><f:convertDateTime pattern="M/d/yyyy" /></h:outputText>
</f:facet><f:facet name="input"><p:calendar id="eventStartdate" value="#{event.startdate}" effect="fold" /></f:facet></p:cellEditor>
</p:column>
<p:column headerText="End Date"><p:cellEditor><f:facet name="output"><h:outputText value="#{event.enddate}"><f:convertDateTime pattern="M/d/yyyy" /></h:outputText>
</f:facet><f:facet name="input"><p:calendar id="eventEnddate" value="#{event.enddate}" effect="fold" /></f:facet></p:cellEditor>
</p:column>
<p:column headerText="Status"><p:cellEditor><f:facet name="output"><h:outputText value="#{event.status}" /></f:facet>
<f:facet name="input">
<p:selectOneMenu id="eventstatuses" value="#{event.status}" style="width: 100%; margin: auto; " scrollHeight="80" showHeader="false" label="Statuses">
<f:selectItems value="#{testBean.eventStatuses}" var="status" itemLabel="#{status}" />
</p:selectOneMenu>
</f:facet></p:cellEditor>
</p:column>
<p:column id="delete" style="text-align: center; vertical-align: middle; min-width: 54px; ">
<p:commandButton update="facilitatorAdminEvents" icon="ui-icon-close" actionListener="#{testBean.deleteEvent(event.id)}"></p:commandButton>
</p:column>
</p:dataTable>
onCellEdit方法(我将其命名为editEvent()):
public void editEvent(CellEditEvent cellEditEvent) {
Object newValue = cellEditEvent.getNewValue();
String columnHeader = cellEditEvent.getColumn().getHeaderText();
Event editedEvent = (Event) ((DataTable) cellEditEvent.getComponent()).getRowData();
Event eventBeforeEdit = null;
for (Event thisEvent : events) { // Find this event in the list of cached events.
if (editedEvent.getId() == thisEvent.getId()) {
eventBeforeEdit = thisEvent;
}
}
log.info("Updating event " + eventBeforeEdit + " to " + editedEvent);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
String update = null;
if ("Event Name".equals(columnHeader)) {
update = "update events set name = '" + newValue + "' where id = " + editedEvent.getId();
} else if ("Start Date".equals(columnHeader)) {
update = "update events set startdate = '" + dateFormatter.format(newValue) + "' where id = " + editedEvent.getId();
} else if ("End Date".equals(columnHeader)) {
update = "update events set enddate = '" + dateFormatter.format(newValue) + "' where id = " + editedEvent.getId();
} else if ("Status".equals(columnHeader)) {
update = "update events set status = '" + newValue + "' where id = " + editedEvent.getId();
} else {
log.error("Unrecognized value " + newValue + " encountered during event edit.");
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Event Save Failure:", "Apologies but we were unable to parse your entry " + newValue);
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
try {
mysqlNamedParameterJdbcTemplate.update(update, new HashMap<String, String>());
} catch (DuplicateKeyException e) { // There may be an event with the same name.
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Event Save Failure:",
"That name has already been used for an archived or logically deleted event. " + "Please use a different name for the new event to avoid confusion.");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
log.info("Event " + eventBeforeEdit + " updated to " + editedEvent);
loadEvents();
}