weixin_33738578 2012-08-01 21:32 采纳率: 0%
浏览 43

primefaces Ajax更新元素

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog