Wednesday 11 September 2013

How to select current row in adf table and perform delete operation

ADF table component is used to display the VO records in tabular form. It has its own many in-built features such as sorting, filtering and we can add other features like detach, manage columns, exporting data in excel file etc using af:panelCollection and toolbar facet component.

It also have row selection both single/multiple. Row selection is very important in respect of performing operations of particular rows.

In this blog I will show how we can get the current row of the table, fetching the primary key of the record and performing the delete operation using java code.

Let us assume our table is having a binding attribute mapped to a bean class and having rowSelection attribute set to "single".

e.g : rowSelection="single" binding="#{pageFlowScope.department.departmentTable}"

We will add a column in the table, which will have a command button and a actionListener mapped to bean class.

e.g : <af:column headerText="Action" id="c5">
            <af:commandButton text="Delete" id="cb1"
                              actionListener="#{pageFlowScope.department.deleteRecord}"/>
          </af:column>

Java Code snippet :

    public void deleteRecord(ActionEvent actionEvent) {
//Fetch current row data as per index
        FacesCtrlHierNodeBinding rowdata =            (FacesCtrlHierNodeBinding)departmentTable.getRowData(departmentTable.getRowIndex());
 //getting row and typecasting in RowImpl
        DeptVORowImpl rowImpl = (DeptVORowImpl)rowdata.getRow();
//Getting primary key of the record
        Number keyVal = rowImpl.getDepartmentId();
        DCBindingContainer dcBindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
//Iterator binding on the page          
 DCIteratorBinding iteratorBinding = (DCIteratorBinding)dcBindings.get("DeptVOIterator");
        RowSetIterator rowSetIterator = iteratorBinding.getRowSetIterator();
        Key key = new Key(new Object[] { keyVal });
        Row row = rowSetIterator.findByKey(key, 1)[0];
        rowSetIterator.setCurrentRow(row);
//Removing the current record
rowSetIterator.removeCurrentRow();
}

After delete perform AM commit operation.

*Comments and feedback are most welcomed, Thanks.

1 comment: