Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a row to data table using ArrayDataModel in jsf?

Tags:

java

jsf

How can i add a row to datatable and add new data to the table using ArrayDataModel?

like image 409
Warrior Avatar asked Dec 20 '25 09:12

Warrior


1 Answers

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

}
like image 150
McDowell Avatar answered Dec 22 '25 23:12

McDowell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!