设置子项后,将父数据值设置为true

时间:2014-01-24 13:58:37

标签: java jquery jsf primefaces

我想自动将根模块视图设置为true,如果选择了子模块,请任何人帮我解决这个问题,请检查我附带的JSF和Managed Bean代码。

JSF代码

<p:dialog widgetVar="assignPermission" id="assignPermissionDlgId">
    <p:panel>
        <h:form id="form">
            <p:dataTable value="#{roleModule.modulesList}" var="modules"
                         id="tableId">
                <p:column headerText="Root Module ID:">
                    <h:outputText value="#{modules.moduleID}" />
                </p:column>
                <p:column headerText="Module ID:">
                    <h:outputText value="#{modules.rootID}" />
                </p:column>
                <p:column headerText="Module Description:">
                    <h:outputText value="#{modules.moduleDescription}" />
                </p:column>
                <p:column headerText="View">
                    <h:selectBooleanCheckbox id="view" value="#{modules.view}" />
                </p:column>
                <p:column headerText="Create">
                    <h:selectBooleanCheckbox id="create" value="#{modules.create}" />
                </p:column>
                <p:column headerText="Edit">
                    <h:selectBooleanCheckbox id="edit" value="#{modules.edit}" />
                </p:column>
                <p:column headerText="Delete">
                    <h:selectBooleanCheckbox id="delete" value="#{modules.delete}" />
                </p:column>
            </p:dataTable>

            <p:toolbar>
                <p:toolbarGroup>
                    <p:commandButton value="Submit" update=":data"
                                     action="#{roleModule.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>
        </h:form>
    </p:panel>
</p:dialog>

ManagedBean

    public void confirmMethod()
    {
        System.out.println(modulesList.size());

        PreparedStatement pst = null;
        Connection con = getConnection();
//      System.out.println("value check"+applicationShort);
        String insert="INSERT INTO role_module_mapping VALUES(?,?,?,?,?,?)";


        for(Module list : modulesList)
        {
            try {
                pst=con.prepareStatement(insert);
                pst.setInt(1, 0);
                pst.setInt(2, list.getModuleID());

                pst.setString(3, (list.isCreate())==true? "T" : "F");
                pst.setString(4, (list.isEdit())==true? "T" : "F");
                pst.setString(5, (list.isDelete())==true? "T" : "F");
                pst.setString(6, (list.isView())==true? "T" : "F");
                pst.executeUpdate();
//              System.out.println("Method called"+value);

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }


}

的js

<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>

Actual working scree

1 个答案:

答案 0 :(得分:0)

假设您正在使用父结构和子进程使用相同实体的树结构,我会做类似的事情:

<p:selectBooleanCheckbox id="view" value="#{modules.view}" widgetVar="ckBoxVar#{modules.rootID}" onchange="#{(modules.moduleID ne null) ? 'ckBoxVar' : ''}#{(modules.moduleID ne null) ? modules.moduleID : ''}#{(modules.moduleID ne null) ? '.check()' : ''}"  />

出于某种原因,我有这种强烈的感觉,当你写:

<p:column headerText="Root Module ID:">
    <h:outputText value="#{modules.moduleID}" />
</p:column>
<p:column headerText="Module ID:">
    <h:outputText value="#{modules.rootID}" />
</p:column>
你实际上是想说这个:

<p:column headerText="Root Module ID:">
    <h:outputText value="#{modules.rootID}" />
</p:column>
<p:column headerText="Module ID:">
    <h:outputText value="#{modules.moduleID}" />
</p:column>