Creating new row on pressing “TAB” key in the last cell of Table.

Creating new row on pressing “TAB” key in the last cell of Table.

Someone asked in oracle community forum, How to create new row on pressing tab key in table’s last cell? So here is complete example with sample code (jdev 11.1.1.9.0). I’m using HR Schema for sample application.

There are three main steps

  1. First add clientListener and serverListener to last column of ADF table.
          
          
            
                                
           
              
            
          
          
  1. Following java script method is called when user press any key on last table cell’s text field. In java script code we are identifying the TAB key, if user presses “TAB” key then managed bean method is called using server listener.
     function tabkeypressed(evt){   
          var _keyCode = evt.getKeyCode();
          var comp = evt.getSource();
      if (_keyCode == AdfKeyStroke.TAB_KEY ){   
        
         AdfCustomEvent.queue(comp, "tabpress",{}, true);         
         evt.cancel();
        }
      }
  

  1. Managed bean method checks whether current row is the last row or not. If current row is last row of the table, then it will create a new row at the end of the table.
    public void tabPressed(ClientEvent clientEvent) {
        BindingContext bindingctx=BindingContext.getCurrent();
        BindingContainer binding=bindingctx.getCurrentBindingsEntry();
        DCBindingContainer bindingsImpl = (DCBindingContainer) binding;
        DCIteratorBinding dciter = bindingsImpl.findIteratorBinding("EmployeesView1Iterator");
        ViewObject vo=dciter.getViewObject();
       //Return current row index no.
        int index=vo.getCurrentRowIndex()+1;
      //get the last row no.
        long cnt=vo.getEstimatedRowCount();
        if(cnt==index)
        {
            createRow();
       AdfFacesContext.getCurrentInstance().addPartialTarget(emptab);
            }

    }


    public void createRow()
    {
      
                    //Get ViewObject
                    BindingContext bindingctx=BindingContext.getCurrent();
                    BindingContainer binding=bindingctx.getCurrentBindingsEntry();
                    DCBindingContainer bindingsImpl = (DCBindingContainer) binding;
                    DCIteratorBinding dciter = bindingsImpl.findIteratorBinding("EmployeesView1Iterator");
                   
                    //Get current data RowSetIterator
                    RowSetIterator rsi =dciter.getRowSetIterator();
                    //Get last Row of current Iterator
                    Row lRow = rsi.last();
                    //Get index of the last row
                    int lrIndex = rsi.getRangeIndexOf(lRow);
                    //Create a new row
                    Row nRow = rsi.createRow();
                    //Initialize that row
                    nRow.setNewRowState(Row.STATUS_INITIALIZED);
                    //Add row in last of current rowset
                    rsi.insertRowAtRangeIndex(lrIndex + 1, nRow);
                    //Set newly created row as current row
                    rsi.setCurrentRow(nRow);
                        }

About Manish Pandey

Manish is a Project Leader at Jagran Prakashan Ltd, focused on Jdeveloper Fusion Web Application(specifically Oracle ADF). He shares his knowledge through the OTN Jdev/ADF Forum. He has written more than 50 article about Oracle ADF and Jdeveloper.

2 comments

  1. Thank you very much. I tried the same code it is working fine but cursor focus lost after the row is created.

Leave a Reply