Delete a Row in ADF table of EJB binding.

This is a general requirement for every updatable ADF table. We have couple of problems while deleting using ADF Delete data control.

Problem1: The ADF Delete binding always removes the first Row in a table.

Problem2: We have a Add button, when a new Row is added to a table, that might not be stored to database so the Delete should happen in two cases one for the newly added row and second for the Existing Row(row from the DB).

Solution: Write the below code to the delete action. (Note: adf table rowSelection will be single)

public String cbDelete_action() {

// Add event code here…

FacesContext ctx = FacesContext.getCurrentInstance();

ExpressionFactory ef = ctx.getApplication().getExpressionFactory();

// PlanDetails is the data controller for the table

ValueExpression ve =

ef.createValueExpression(ctx.getELContext(), “#{bindings.getPlanDetailsIterator.currentRow.dataProvider}”,

PlanDetailsView.class);

PlanDetailsView data =

(DetailsView)ve.getValue(ctx.getELContext());

//planTable is the binding value for the table and using this get the selected RowData

JUCtrlHierNodeBinding rowData =

(JUCtrlHierNodeBinding)planTable.getSelectedRowData();

// Removing the Row from the Model (ADF Table)

rowData.getRow().remove();

AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();

// Refresh the ADF table

adfFacesContext.addPartialTarget(planTable);

try {

// Id is the unique id for the table, it will be populated when it got saved. If the ID is not null we need to remove from the DB. sessionMangaer.removeData is a method of the session bean which invokes the actual deletion of the Record.

if (data.getId() != null) {

sessionManager.removeData(data);

}

} catch (Exception e) {

logger.debug(“inside exception” + e.toString());

}

return null;

}