[英]JSF selectbooleancheckbox validation
我有四個清單的模塊清單(查看,創建,編輯和刪除)。 在這種情況下,如果用戶單擊“創建”復選框,“編輯”復選框或“刪除”復選框要自動選中視圖復選框,則與“取消選中視圖”復選框要取消選中“創建。自動編輯和刪除”相同。 由於我是JSF的新手,請幫助我解決此問題。 提前致謝
問候莫罕
<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>
我會在jQuery中執行此操作,原因之一是,在視圖上完成所有取消選中和檢查之后,將它帶入服務器端級別是非常基本的舉動。
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);
}
});
請注意:如果您更新表格,則應重新運行腳本,或者可以僅使用表單ID替換選擇器。 像這樣
$(PrimeFaces.escapeClientId('form')).on ....
當然,您應該在$(document).ready()中包含代碼。
編輯:根據您的要求。
我在github上創建了一個小項目 ,您可以下載該項目,並查看jQuery(通常是JS)如何與JSF一起使用,這是一個實時演示 。
兩個主要文件是main.xhml和checkBoxesJQuery.js 。
希望能幫助到你。
試試這個,這可能對您幫助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代碼:
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;
}
}
您可以嘗試這樣的事情
<?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>
並且如果您的模塊是實體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 + "]";
}
}
最后
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;
}
}
更新:下面有一點勘誤
//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.