在页面加载时重置<p:selectbooleancheckbox> </p:selectbooleancheckbox>

时间:2014-01-16 05:44:59

标签: jsf primefaces

如何重置页面加载?使用CommandButton的

请在此处以Javascript或Ajax

发布示例

请帮助我解决这个问题

这里的JSF代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</h:head>
<h:body>
    <p:commandButton value="Assign Permission"
        onclick="assignPermissionDlg.show()"></p:commandButton>

    <p:dialog widgetVar="assignPermissionDlg" width="75%">
        <h:form id="form">
            <p:panel>
                <p:dataTable value="#{mainBean.list}" var="perm" id="tableId">

                    <p:column headerText="Module Description:">
                        <h:outputText value="#{perm.name}" />
                    </p:column>
                    <p:column headerText="View">
                        <h:selectBooleanCheckbox id="view" value="#{perm.view}" >

                        </h:selectBooleanCheckbox>
                    </p:column>
                    <p:column headerText="Create">
                        <h:selectBooleanCheckbox id="create" value="#{perm.create}">

                        </h:selectBooleanCheckbox>

                    </p:column>
                    <p:column headerText="Edit">
                        <h:selectBooleanCheckbox id="edit" value="#{perm.edit}">

                        </h:selectBooleanCheckbox>
                    </p:column>
                    <p:column headerText="Delete">
                        <h:selectBooleanCheckbox id="delete" value="#{perm.delete}">

                        </h:selectBooleanCheckbox>
                    </p:column>

                </p:dataTable>
                <p:toolbar>
                    <p:toolbarGroup>
                        <p:commandButton value="Submit"
                            action="#{mainBean.confirmMethod}">

                            <p:confirm header="Confirmation" message="Are you sure?"
                                icon="ui-icon-alert" />
                        </p:commandButton>


                    </p:toolbarGroup>
                </p:toolbar>
                <p:confirmDialog global="true" showEffect="fade"
                    hideEffect="explode">
                    <p:commandButton value="Yes" type="button"
                        styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                    <p:commandButton value="No" type="button"
                        styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                </p:confirmDialog>
            </p:panel>
        </h:form>

    </p:dialog>
    <script type="text/javascript">
                $(function() {
                    $(PrimeFaces.escapeClientId('form:tableId'))
                            .on(
                                    "change",
                                    "input[type='checkbox'][name*='edit'], input[type='checkbox'][name*='create'], input[type='checkbox'][name*='delete']",
                                    function() {
                                        var tr = $(this).parent().parent();
                                        var view = tr
                                                .find("input[type='checkbox'][name*='view']");
                                        var create = tr
                                                .find("input[type='checkbox'][name*='create']");
                                        var edit = tr
                                                .find("input[type='checkbox'][name*='edit']");
                                        var deleteBox = tr
                                                .find("input[type='checkbox'][name*='delete']");
                                        if ($(this).is(':checked')) {
                                            view.prop("checked", true);
                                        } else {
                                            if (create.is(':checked') || edit.is(':checked')
                                                    || deleteBox.is(':checked')) {
                                                view.prop("checked", true);
                                            } else
                                                view.prop("checked", false);
                                        }
                                    });

                    $(PrimeFaces.escapeClientId('form:tableId')).on(
                            "change",
                            "input[type='checkbox'][name*='view']",
                            function() {
                                var tr = $(this).parent().parent();
                                var view = tr.find("input[type='checkbox'][name*='view']");
                                var create = tr.find("input[type='checkbox'][name*='create']");
                                var edit = tr.find("input[type='checkbox'][name*='edit']");
                                var deleteBox = tr
                                        .find("input[type='checkbox'][name*='delete']");
                                if ($(this).is(':not(:checked)')) {
                                    create.prop("checked", false);
                                    edit.prop("checked", false);
                                    deleteBox.prop("checked", false);
                                }
                            });
                })


                </script>


</h:body>
</html>

Bean Code

public class UserPojo {

    private String name;
    private Boolean view;
    private Boolean create;
    private Boolean edit;
    private Boolean delete;

    public Boolean getView() {
        return view;
    }

    public void setView(Boolean view) {
        this.view = view;
    }

    public Boolean getCreate() {
        return create;
    }

    public void setCreate(Boolean create) {
        this.create = create;
    }

    public Boolean getEdit() {
        return edit;
    }

    public void setEdit(Boolean edit) {
        this.edit = edit;
    }

    public Boolean getDelete() {
        return delete;
    }

    public void setDelete(Boolean delete) {
        this.delete = delete;
    }

    public UserPojo() {

    }

    public UserPojo(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

最后的代码

import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;


    @ManagedBean
    @ViewScoped
    public class MainBean implements Serializable{


        /**
         * 
         */
        private static final long serialVersionUID = -1245080637297904760L;
        private List<UserPojo> list;

        public MainBean() {
              fillList();
        }

        public List<UserPojo> getList() {
            return list;
        }

        public void setList(List<UserPojo> list) {
            this.list = list;
        }

        public void fillList() {
            list = new ArrayList<UserPojo>();
            list.add(new UserPojo("Jack"));
            list.add(new UserPojo("Jhon"));
            list.add(new UserPojo("Smack"));
            list.add(new UserPojo("Jimmy"));
            list.add(new UserPojo("Dummu"));
            list.add(new UserPojo("Stakr"));
            list.add(new UserPojo("Simi"));
        }

        public void confirmMethod()
        {
            System.out.println("Save values in DB****************");
            for(UserPojo ul : list)
            {
                System.out.println("insert statement");



            }
        }

    }

Please help me to solve my issue Thanks

2 个答案:

答案 0 :(得分:0)

您可以清除构造函数中复选框的值。

这是您的复选框的外观:

    <p:selectBooleanCheckbox value="#{BeanClass.value1}" > </p:selectBooleanCheckbox>

选中该复选框后,value1变为true。在bean类的构造函数中,将value1设为false。

你豆类:

    Class BeanClass
    {

        private boolean value1;
        public BeanClass()
        {
            value1 = false;
            //Someother statements might follow.
        }

        public boolean getValue1()
        {
            return value1;
        }

        public void setValue1(boolean value1)
        {
            this.value1 = value1;
        }
    }

请注意,我在构造函数中将value1的值设置为false,在页面加载时调用它,从而在primeface标记中呈现false。 希望这会有所帮助。

答案 1 :(得分:0)

如果我理解你想要达到的目标那么它就非常简单......

首先设置对话框ID

 <p:dialog id="assignPermissionDlgId" widgetVar="assignPermissionDlg" width="75%">

然后将按钮改为此

<p:commandButton value="Assign Permission" update=":assignPermissionDlgId"
    oncomplete="assignPermissionDlg.show()"></p:commandButton>

我认为每次打开对话框时都会看到选中的前一个复选框,这样就会在显示对话框之前更新对话框,更新会导致重新渲染。

但是我看不到你在哪里保存复选框值! 注意:如果复选框中有值,则答案会有所不同.....