Tuesday, October 14, 2008

Add new row in ADF table on button click

Add new row in ADF table on button click – [JDeveloper Version 10.1.3]
In this example we will add a new row at last position in an ADF table. I am taking an example of Employee table in HR schema. We need this approach if we want to create an empty row in ADF table and then initialize it with some values or to add the empty row at the end of the table.

Step 1) Create a new Employee View Object(say EmpViewObjEOBased) based on an Employee Entity object.

Step 2) Add the above view object in your application module.

Step 3) Drop it on your page as a ADF table (not as ADF readonly table).

Step 4) Add the following code inside your application module class.


public void createRow() {
Row newRow = getEmpViewObjEOBased1().createRow();
newRow.setNewRowState(Row.STATUS_INITIALIZED);
//get instance of the above created view object
ViewObjectImpl vo=getEmpViewObjEOBased1();
// to insert row at the end of the table
vo.insertRowAtRangeIndex(vo.getRangeSize()-1,newRow);
System.out.println(getEmpViewObjEOBased1().getCurrentRowIndex());
}

If you want to insert the row at the begening of the table replace line vo.insertRowAtRangeIndex(vo.getRangeSize()-1,newRow); with
vo.insertRow(newRow);

Step 5) Expose this application module method to client.




This will expose the method to be used in UI and method will appear in the data control.

Step 6) Drop the createRow button inside the “Table facets->action” as a ADF command button.


Now run your page and check the result...

2 comments:

Aditi Gupta said...

Wow very simple code.. How did you know that You have to use these functions and have to proceed through these steps.. I mean how can I know.. I am new to ADF so want to learn it through correct path. Will be very grateful to you.Thanks.

-- Aditi

Vikram Kohli said...

Thanks for reading the blog. What help do you need. Please read the blog post http://kohlivikram.blogspot.com/2009/07/oracle-adf-i-am-beginner-from-where-i.html

Regards,
Vikram