【发布时间】: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