PrimeFaces:使用对话框添加用户不通过ajax

时间:2016-05-17 18:29:03

标签: ajax jsf primefaces datatable dialog

我有一个页面,其中一些用户通过datatable显示。我有一个添加新用户的选项。当添加窗口在同一页面中时,用户将通过ajax添加到数据表中。但是,如果我将add选项传递给primeface对话框,则ajax功能不起作用,并且数据表不会刷新。

以下是我的代码:

Code1:这里我们在同一页面中使用了添加用户窗口 - 这个工作

 <h:form>
    <h:panelGrid id="grid" cellpadding="5" columns="2"
        style="margin-bottom:10px">
        <f:facet name="header">
            <p:messages id="msgs" />
        </f:facet>
        <p:outputLabel for="firstname" value="Firstname:" />
        <p:inputText id="firstname" value="#{userView.firstname}" />
        <p:outputLabel for="surname" value="Surname:" />
        <p:inputText id="surname" value="#{userView.lastname}"
        required="true" requiredMessage="Surname is required." />
    </h:panelGrid>

    <h:panelGrid id="grid2">
        <p:dataTable var="user" value="#{userView.getUsers()}">
            <p:column headerText="FirstName">
                <h:outputText value="#{user.firstname}" />
            </p:column>
            <p:column headerText="LastName">
                <h:outputText value="#{user.lastname}" />
            </p:column>
        </p:dataTable>
    </h:panelGrid>

    <h:panelGrid columns="1">
        <p:commandButton value="All" id="btnAll" process="@all" update="grid2"
            actionListener="#{userView.save}" />
    </h:panelGrid>

</h:form>

我的托管Bean是

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean
public class UserView {

private String firstname;
private String lastname;
public List<User> dataUser;

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public UserView()
{
    dataUser = new ArrayList<User>();

    dataUser.add(new User("AA", "LA"));
    dataUser.add(new User("AB", "LB"));
    dataUser.add(new User("AC", "LC"));
    dataUser.add(new User("AD", "LD"));
    dataUser.add(new User("AE", "LE"));

}




public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public List<User> getUsers()
{
    return dataUser;
}

public void save() {
    FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage("Welcome " + firstname + " " + lastname));
    dataUser.add(new User(firstname, lastname));
}

public class User{
     public String firstname;
     public String lastname;


     public User(String fn, String ln)
     {
         this.firstname= fn;
         this.lastname= ln;
     }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

}

代码2:如果我在pdialog中使用add用户,则页面不会刷新。上面的托管bean保持不变

<h:form>
    <h:panelGrid id="grid2">
        <p:dataTable var="user" value="#{userView.getUsers()}">
            <p:column headerText="FirstName">
                <h:outputText value="#{user.firstname}" />
            </p:column>

            <p:column headerText="LastName">
                <h:outputText value="#{user.lastname}" />
            </p:column>
        </p:dataTable>
    </h:panelGrid>



    <h:panelGrid columns="1">
        <p:commandButton value="AllPopup" id="btnAllPopup" process="@all"
            update="grid2" onclick="PF('dlg1').show();" />
    </h:panelGrid>


    <p:dialog header="Basic Dialog" widgetVar="dlg1" minHeight="40">
        <h:panelGrid id="gridpopup" cellpadding="5" columns="2"
            style="margin-bottom:10px">

            <p:outputLabel for="firstname1" value="Firstname:" />
            <p:inputText id="firstname1" value="#{userView.firstname}" />

            <p:outputLabel for="surname1" value="Surname:" />
            <p:inputText id="surname1" value="#{userView.lastname}"
                required="true" requiredMessage="Surname is required." />
        </h:panelGrid>

        <h:panelGrid columns="1">
            <p:commandButton value="Alladd" id="btnAlladd" process="@all"
                update="grid2" actionListener="#{userView.save}" />
        </h:panelGrid>
    </p:dialog>
</h:form> 

请帮助理解问题所在

0 个答案:

没有答案