How to control the location of the new row in af:table

Environment (JDeveloper 11.1.2.0.0, ADF BC, ADF Faces, hr schema)

When we add a new row in af:table,the default behavior is that the new row is added before the current row. Sometimes we may need to change this behavior. In this post I will show how we can add new row at different locations within our table, and automatically scrolling to that new row.

This post is based on hr schema (EMPLOYEES table) and assumed that you have already built your BC. The button used to add a new row has its PartialSubmit property set to true, and the table has its PartialTrigger property references the button’s id, also our table should have its DisplayRow property set to selected, the table binding in the managed bean is empTable (setEmpTable(RichTable empTable), getEmpTable()).

  1. To add a new row at the first location on the table use this method:   
    public String addRowAtFirst() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    nav.insertRowAtRangeIndex(0, newRow);        dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  2. To add a new row at the end of the table use this method:   
    public String addRowAtLast() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    Row lastRow = nav.last();
    int lastRowIndex = nav.getRangeIndexOf(lastRow);
    nav.insertRowAtRangeIndex(lastRowIndex+1, newRow);
    dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  3. To add a new row before the current selected row (default behavior) use this method:   
    public String addRowBefore() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    nav.insertRow(newRow);        dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  4. To add a new row after the current selected row use this method:   
    public String addRowAfter() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    Row currentRow = nav.getCurrentRow();
    int currentRowIndex = nav.getRangeIndexOf(currentRow);
    nav.insertRowAtRangeIndex(currentRowIndex+1, newRow);
    dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }

7 Comments

Filed under ADF

7 responses to “How to control the location of the new row in af:table

  1. Alok

    please help me how along with new row i can add some components coming from data controls which are web service based.

  2. Hi Mohammad,
    Thank you for the post!
    I need to know one thing that when i am inserting a empty row into emp table,it should delete immediately.but when i am trying to delete the row validation get fire.how can i able to delete?if not what are the reasons?

    Please give me your suggestion!!

    Thanks
    Amar

    • Mohammad Jabr

      Hello Amar,
      How do you delete the row?Are you using a button? if yes is the immediate property for the button set to true?

  3. Shyam

    Hi Mohammed,

    I get a NullPointerexception after I click the button at JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();

Leave a reply to Mohammad Jabr Cancel reply