【问题标题】:How to enable/disable inputText on rowSelectCheckbox and rowUnselectCheckbox如何在 rowSelectCheckbox 和 rowUnselectCheckbox 上启用/禁用 inputText
【发布时间】:2016-04-21 21:06:16
【问题描述】:

如果选中或未选中复选框,我需要您的帮助来启用和禁用基于 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;
        }
       }
     }
}

学生豆:

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 中,如果选中该行,我在启用 inputText 以进行输入时遇到困难。所以请你帮忙。 现在我得到了错误: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 if I tick and checkbox

【问题讨论】:

  • 没有禁用标签属性。是inputText吗?
  • @Unknown 对不起,这是一个错误

标签: jsf jsf-2 primefaces


【解决方案1】:

你使用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

【讨论】:

  • 它在 getId 和 equals 上给了我一个错误,它位于代码的第三行,说明找不到 getId 方法并且也找不到 equals 方法
  • 已更新 for loop 请立即尝试,因为我错过了 for 循环中的一件事,所以该错误即将到来。在你的 bean 中应该有字段 Integer id 和需要的 setter 和 getter。
  • 我在 java.lang.IndexOutOfBoundsException 中遇到错误:索引:3,大小:3
  • 也请发Student bean
  • 并在此行后添加breakempList.get(j).setEnable(false);
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2016-08-16
相关资源
最近更新 更多