0
votes

I need your help in enabling and disabling inputText based on rowSelectCheckbox and rowUnselectCheckbox if the checkbox is ticked or unticked. If it is ticked, then I need to enable the inputText otherwise it should be disabled on page load and on untick. By default the inputText is disabled on the page load. Here is the code for the 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>

And here is the code in the 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;
        }
       }
     }
}

The Student 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;
    }

And in the Bean, I am facing difficulties in enabling the inputText for entry if the row is ticked. So could you please help. Now I got the error : java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 if I tick and checkbox

1
<h:outputText> does not have disabled Tag Attributes. Is it inputText? - Unknown
@Unknown Sorry it was a mistake - 99maas

1 Answers

1
votes

First thing you are using selectionMode="multiple" it means there will be multiple rows with textField enabled next instead of this :

 <h:inputText value="#{emp.remarks}" disabled="#{empBean.enable}" />

write

 <h:inputText value="#{emp.remarks}" disabled="#{emp.enable}" />

means declare one variable enable in the bean itself after that:

    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);
     }
    }
  }

before to this you can write one for loop and disable all the textField for list for that will work for rowUnselect