A simple application that uses Primefaces datatables with radio button selection doesn't behave properly.
When I choose an element with radio button, the event
argument is null
in delete()
method, therefore the selected row can't be removed from the datatable.
view.xhtml // Here is where I get problems
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Event List</title>
</h:head>
<h:body>
<h:form>
<p:dataTable id="eventlist" var="event" value="#{eventBean.eventlist}" selection="#{eventBean.selectedEvent}" rowKey="#{event.id}" scrollable="true" scrollHeight="200" style="width:500px;">
<f:facet name="header">
Event List
</f:facet>
<p:column selectionMode="single" style="width:16px;text-align:center"/>
<p:column headerText="ID">
<h:outputText value="#{event.id}">
</h:outputText>
</p:column>
<p:column headerText="Date">
<h:outputText value="#{event.date}">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:outputText>
</p:column>
<f:facet name="footer">
<p:commandButton process="eventlist" action="#{eventBean.delete(eventBean.event)}" value="#{eventBean.event}" ajax="true" >
</p:commandButton>
</f:facet>
</p:dataTable>
</h:form>
</h:body>
EventBean.java // This is my managed bean
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@ManagedBean
@Named(value = "eventBean")
@RequestScoped
public class EventBean {
@EJB
private EventManager em;
private Event event;
private Event selectedEvent = new Event();
private Date currentDate;
private List<Event> eventlist = new ArrayList<Event>();
public Date getCurrentDate() {
if (currentDate == null) {
currentDate = new Date();
}
return currentDate;
}
public EventBean() {
}
public Event getEvent() {
if (event == null) {
event = new Event();
}
return event;
}
public void setEvent(Event event) {
this.event = event;
}
public List<Event> getEventlist() {
return eventlist;
}
public void setEventlist(List<Event> eventlist) {
this.eventlist = eventlist;
}
public Event getSelectedEvent() {
return selectedEvent;
}
public void setSelectedEvent(Event selectedEvent) {
this.selectedEvent = selectedEvent;
}
public String create() {
em.save(event);
eventlist = em.findEvents();
return "view";
}
public String delete(Event event){ //the event object is null
em.deleteEvent(event);
return "index";
}
}
Briefly, what I would like to achieve is: select properly the row from the datatable and then delete it both from the datatable and database.