Adding a row to a data table using ArrayDataModel in jsf?

How to add row to datatable and add new data to table using ArrayDataModel?

+3
source share
1 answer

You can do this in a managed 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;
    }

}
+2
source

Source: https://habr.com/ru/post/1702903/


All Articles