Wednesday, April 4, 2018

Insert a new row at the end in af:table

keep below code at voimpl
import oracle.jbo.Row;

    public void insertRow(Row row){
      Row lastRow = this.last();
      if (lastRow != null){
        int range = this.getRangeIndexOf(lastRow) +1;
        this.insertRowAtRangeIndex(range, row);
        this.setCurrentRow(row);
       }else{
        super.insertRow(row);
       }
    }

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


There are three main steps
  1. First add clientListener and serverListener to last column of ADF table.<af:column sortproperty="#{bindings.EmployeesView1.hints.LastName.name}" sortable="false" headertext="#{bindings.EmployeesView1.hints.LastName.label}" id="c3">    <af:inputtext value="#{row.bindings.LastName.inputValue}" label="#{bindings.EmployeesView1.hints.LastName.label}" required="#{bindings.EmployeesView1.hints.LastName.mandatory}" columns="#{bindings.EmployeesView1.hints.LastName.displayWidth}" maximumlength="#{bindings.EmployeesView1.hints.LastName.precision}" shortdesc="#{bindings.EmployeesView1.hints.LastName.tooltip}" id="it1" autosubmit="true">                        <af:clientlistener method="tabkeypressed" type="keyPress">   <af:serverlistener type="tabpress" method="#{pageFlowScope.TestBean.tabPressed}">      <f:validator binding="#{row.bindings.LastName.validator}">    </f:validator></af:serverlistener></af:clientlistener></af:inputtext>  </af:column>  
  2. 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.
  3. function tabkeypressed(evt){     
  4.     var _keyCode = evt.getKeyCode();  
  5.      var comp = evt.getSource();  
  6.  if (_keyCode == AdfKeyStroke.TAB_KEY ){     
  7.         AdfCustomEvent.queue(comp, "tabpress",{}, true);           
  8.     evt.cancel();  
  9.   }  
  10.  } 
  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.

  1. public void tabPressed(ClientEvent clientEvent) {  
  2.     BindingContext bindingctx=BindingContext.getCurrent();  
  3.     BindingContainer binding=bindingctx.getCurrentBindingsEntry();  
  4.     DCBindingContainer bindingsImpl = (DCBindingContainer) binding;  
  5.     DCIteratorBinding dciter = bindingsImpl.findIteratorBinding("EmployeesView1Iterator");  
  6.     ViewObject vo=dciter.getViewObject();  
  7.    //Return current row index no.  
  8.     int index=vo.getCurrentRowIndex()+1;  
  9.   //get the last row no.  
  10.     long cnt=vo.getEstimatedRowCount();  
  11.     if(cnt==index)  
  12.     {  
  13.         createRow();  
  14.    AdfFacesContext.getCurrentInstance().addPartialTarget(emptab);  
  15.         }  
  16.   
  17. }  
  18.   
  19.   
  20. public void createRow()  
  21. {  
  22.     
  23.                 //Get ViewObject  
  24.                 BindingContext bindingctx=BindingContext.getCurrent();  
  25.                 BindingContainer binding=bindingctx.getCurrentBindingsEntry();  
  26.                 DCBindingContainer bindingsImpl = (DCBindingContainer) binding;  
  27.                 DCIteratorBinding dciter = bindingsImpl.findIteratorBinding("EmployeesView1Iterator");  
  28.                  
  29.                 //Get current data RowSetIterator  
  30.                 RowSetIterator rsi =dciter.getRowSetIterator();  
  31.                 //Get last Row of current Iterator  
  32.                 Row lRow = rsi.last();  
  33.                 //Get index of the last row  
  34.                 int lrIndex = rsi.getRangeIndexOf(lRow);  
  35.                 //Create a new row  
  36.                 Row nRow = rsi.createRow();  
  37.                 //Initialize that row  
  38.                 nRow.setNewRowState(Row.STATUS_INITIALIZED);  
  39.                 //Add row in last of current rowset  
  40.                 rsi.insertRowAtRangeIndex(lrIndex + 1, nRow);  
  41.                 //Set newly created row as current row  
  42.                 rsi.setCurrentRow(nRow);  
  43.                     }  

No comments:

Post a Comment