How to enable/disable inputText on rowSelectCheckbox

UI Components for JSF
Post Reply
maas
Posts: 29
Joined: 26 Feb 2015, 17:46

22 Apr 2016, 13:57

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:

Code: Select all

 <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 in the backing bean:

Code: Select all

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

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

25 Apr 2016, 07:40

Its a basic java question.You are trying to access index3 which doesn't exist.It should be i<size but not i<=size.
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 32 guests