使用jsf中的ArrayDataModel向数据表添加行?

时间:2009-02-09 07:23:20

标签: java jsf

如何向datatable添加行并使用ArrayDataModel向表中添加新数据?

1 个答案:

答案 0 :(得分:2)

您可以在托管bean中执行此操作:

public class ArrayDataBean {

    private Object[] rows = { "One", "Two", "Three" };

    private final DataModel dataModel = new ArrayDataModel(rows);

    /** Bind to dataTable value */
    public DataModel getDataModel() {
        return dataModel;
    }

    /** Bind to command control action */
    public String addRow() {
        Object[] newArray = new Object[rows.length + 1];
        System.arraycopy(rows, 0, newArray, 0, rows.length);
        newArray[rows.length] = "NewRow" + System.currentTimeMillis();

        rows = newArray;
        dataModel.setWrappedData(rows);

        // return navigation rule, if any
        return null;
    }

}