如何在p:datatable中没有选择行时阻止显示p:对话框?

时间:2013-06-16 22:40:23

标签: java jsf primefaces

我有带p:datatble的jsf页面和用于添加/更新/删除表格数据的控制按钮。当用户点击按钮时,会显示p:dialog。 这是我的users.xhtml

<h:form id="form">
                    <!-- Table for displayng users information -->
                    <p:dataTable id="users" var="user" value="#{usersBean.users}" selectionMode="single"
                                 selection="#{usersBean.selected}" rowKey="#{user.id}">
                        <!-- Controls -->
                        <f:facet name="header">
                            <p:commandButton id="addButton" value="New" oncomplete="addUser.show()"
                                             update=":form:newUser"/>
                            <p:commandButton id="updbutton" value="Update" oncomplete="updUser.show()"
                                             update=":form:updateUser" action="#{usersBean.constructUser}"/>
                            <p:commandButton id="delButton" value="Delete" oncomplete="delUser.show()"
                                             update=":form:delDialog"/>
                        </f:facet>
                        <p:column>
                            <f:facet name="header">
                                Login
                            </f:facet>
                            <h:outputText value="#{user.login}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Password
                            </f:facet>
                            <h:outputText value="#{user.password}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Name
                            </f:facet>
                            <h:outputText value="#{user.name}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Surname
                            </f:facet>
                            <h:outputText value="#{user.surname}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Role
                            </f:facet>
                            <h:outputText value="#{user.role}"/>
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton value="CVS" ajax="false">
                                <p:dataExporter target="users" type="csv" fileName="Users"/>
                            </p:commandButton>
                        </f:facet>
                    </p:dataTable>
                    <!-- Dialog for adding new User -->
                    <p:dialog id="addDialog" header="Add new user" widgetVar="addUser" resizable="false"
                              width="400" showEffect="clip" hideEffect="fold">
                        <h:panelGrid id="newUser" columns="2" cellpadding="4">
                            <p:outputLabel for="newLogin" value="Login :"/>
                            <p:inputText id="newLogin" value="#{usersBean.newUser.login}"/>
                            <p:outputLabel for="newPassword" value="Password :"/>
                            <p:inputText id="newPassword" value="#{usersBean.newUser.password}"/>
                            <p:outputLabel for="newName" value="Name :"/>
                            <p:inputText id="newName" value="#{usersBean.newUser.name}"/>
                            <p:outputLabel for="newSurname" value="Surname :"/>
                            <p:inputText id="newSurname" value="#{usersBean.newUser.surname}"/>
                            <p:outputLabel for="newRole" value="Role :"/>
                            <p:selectOneMenu id="newRole" value="#{usersBean.newUser.role}">
                                <f:selectItems value="#{usersBean.roles}"/>
                            </p:selectOneMenu>

                            <f:facet name="footer">
                                <p:commandButton value="Save" actionListener="#{usersBean.addUser}"
                                                 update=":form:users"
                                                 oncomplete="addUser.hide()"/>
                            </f:facet>
                        </h:panelGrid>
                    </p:dialog>
                    <!-- Dialog for deleting User -->
                    <p:confirmDialog id="delDialog" message="Are You sure to delete
                    #{usersBean.selected.name} user?" widgetVar="delUser" resizable="false"
                                     width="400" showEffect="clip" hideEffect="fold">
                        <p:commandButton id="confirm" value="Yes" action="#{usersBean.delete}"
                                         update=":form:users" onclick="delUser.hide()"/>
                        <p:commandButton id="declice" value="No" onclick="delUser.hide()"/>
                    </p:confirmDialog>
                    <!-- Dialog for updating User information -->
                    <p:dialog id="updDialog" header="Update user information" widgetVar="updUser"
                              resizable="false" width="400" showEffect="clip" hideEffect="fold">
                        <h:panelGrid id="updateUser" columns="2" cellpadding="4">
                            <p:outputLabel for="updLogin" value="Login :"/>
                            <p:inputText id="updLogin" value="#{usersBean.updated.login}"/>
                            <p:outputLabel for="updPassword" value="Password :"/>
                            <p:inputText id="updPassword" value="#{usersBean.updated.password}"/>
                            <p:outputLabel for="updName" value="Name :"/>
                            <p:inputText id="updName" value="#{usersBean.updated.name}"/>
                            <p:outputLabel for="updSurname" value="Surname :"/>
                            <p:inputText id="updSurname" value="#{usersBean.updated.surname}"/>
                            <p:outputLabel for="updRole" value="Role :"/>
                            <p:selectOneMenu id="updRole" value="#{usersBean.updated.role}">
                                <f:selectItems value="#{usersBean.roles}"/>
                            </p:selectOneMenu>

                            <f:facet name="footer">
                                <p:commandButton value="Update" action="#{usersBean.updUser}"
                                                 update=":form:users" onclick="updUser.hide()"/>
                            </f:facet>
                        </h:panelGrid>
                    </p:dialog>
                </h:form>

我的托管bean:

@ManagedBean(name = "usersBean")
@ViewScoped
public class UsersController {
    private List<String> roles;
    private UserDAO userDAO;
    private List<UserEnt> users;
    private UserEnt newUser;
    private UserEnt selected;
    private UserEnt updated;

    public UsersController() {
        userDAO = new UserDAO();
        newUser = new UserEnt();
        selected = new UserEnt();
        updated = new UserEnt();
    }

    public UserEnt getNewUser() {
        return newUser;
    }

    public void setNewUser(UserEnt newUser) {
        this.newUser = newUser;
    }

    public List<UserEnt> getUsers() {
        return users;
    }

    public void setUsers(List<UserEnt> users) {
        this.users = users;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public UserEnt getSelected() {
        return selected;
    }

    public void setSelected(UserEnt selected) {
        this.selected = selected;
    }

    public UserEnt getUpdated() {
        return updated;
    }

    public void setUpdated(UserEnt updated) {
        this.updated = updated;
    }

    @PostConstruct
    public void initData() {
        roles = new ArrayList<String>();
        roles.add("ROLE_USER");
        roles.add("ROLE_ADMIN");
        users = userDAO.getListOfUsers();
    }

    public void constructUser() {
        updated = selected;
    }

    public void addUser() {
        userDAO.saveNewUser(newUser);
        users = userDAO.getListOfUsers();
    }

    public void delete() {
        userDAO.deleteUser(selected);
        users = userDAO.getListOfUsers();
    }

    public void updUser() {
        userDAO.updateUser(updated);
        users = userDAO.getListOfUsers();
    }
}

如果在p:dialog

中未选择任何行,我如何阻止显示p:datatable

1 个答案:

答案 0 :(得分:1)

更改您的更新按钮,如:

<p:commandButton id="updbutton" value="Update" oncomplete="handleRequest(xhr, status, args)"
                                             update=":form:updateUser" action="#{usersBean.constructUser}"/>

并添加如下脚本:

 <script type="text/javascript">  
        function handleRequest(xhr, status, args) {  
            if(!args.noUserSelected) {  
                updUser.show()

            }   
           }  
    </script> 

在你的bean类中,进行如下更改。

  public void constructUser() {
        /*ADD SOME LOGIC TO CHECK if selected is initializeded with values based on if selected is not initialized with values add callback parameter RequestContext.getCurrentInstance().addCallbackParam("noUserSelected", true);*/
        updated = selected;
    }

这样你就可以在js函数中得到这个参数,并且不会显示对话框。

HTH,