在datatable中添加列的序列号

时间:2013-05-31 06:03:24

标签: java jsf jsf-2

您想创建一个带有序列号的列的数据,并且我通过在按钮点击时添加行来使数据库动态化,我需要生成序列号,我为此场景做了什么聊天,

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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

在bean中声明一个新字段以保存序列号。当您使用bean的实例填充List时,每次都给它一个递增的值。所以在你的jsf中,你可以直接访问序列号属性。