简体   繁体   English

JSF selectbooleancheckbox验证

[英]JSF selectbooleancheckbox validation

I have modules list with four checkbooks (View, Create,Edit and Delete). 我有四个清单的模块清单(查看,创建,编辑和删除)。 In that, if user click on Create check box or edit check box or delete check box want to checked view check box automatically and same, if uncheck view check box want to uncheck create.Edit and Delete automatically. 在这种情况下,如果用户单击“创建”复选框,“编辑”复选框或“删除”复选框要自动选中视图复选框,则与“取消选中视图”复选框要取消选中“创建。自动编辑和删除”相同。 Please help me to solve this issue as i'm new to JSF. 由于我是JSF的新手,请帮助我解决此问题。 thanks in advance 提前致谢

Regards Mohan 问候莫罕

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



                        <p:column headerText="Root 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="vi" value="#{roleModule.view[modules.moduleID]}"/>
                            </p:column>
                            <p:column headerText="Create" >
                                <h:selectBooleanCheckbox value="#{roleModule.create[modules.moduleID]}">
                                <p:ajax update="vi"  listener="#{roleModule.permissionCheck}"/>
                                </h:selectBooleanCheckbox>

                            </p:column>
                            <p:column headerText="Edit" >
                                <h:selectBooleanCheckbox value="#{roleModule.edit[modules.moduleID]}">
                                <p:ajax update="vi"  listener="#{roleModule.permissionCheck}"/>
                                </h:selectBooleanCheckbox>
                            </p:column>
                            <p:column headerText="Delete" >
                                <h:selectBooleanCheckbox value="#{roleModule.delete[modules.moduleID]}">
                                <p:ajax update="vi"  listener="#{roleModule.permissionCheck}"/>
                                </h:selectBooleanCheckbox>
                            </p:column>

                    </p:dataTable>

I would do it in jQuery, for one reason, it's so basic move to take it into server-side level, after all the unchecking and checking is done on the view. 我会在jQuery中执行此操作,原因之一是,在视图上完成所有取消选中和检查之后,将它带入服务器端级别是非常基本的举动。

JS JS

      $(PrimeFaces.escapeClientId('form:table'))
        .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:table')).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);
            } 
        });

Please Note: if you update the table, you should rerun the script or you could replace the selector with the form id only. 请注意:如果您更新表格,则应重新运行脚本,或者可以仅使用表单ID替换选择器。 like this 像这样

$(PrimeFaces.escapeClientId('form')).on ....

of course you should include the code in the $( document ).ready() . 当然,您应该在$(document).ready()中包含代码。


EDIT: BASED ON YOUR REQUEST. 编辑:根据您的要求。

I have created a small project on github , you can download the project and see how jQuery (JS in general) works with JSF, and here's a live demo . 在github上创建了一个小项目 ,您可以下载该项目,并查看jQuery(通常是JS)如何与JSF一起使用,这是一个实时演示

Two main files are main.xhml and checkBoxesJQuery.js . 两个主要文件是main.xhmlcheckBoxesJQuery.js

Hope it helps. 希望能帮助到你。

Try this, This may help you JSF Code: 试试这个,这可能对您帮助JSF代码:

<h:selectBooleanCheckbox value="#{bean.viewChecked}" >
    <a4j:support action="#{bean.viewCheckBoxAction}" event="onclick" reRender="panelId"/>
</h:selectBooleanCheckbox>
<h:outputText value="View" />

<h:selectBooleanCheckbox value="#{bean.createChecked}" >
    <a4j:support action="#{bean.createCheckBoxAction}" event="onclick" reRender="panelId"/>
 </h:selectBooleanCheckbox>
 <h:outputText value="Create" />

 <h:selectBooleanCheckbox value="#{bean.editChecked}" >
     <a4j:support action="#{bean.editCheckBoxAction}" event="onclick" reRender="panelId"/>
 </h:selectBooleanCheckbox>
 <h:outputText value="Edit" />

 <h:selectBooleanCheckbox value="#{bean.deleteChecked}" >
    <a4j:support action="#{bean.deleteCheckBoxAction}" event="onclick" reRender="panelId"/>
 </h:selectBooleanCheckbox>
 <h:outputText value="Delete" />

Bean Code: Bean代码:

public String viewCheckBoxAction()
    {
        if(!viewChecked) // while view is unchecked then uncheck all the others
        {
            editChecked = false;
            deleteChecked = false;
            createChecked = false;
        }

        return null;
    }


    public String createCheckBoxAction()
    {
        viewCheckManage();
        return null;
    }


    public String editCheckBoxAction()
    {
        viewCheckManage();
        return null;
    }


    public String deleteCheckBoxAction()
    {
        viewCheckManage();
        return null;
    }

    private void viewCheckManage() 
    {
        if(createChecked || deleteChecked || editChecked) // while any one is checked then view also checked
        {
            viewChecked = true;
        }
        else
        {
            viewChecked = false;
        }
    }

You may try something like this 您可以尝试这样的事情

<?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"
    xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Test</title>
</h:head>
<h:body>

    <h:form id="form">
    <p:dataTable value="#{roleModule.modulesList}" var="module" id="table">
        <p:column headerText="Module ID:">
            <h:outputText value="#{module.moduleID}" />
        </p:column>
        <p:column headerText="Root Module ID:">
            <h:outputText value="#{module.rootID}" />
        </p:column>
        <p:column headerText="Module Description:">
            <h:outputText value="#{module.moduleDescription}" />
        </p:column>
        <p:column headerText="View">
            <h:selectBooleanCheckbox id="view" value="#{module.view}">
                <p:ajax update=":form:table" listener="#{roleModule.permissionCheck}" />
            </h:selectBooleanCheckbox>
        </p:column>
        <p:column headerText="Create">
            <h:selectBooleanCheckbox id="create" value="#{module.create}">
                <p:ajax update=":form:table" listener="#{roleModule.permissionCheck}" />
            </h:selectBooleanCheckbox>

        </p:column>
        <p:column headerText="Edit">
            <h:selectBooleanCheckbox id="edit" value="#{module.edit}">
                <p:ajax update=":form:table" listener="#{roleModule.permissionCheck}" />
            </h:selectBooleanCheckbox>
        </p:column>
        <p:column headerText="Delete">
            <h:selectBooleanCheckbox id="delete" value="#{module.delete}">
                <p:ajax update=":form:table" listener="#{roleModule.permissionCheck}" />
            </h:selectBooleanCheckbox>
        </p:column>

        </p:dataTable>
    </h:form>
</h:body>
</html>

and if your module is an entity bean, you may want to annotate your checkboxes attributes with @Transient 并且如果您的模块是实体bean,则可能需要使用@Transient注释复选框属性

import java.io.Serializable;


public class Module implements Serializable {
    private static final long   serialVersionUID    = 176253618089501709L;
    private String moduleID,rootID,moduleDescription;
    private boolean view,edit,delete,create;

    public String getModuleID() {
        return moduleID;
    }

    public void setModuleID(String moduleID) {
        this.moduleID = moduleID;
    }

    public String getRootID() {
        return rootID;
    }

    public void setRootID(String rootID) {
        this.rootID = rootID;
    }

    public String getModuleDescription() {
        return moduleDescription;
    }

    public void setModuleDescription(String moduleDescription) {
        this.moduleDescription = moduleDescription;
    }

    public boolean isView() {
        return view;
    }

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

    public boolean isEdit() {
        return edit;
    }

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

    public boolean isDelete() {
        return delete;
    }

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

    public boolean isCreate() {
        return create;
    }

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

    @Override
    public String toString() {
        return "Module [moduleID=" + moduleID + ", rootID=" + rootID + ", moduleDescription=" + moduleDescription + ", view=" + view + ", edit=" + edit + ", delete=" + delete + ", create=" + create + "]";
    }

}

and finally 最后

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;


@ManagedBean
@ViewScoped
public class RoleModule implements Serializable{

    private static final long   serialVersionUID    = 1L;

    private List<Module> modulesList;

    @PostConstruct
    public void init() {
        modulesList = new ArrayList<Module>();

        Module m1 = new Module();
        m1.setModuleID("1");
        m1.setRootID("root");
        m1.setModuleDescription("desc1");
        modulesList.add(m1);

        Module m2 = new Module();
        m2.setModuleID("2");
        m2.setRootID("root");
        m2.setModuleDescription("desc2");
        modulesList.add(m2);
    }

    public void permissionCheck(AjaxBehaviorEvent event){
        Boolean value = (Boolean) ((UIInput) event.getComponent()).getValue();

        UIInput component = ((UIInput) event.getComponent());

        FacesContext context = FacesContext.getCurrentInstance();
        Module module = context.getApplication().evaluateExpressionGet(context, "#{module}", Module.class);

        System.out.println(module+","+value+","+component.getId());

        switch(component.getId()){
            case "create":
            case "delete":          
            case "edit":
                if (value){
                    module.setView(true);
                }
                break;
            case "view":
                if (!value){
                    module.setCreate(false);
                    module.setDelete(false);
                    module.setEdit(false);
                }
        }
    }

    public List<Module> getModulesList() {
        return modulesList;
    }

    public void setModulesList(List<Module> modulesList) {
        this.modulesList = modulesList;
    }

}

UPDATE: a little errata below 更新:下面有一点勘误

                //try as non-string using equal

                Path pathFilterNonString = getPath(filter.getKey(), site, siteType);
                Class pathType = pathFilterNonString.getJavaType();

                if (pathType.equals(Long.class)){
                    try{
                        filterCondition = cb.and(filterCondition, cb.equal(pathFilterNonString, Long.valueOf(filter.getValue())));
                    }catch(java.lang.NumberFormatException nfe){
                        //ignore
                        //java.lang.NumberFormatException: For input string: "a"
                    }
                }else if (pathType.equals(Timestamp.class)){
                    try{
                        filterCondition = cb.and(filterCondition, cb.equal(pathFilterNonString, Timestamp.valueOf(filter.getValue())));
                    }catch(java.lang.IllegalArgumentException e){
                        //ignore
                        //java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
                    }

                }

您可以使用复选框的onchange属性,并添加JavaScript代码,如document.getElementByID('formID:tableID:lineID:boxID').checked=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM