i have a behaviour here i don't understand. I am using Spring 3.1, Primefaces 3.3.1 and JSF 2.
I am using a datatable and want to add a row via a dialog within a form. This is my table, including the button to call the dialog:
<h:form id="form">
<!-- open the dialog on click -->
<p:commandButton value="New" onclick="dlgAddTask.show()"
update="taskList" />
<p:dataTable var="task" value="#{taskBean.tasks}" id="taskList"
widgetVar="taskList" editable="true" rowkey="task.id"
paginator="true" rows="50"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="25,50,100">
<p:ajax event="rowEdit" update="@this" listener="#{taskBean.save}" />
<p:column headerText="Name" style="width:125px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{task.name}" style="width:100%" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{task.name}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:20px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
This is my dialog with the relevant javascript (it's in the same form as the table is):
<p:dialog id="dlgAddTask" header="Add New Task" widgetVar="dlgAddTask">
<h:form>
<h:panelGrid columns="2" cellpadding="5" width="500">
<h:outputLabel for="name" value="Name" />
<p:inputText value="#{addTaskBean.name}" id="name" required="true"
label="name" />
<h:outputLabel for="description" value="Description:" />
<p:inputTextarea value="#{addTaskBean.description}"
id="description" label="description" />
<h:outputLabel for="dueTo" value="Due To:" />
<p:calendar value="#{addTaskBean.dueTo}" id="dueTo" label="dueTo" />
<h:outputLabel for="done" value="Done:" />
<p:selectBooleanCheckbox value="#{addTaskBean.done}" id="done"
label="done" />
<f:facet name="footer">
<p:commandButton id="addButton" value="Add"
actionListener="#{addTaskBean.addTask}"
oncomplete="handleLoginRequest(xhr, status, args)" />
<p:commandButton value="Cancel" onclick="dlgAddButton.hide()" />
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
<script type="text/javascript">
function handleLoginRequest(xhr, status, args) {
if (args.validationFailed || !args.taskAdded) {
jQuery('#dialog').effect("shake", {
times : 3
}, 100);
} else {
dlgAddTask.hide();
}
}
</script>
</h:form>
And this is my Bean (leaving out the getter and setter):
public class AddTaskBean {
@Inject
TaskDao taskDao;
private boolean done;
private String name;
private String description;
private Date dueTo;
@Transactional
public void addTask() {
RequestContext context = RequestContext.getCurrentInstance();
FacesMessage msg = null;
Task task = new Task();
boolean taskAdded = false;
task.setDescription(getDescription());
task.setDone(isDone());
task.setDueTo(getDueTo());
task.setName(getName());
if (taskDao.persist(task) != null) {
taskAdded = true;
msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Task",
getName() + " added.");
} else {
taskAdded = false;
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Task Error",
"Something went wrong on adding task.");
}
FacesContext.getCurrentInstance().addMessage(null, msg);
context.addCallbackParam("taskAdded", taskAdded);
}
Now, if i open the dialog, add a task, it works up to the update point. So i have a new task in the database, the dialog closes, but the table does not get updated. Hitting F5 shows me the new task in the table.
But (please read carefully as it is a bit tricky to explain for me), if i dont close the dialog after adding a task (by not calling the javascript via oncomplete="handleLoginRequest(xhr, status, args)"), the task will be added too, but the dialog remains open of course. If i press cancel then, the dialog closes and the table get's updated and i can see the new task.
I cannot explain this, maybe one of you can?
Thanks in Advance