如何 enable/disable 在 rowSelectCheckbox 和 rowUnselectCheckbox 上输入文本
How to enable/disable inputText on rowSelectCheckbox and rowUnselectCheckbox
我需要你的帮助来启用和禁用基于 rowSelectCheckbox 和 rowUnselectCheckbox 的 inputText(如果复选框被选中或未选中)。如果它被勾选,那么我需要启用 inputText 否则它应该在页面加载和取消勾选时被禁用。默认情况下,inputText 在页面加载时被禁用。这是 jsf 的代码:
<h:form id="request">
<p:dataTable value="#{dataTableView.employeeList}" id="Employee" var="emp"
selection="#{dataTableView.selectedEmployees}" rowKey="#{emp.id}">
<p:ajax event="rowSelectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:ajax event="rowUnselectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:columnGroup type="header">
<p:row>
<p:column/>
<p:column headerText="ID"/>
<p:column headerText="Name"/>
<p:column headerText="Location"/>
<p:column headerText="Remarks"/>
</p:row>
</p:columnGroup>
<p:column selectionMode="multiple" style="width:2%;text-align:center"/>
<p:column headerText="ID">
<h:outputText value="#{emp.id}"/>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{emp.name}"/>
</p:column>
<p:column headerText="Location">
<h:outputText value="#{emp.location}"/>
</p:column>
<p:column headerText="Remarks">
<h:inputText id="inputT1" value="#{emp.remarks}" disabled="#{emp.disable}"/>
</p:column>
</p:dataTable>
</h:form>
下面是 bean 中的代码:
private List<Student> employeeList = new ArrayList<Student>();
private List<Student> selectedEmployees;
private boolean disable;
@PostConstruct
public void init() {
//add Employees
disable=true;
Student w1 = new Student(111, "AAAA", "ZZZZ", "", disable);
Student w2 = new Student(222, "CCCCC", "ZZZZZ", "OUT", disable);
Student w3 = new Student(222, "BBBBBB", "YYYYYYY", "IN", disable);
employeeList.add(w1);
employeeList.add(w2);
employeeList.add(w3);
}
public void EnableInputText(SelectEvent event) {
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=employeeList.size();j++){
if(selectedEmployees.get(i).getId().equals(employeeList.get(j).getId()))
{
employeeList.get(j).setDisable(false);
break;
}
}
}
}
学生 Bean:
public class Student {
private Integer id;
private String name;
private String location;
private String remarks;
private boolean disable;
public Student(Integer id, String name, String location, String remarks, boolean disable){
this.id = id;
this.name = name;
this.location = location;
this.remarks=remarks;
this.disable=disable;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getRemarks() {
return remarks;
}
public void setDisable(boolean disable) {
this.disable = disable;
}
public boolean isDisable() {
return disable;
}
并且在 Bean 中,如果勾选该行,我在启用输入文本输入时遇到困难。那么请你帮忙。
现在我得到了错误:
java.lang.IndexOutOfBoundsException:索引:3,大小:3,如果我勾选和复选框
首先你正在使用 selectionMode="multiple"
这意味着接下来会有多行启用 textField
而不是这个:
<h:inputText value="#{emp.remarks}" disabled="#{empBean.enable}" />
写入
<h:inputText value="#{emp.remarks}" disabled="#{emp.enable}" />
表示之后在bean本身声明一个变量enable
:
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=empList.size();j++){
if(selectedEmployees.get(i).getId().equals(empList.get(j).getId()){
empList.get(j).setEnable(false);
}
}
}
在此之前,您可以编写一个 for loop
并禁用列表的所有 textField
,因为它将适用于 rowUnselect
我需要你的帮助来启用和禁用基于 rowSelectCheckbox 和 rowUnselectCheckbox 的 inputText(如果复选框被选中或未选中)。如果它被勾选,那么我需要启用 inputText 否则它应该在页面加载和取消勾选时被禁用。默认情况下,inputText 在页面加载时被禁用。这是 jsf 的代码:
<h:form id="request">
<p:dataTable value="#{dataTableView.employeeList}" id="Employee" var="emp"
selection="#{dataTableView.selectedEmployees}" rowKey="#{emp.id}">
<p:ajax event="rowSelectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:ajax event="rowUnselectCheckbox" listener="#{dataTableView.EnableInputText}" />
<p:columnGroup type="header">
<p:row>
<p:column/>
<p:column headerText="ID"/>
<p:column headerText="Name"/>
<p:column headerText="Location"/>
<p:column headerText="Remarks"/>
</p:row>
</p:columnGroup>
<p:column selectionMode="multiple" style="width:2%;text-align:center"/>
<p:column headerText="ID">
<h:outputText value="#{emp.id}"/>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{emp.name}"/>
</p:column>
<p:column headerText="Location">
<h:outputText value="#{emp.location}"/>
</p:column>
<p:column headerText="Remarks">
<h:inputText id="inputT1" value="#{emp.remarks}" disabled="#{emp.disable}"/>
</p:column>
</p:dataTable>
</h:form>
下面是 bean 中的代码:
private List<Student> employeeList = new ArrayList<Student>();
private List<Student> selectedEmployees;
private boolean disable;
@PostConstruct
public void init() {
//add Employees
disable=true;
Student w1 = new Student(111, "AAAA", "ZZZZ", "", disable);
Student w2 = new Student(222, "CCCCC", "ZZZZZ", "OUT", disable);
Student w3 = new Student(222, "BBBBBB", "YYYYYYY", "IN", disable);
employeeList.add(w1);
employeeList.add(w2);
employeeList.add(w3);
}
public void EnableInputText(SelectEvent event) {
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=employeeList.size();j++){
if(selectedEmployees.get(i).getId().equals(employeeList.get(j).getId()))
{
employeeList.get(j).setDisable(false);
break;
}
}
}
}
学生 Bean:
public class Student {
private Integer id;
private String name;
private String location;
private String remarks;
private boolean disable;
public Student(Integer id, String name, String location, String remarks, boolean disable){
this.id = id;
this.name = name;
this.location = location;
this.remarks=remarks;
this.disable=disable;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getRemarks() {
return remarks;
}
public void setDisable(boolean disable) {
this.disable = disable;
}
public boolean isDisable() {
return disable;
}
并且在 Bean 中,如果勾选该行,我在启用输入文本输入时遇到困难。那么请你帮忙。 现在我得到了错误: java.lang.IndexOutOfBoundsException:索引:3,大小:3,如果我勾选和复选框
首先你正在使用 selectionMode="multiple"
这意味着接下来会有多行启用 textField
而不是这个:
<h:inputText value="#{emp.remarks}" disabled="#{empBean.enable}" />
写入
<h:inputText value="#{emp.remarks}" disabled="#{emp.enable}" />
表示之后在bean本身声明一个变量enable
:
for(int i=0;i<=selectedEmployees.size();i++){ //Assuming you have declared as List
for(int j=0;j<=empList.size();j++){
if(selectedEmployees.get(i).getId().equals(empList.get(j).getId()){
empList.get(j).setEnable(false);
}
}
}
在此之前,您可以编写一个 for loop
并禁用列表的所有 textField
,因为它将适用于 rowUnselect