【问题标题】:reset <p:selectBooleanCheckbox> on page load在页面加载时重置 <p:selectBooleanCheckbox>
【发布时间】:2014-01-16 05:44:58
【问题描述】:

如何在页面加载时重置?使用 JSF 或 CommandButton 的 onClick 事件

请在此处发布一个使用 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 代码

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

【问题讨论】:

  • 我正在尝试发布我的代码,但它只显示错误消息,说包含代码只添加更多命令。如果你能帮助我发布我的代码
  • 只需将您的代码复制粘贴到您的问题中即可。
  • 请查看附件代码
  • 你试过Request Scope吗?在RoleModule托管bean上放置请求范围注释
  • 试过但没有得到预期的结果

标签: jsf primefaces


【解决方案1】:

如果我了解您想要实现的目标,那么这非常简单......

首先设置对话框ID

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

然后将您的按钮更改为此

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

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

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

【讨论】:

  • 我将复选框值保存在数据库中。下一次开放时间我再次获得相同的先前值而无需刷新。
【解决方案2】:

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

这就是您的复选框的外观:

    <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。 希望这会有所帮助。

【讨论】:

  • 你为什么不在这里发布你的jsf和bean代码?我可以看得更清楚。
  • @PLanka 为什么你确定整个 bean 将被重建以将 false 设置为值?这可能适用于@RequestScoped,可能不适用于其他范围!
猜你喜欢
  • 2011-01-26
  • 2017-01-22
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多