2013-05-31 26 views
-3

您好希望創建一個帶有序列號的列的數據表,並且我正在使用按鈕點擊添加行來實現datacolum動態,我需要生成一個序列號,我爲此做了什麼聊天情況下,在數據表中添加列的序列號

我的JSF頁面

<rich:dataTable value="#{section2Bean.employeeList}" 
          var="emp" style="width:100%;"> 
          <h:column> 
           <f:facet name="header"> 
           #{msg.lbl_serialNo} 
          </f:facet> 
           <h:outputText value="#{TnwrdBean.hrmsBean.hrmsSection9.serialNo}" /> 
          </h:column> 
          <h:column> 
          <f:facet name="header"> 
           #{msg.lbl_addRow} 
          </f:facet> 
           <div class="buttons"> 
           <p align="center"> 
           <h:commandButton id="addEduQualRow" type="submit" actionListener="#{section2Bean.addNewEmployee}" 
           value="+" /> 
           </p> 
           </div> 
          </h:column> 
         </rich:dataTable> 

Section2Bean.java

public class Section2Bean extends BaseAction implements Serializable { 
    private static final long serialVersionUID = 32423545435345L; 
List<Employee> employeeList; 
List<Employee> employeetrainingList; 
private boolean checkSelected; 

public List<Employee> getEmployeeList() { 
    return employeeList; 
} 

public void setEmployeeList(List<Employee> employeeList) { 
    this.employeeList = employeeList; 
} 

public void addNewEmployee(ActionEvent event) { 
    employeeList.add(new Employee(employeeList.size(), null)); 
    System.out.println(employeeList); 
    for(int i = 1;i<=employeeList.size();i++){ 
    } 
} 

public void deleteNewEmployee(ActionEvent event){ 
    employeeList.remove(employeeList.hashCode()); 
} 
@PostConstruct 
public void init() { 
    employeeList = new ArrayList<Employee>(); 
    employeetrainingList =new ArrayList<Employee>(); 
    employeeList.add(new Employee(1, "")); 
} 

public Section2Bean() { 
} 

public boolean isCheckSelected() { 
    return checkSelected; 
} 

public void setCheckSelected(boolean checkSelected) { 
    this.checkSelected = checkSelected; 
} 
} 
+0

你有什麼試圖生成序列號?你在期待什麼? –

+0

我需要添加序列號到第一列,但我的代碼反映在所有列中,因爲它的整數,我想我應該可能添加一個arraylist –

+0

您可以發佈Employee類嗎? – Adarsh

回答

0

你的數據表

<h:column rendered="#{section2Bean.showSerials}"> 
    #{section2Bean.getSerial(emp)} 
</h:column> 

你豆

import java.util.UUID; 

private Map<Employee, String> serials; 
private boolean showSerials; 
// getter 

@PostConstruct 
public void init() { 
    serials = new HashMap<>(); 
    for(Employee employee : ...) { 
     serials.put(employee, newSerial()); 
    } 
} 

public String getSerial(Employee employee) { 
    return serials.get(employee); 
} 

public void buttonClick(ActionEvent event) { 
    // other logic? 
    showSerials = true; 
} 

private String newSerial() { 
    return UUID.randomUUID().toString(); 
} 
+0

我試過,但沒有工作,我應該在初始化方法中迭代員工列表 –

+0

是的,通常你會從另一個支持bean中檢索這些,雖然這不是你的例子。 – Aquillo

0

聲明一個新的領域,你bean來持有的序列號。當你用bean的實例填充List時,每次給它一個遞增的值。因此,在您的jsf中,您可以直接訪問序列號屬性。

+0

我試過了,但是它反映在序列號爲 –

+0

@ user1858826的所有值中,表示相同的值會反映在所有行中? – Adarsh

+0

是的,它被反映在所有行中 –