Wednesday, April 21, 2010

Inserting new rows as last row of table

By Default when you add a new row on table (or view object) it is added before the current selected row.

End user requirement was that when we press Insert the new row should be added as last row no matter which is the current row.

This is implemented by overriding the View Object method insert(row).
* Insert new Rows at the end of RowSet.
* @param row
public void insertRow(Row row) {
//go to the end of Rowset if it has rows
Row lastRow = this.last();
if (lastRow != null) {
//insert new row at the end and make it current
int indx = this.getRangeIndexOf(lastRow) + 1;
this.insertRowAtRangeIndex(indx, row);
} else { // empty Rowset

Note: This could cose performance issue when inserting new rows on very large tables.

Don’t forget in table components to always set displayRow="selected" so that the new row to be visible no matter how many rows there are.

Test Case:


  1. I'm having a problem in with the above. If I do the following:
    1. Add a new row
    2. Add some dummy values to the row
    3. Delete the row (delete button with immediate=true, partialSubmit=true)
    4. Add another new row

    Step 4 shows the old dummy values from the row created in step 1 & 2. Any ideas how I can get around this? I need partialSubmit=true on the delete button so that my dialog does not disappear, and I need immediate=true on the delete button so that I can skip validation rules when deleting a row.

    The primary key for my row is calculated based on the number of existing rows, so when I delete and re-add, the same primary key will be used. Could this be the cause?

    Great blog btw - invaluable resource!

  2. Does this happen only when you use this workaround to add rows at the end?
    It does not seem to be related.
    Anyway it is not a good practice to have primary key calculated like that. I would suggest to use a sarogate pk (i.e. sequence) and you can still have your calculated key as a unique key to be visible for the user.

  3. Hi,
    Your method doesn't works in all case. I use this method which seems to work better :
    public void insertRow(Row pRow) {
    Row lastRow = this.last();
    if (lastRow != null) {
    RowSetIterator iter = this.getDefaultRowSet(); iter.insertRowAtRangeIndex(iter.getRangeSize(), pRow);
    } else { // empty Rowset

  4. I try this and it works, in fact I used this many times, but in in one I get this message: indexRange, I found this:
    vo.last(); //Navigate to the end of the rowset; //Go to the record after the last one, you wont get an error unless you now try and reference vo.getCurrentRow.getAttr..... etc... which will return null pointer
    Row row = vo.createRow(); //create your row
    vo.insertRow(row); //insert it

    // Required per OA Framework Model Coding Standard M69

    And it works good.

  5. Hi ,

    I am able to create a row always at the last in the table.But now my requirement is to create a row below the selected record in the table.
    Any suggestions?

  6. Hi,
    If I'm using paging with a rangeSize of 10 yet have > 10 records already existing in the table. When the user clicks the Create button how do I get the table to display the last page (where the new row has been created).

    I know I can do it declaratively with the table "first", but can I do it programatically?

  7. Hi
    I want to add row to table with java code (in managed bean not in application module or view object)
    please send to:

  8. This could cose performance issue when inserting new rows on very large tables.

    How to prevent the query execution due to vo.last().

    please send it on