Friday, December 10, 2010

ADF 11g : How to control where a new row is inserted

On the JDeveloper forum a question was asked how to insert a new row in an ADF table after the selected row. Default behavior is insertion before the current row. In this short post I describe how to achieve this. In order to do this I create a custom method on the application module that takes the position (BEFORE or AFTER) as an argument. Based on this argument the new row is inserted at the appropriate position.

  public void createRow(String beforeOrAfter){  
Row newRow = getJobsView1().createRow();
newRow.setNewRowState(Row.STATUS_INITIALIZED);
//get instance of the above created view object
ViewObjectImpl vo=getJobsView1();
// to insert row at the end of the table
if (beforeOrAfter.equalsIgnoreCase("AFTER")){
vo.next();
vo.insertRow(newRow);
}
else
{
vo.insertRow(newRow);
}
}


I publish the method, and drop it as a Button on the page. I copied the button so I have one for each option.

  <af:panelCollection id="pc1">  
<f:facet name="menus"/>
<f:facet name="toolbar">
<af:toolbar id="t2">
<af:commandButton actionListener="#{bindings.createRowBefore.execute}"
text="create Before"
disabled="#{!bindings.createRowBefore.enabled}" id="cb1"/>
<af:commandButton actionListener="#{bindings.createRow.execute}"
text="create After"
disabled="#{!bindings.createRow.enabled}" id="cb2"/>
</af:toolbar>
</f:facet>
<f:facet name="statusbar"/>
<af:table value="#{bindings.JobsView1.collectionModel}" var="row"
rows="#{bindings.Jo .......................
........................................

The page definition contains the corresponding MethodAction. I copied the MethodAction, so I have one for each option.
   <methodAction id="createRow" RequiresUpdateModel="true"  
Action="invokeMethod" MethodName="createRow"
IsViewObjectMethod="false" DataControl="AppModuleDataControl"
InstanceName="AppModuleDataControl.dataProvider">
<NamedData NDName="beforeOrAfter" NDValue="AFTER"
NDType="java.lang.String"/>
</methodAction>
<methodAction id="createRowBefore" RequiresUpdateModel="true"
Action="invokeMethod" MethodName="createRow"
IsViewObjectMethod="false" DataControl="AppModuleDataControl"
InstanceName="AppModuleDataControl.dataProvider">
<NamedData NDName="beforeOrAfter" NDValue="BEFORE"
NDType="java.lang.String"/>
</methodAction>

When I run the page, the 'insert before' button inserts the new record before the currently selected one.

So now the 'insert after' button inserts the new record after the currently selected one.