ADF 11g R2 : Using the ActiveRowKey property

In ADF 11g Release 2, the ADF Table component has a property called ‘ActiveRowKey’. According to documentation, this represents the row that is currently active on the client. In click-to-edit mode, the active row will be made editable and is brought into view (if not already visible). Upon initial display, the click-to-edit component defaults the active row to the first visible row. In this post I will show you how to use the activeRowKey programmatic

Use Case.

Imagine the following use case: I have a ‘click-to-edit’ table and I added a ‘filter’ field with a button in the surrounding panel collection. User enters (in this case) a last name and pushes the button. The table scrolls to the first row that matches the criteria, and the row is active to edit.

Implementation.

I create an ADF table based on ADF business components for the Employees table.
It’s a clickToEdit table with singleSelection. After creating it I bind it to a bean. I’ll show later why I need to do that. The table code looks like this:

             <af:table value="#{bindings.Employees1.collectionModel}" var="row"
                      rows="#{bindings.Employees1.rangeSize}"
                      emptyText="#{bindings.Employees1.viewable ? 'No data to display.' : 'Access Denied.'}"
                      fetchSize="#{bindings.Employees1.rangeSize}"
                      rowBandingInterval="0"
                      selectionListener="#{bindings.Employees1.collectionModel.makeCurrent}"
                      rowSelection="single" id="t1" editingMode="clickToEdit"
                      selectedRowKeys="#{bindings.Employees1.collectionModel.selectedRow}"
                      binding="#{pageFlowScope.empsBean.empTable}">
              <af:column ....................

Next I surround it with a panel collection and I turn off a lot of features (also new in R2) of the panel collection. In the toolbar facet I add an inputText component and a commanfbutton. The inputtext will hold the search value, and the button invokes logic to scroll the table and make to row active.

           <af:panelCollection id="pc1" featuresOff="viewMenu detach" >
            <f:facet name="menus"/>
            <f:facet name="toolbar">
            <af:toolbar id="t2">
            <af:group id="g1">
            <af:inputText label="enter last name" value="#{pageFlowScope.empsBean.searchValue}" id="it11"/>
            <af:commandToolbarButton  text="goto employee" id="ctb1"
                                     actionListener="#{pageFlowScope.empsBean.gotoPressed}"/>
            </af:group>
            </af:toolbar>
            </f:facet>

In the actionListener I first get a hold of the RowSetItetator. For each and every row in the rowSetIterator, I check if it matches the entered search string. I do that in a separate method called matchFound() (see line 8).

   public void gotoPressed(ActionEvent actionEvent) {
    // Add event code here...
    DCIteratorBinding it = ADFUtils.findIterator("Employees1Iterator");
    RowSetIterator rsi = it.getRowSetIterator();
    RowKeySet oldSelection = empTable.getSelectedRowKeys();

    if (rsi.first() != null) {
      Row r = rsi.first();
           while (rsi.hasNext() && getKey() == null && (!matchFound(r,oldSelection))) {
                   r = rsi.next();
      }
    }
  }

In the matchFound() method I compare the value of the LastName attribute with the value entered by the user (see line 7). If it is a match there are two more things I need to do. First I add the key of the row found to be the activeRowKey (see line 11). Finally I need to make the row current. If I don’t do this, it will still work, but by making the row current, it just looks a lot nicer. The trick I use here (see line 13) is a method (line 19) that I borrowed from Frank Nimphius’ blog.

   private boolean matchFound (Row r,RowKeySet oldSelection){
    setKey(null);
    ArrayList lst = new ArrayList(1);
    RowKeySetImpl newSelection = new RowKeySetImpl();
    Key key = null;
    String rowValue = (String)r.getAttribute("LastName");
    if (rowValue.toString().contains(searchValue)) {
      System.out.println("now setting key to " + key);
      key = r.getKey();
      lst.add(key);
      empTable.setActiveRowKey(lst);
      newSelection.add(lst);
      makeCurrent(empTable, newSelection, oldSelection);
      return true;
    }
    return false;
  }

  private void makeCurrent(RichTable empTable, RowKeySet newCurrentRow, RowKeySet oldCurrentRow) {
    //To make a row current, we need to create a SelectionEvent which
    //expects the following arguments: component, unselected_keys,
    //selected_keys. In our example, we don't have unselected keys and
    //therefore create an empty RowSet for this
    SelectionEvent selectionEvent =
      new SelectionEvent(oldCurrentRow, newCurrentRow, empTable);
      selectionEvent.queue();
      AdfFacesContext.getCurrentInstance().addPartialTarget(empTable);
  }

The works.

Start the application, type some searchText, push the button and of you go.
Before
The row is found, and immediate available for edit.
After

Resources.

ADF Code Corner Oracle JDeveloper OTN Harvest
The workspace for this post.

One Response

  1. saravnana October 8, 2014