2016-04-21 21 views
0

禁用的inputText我需要你在啓用和基於rowSelectCheckbox和rowUnselectCheckbox如果該複選框被選中或取消選中禁用的inputText幫助。如果打勾,那麼我需要啓用inputText,否則它應該在頁面加載和取消時被禁用。默認情況下,在頁面加載時禁用inputText。下面是JSF代碼:如何啓用/ rowSelectCheckbox和rowUnselectCheckbox

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

和JavaBean,我面對如果行被勾選,則啓用inputText以便輸入時遇到困難。所以你可以請幫忙。 現在,我得到了錯誤: java.lang.IndexOutOfBoundsException:指數:3,大小:3,如果我打勾和checkbox你使用selectionMode="multiple"

+0

工作的沒有禁用標記屬性。它是inputText嗎? – Unknown

+0

@Unknown對不起這是一個錯誤 – 99maas

回答

1

第一件事就意味着會有多行文本框與未來,而不是 啓用這樣的:

<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

+0

這是給我上的getId錯誤,等於它是在你的代碼指出的getId方法沒有找到和equals方法的第三行中找不到以及 – 99maas

+0

更新'爲loop'請現在就試試吧,那個錯誤即將到來,因爲我錯過了for循環中的一件事情。並且在你的bean中應該有字段'Integer id'和setters和getters。 – techipank

+0

我得到的java.lang.IndexOutOfBoundsException一個錯誤:指數:3,大小:3 – 99maas