Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为datatable中的列添加序列号_Java_Jsf_Jsf 2 - Fatal编程技术网

Java 为datatable中的列添加序列号

Java 为datatable中的列添加序列号,java,jsf,jsf-2,Java,Jsf,Jsf 2,嗨,我想创建一个带有序列号的列的数据表,我正在通过点击按钮添加行来动态创建数据列,我需要生成一个序列号,我在这种情况下会做什么 我的jsf页面 <rich:dataTable value="#{section2Bean.employeeList}" var="emp" style="width:100%;"> <h:column>

嗨,我想创建一个带有序列号的列的数据表,我正在通过点击按钮添加行来动态创建数据列,我需要生成一个序列号,我在这种情况下会做什么

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

您的数据表

你的豆子


在bean中声明一个新字段来保存序列号。当您用bean实例填充列表时,每次给它一个递增的值。因此,在jsf中,您可以直接访问serial number属性。

您尝试生成的序列号是什么?你在期待什么?我需要在第一列添加序列号,但是我的代码会反映在所有列中,因为它是一个整数,我想我可能应该添加一个ArrayList。你也可以发布Employee类吗?我尝试过,但它会反映在serial的所有值中number@user1858826你的意思是相同的值反映在所有行中?是的,它反映在所有行中。我可以指出你在哪里如果你能分享那段代码,那就错了。我试过了,但没用,我应该在init method中迭代雇员列表吗。
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;
}
}
<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();
}