This discussion is archived
7 Replies Latest reply: Jul 13, 2012 4:44 AM by adfLearner RSS

Moving the row of the table up/down/start/end

782792 Newbie
Currently Being Moderated
I have to create component for the table which should move the row one level up, one level down, at the start or end by clicking on these images( >| >> << |<).


Currently this is what I am doing-- Instead of the above requirements I am trying to achieve this functionality by allowing the user to drag and drop the row where ever he wants on the same table.

I have used the collectionDragSource and collectionDropTarget on the table

<af:table id="t1">
<af:column></af:column>
<af:collectionDragSource modelName="dragRow"/>
<af:collectionDropTarget modelName="dragRow" dropListener="#{DragAndDrop.dragAndDrop}"/>
</af:table>

Managed Bean

public DnDAction dragAndDrop(DropEvent dropEvent) {
int souceRowIndex, targetRowIndex = 0;
Row sourceRow, targetRow = null;
return DnDAction.MOVE;
}

I got the souceRowIndex, targetRowIndex ,sourceRow, targetRow but unable to interchange the row.

Please suggest how to move or interchange the row using rowIndex.
Is there any other way to achieve this functionality.
  • 1. Re: Moving the row of the table up/down/start/end
    767361 Journeyer
    Currently Being Moderated
    Hi Vijay:
    Here is what I did in the past to get it working:
    1) in your jsf page, within your <af:table> thinking of adding one column (such column might need to be added in your database table as well, say: displayOrder number):
    <af:column id="c7" sortProperty="DisplayOrder" sortable="false"
    headerText="Order" rowHeader="unstyled">
    <af:commandLink text="" id="cl1"
    disabled="#{row.bindings.DisplayOrderInt.inputValue==1}"
    actionListener="#{YourBean.upArrowClicked}">
    <af:image source="/images/arrow_up.gif" shortDesc="Move Up"
    id="img_upArrow"/>
    </af:commandLink>
    <af:commandLink text="" id="cl2"
    disabled="#{row.bindings.DisplayOrderInt.inputValue==bindings.FinancialInfoView1.estimatedRowCount}"
    actionListener="#{YourBean.downArrowClicked}">
    <af:image source="/images/arrow_down.gif" shortDesc="Move Down"
    id="img_dnArrow"/>
    </af:commandLink>
    <%--
    <input type="image" src="images/arrow_up.gif" name="Up" value="UP"/>
    --%>
    </af:column>

    2) in your backing bean, add the event handler logic within upArrowClicked and downArrowClicked respectively:
    public void upArrowClicked(ActionEvent actionEvent) {
    // Add event code here...
    if (actionEvent == null)
    return;
    final FinancialInfoViewImpl financialInfoView =
    (FinancialInfoViewImpl)getViewObject(FINANCIAL_MODULE,
    FINANCIAL_INFO_VIEW);
    if (financialInfoView == null)
    throw new JboException(CANNOT_FIND_VIEW + FINANCIAL_INFO_VIEW);
    final String financialIteratorName = "FinancialInfoView1Iterator";
    final DCIteratorBinding financialIterator =
    (DCIteratorBinding)getBindings().get(financialIteratorName);
    if (financialIterator == null)
    throw new JboException(CANNOT_FIND_ITERATOR + financialIterator);
    final int rowIndex = financialIterator.getCurrentRowIndexInRange();
    final FinancialInfoViewRowImpl currentRow =
    (FinancialInfoViewRowImpl)financialIterator.getCurrentRow();
    final FinancialInfoViewRowImpl previousRow =
    (FinancialInfoViewRowImpl)financialIterator.getRowAtRangeIndex(rowIndex -
    1);
    final int currentOrder = currentRow.getDisplayOrder().intValue();
    final int previousOrder = previousRow.getDisplayOrder().intValue();
    currentRow.setDisplayOrder(new Number(previousOrder));
    previousRow.setDisplayOrder(new Number(currentOrder));
    //executeOperation("Commit");
    executeOperation1("Commit");
    financialIterator.executeQuery();
    financialInfoView.executeQuery();
    setIteratorSelectedRowIndex(financialIterator, rowIndex - 1);
    }

    Please mark this as the 'Correct' or 'Helpful' answer if it is.

    Thanks,
    Alex
  • 2. Re: Moving the row of the table up/down/start/end
    John Stegeman Oracle ACE
    Currently Being Moderated
    @Alexbie - Just a warning.... your sample code will commit any pending transactions, which is a side effect the OP may not wish. Also, the question isn't about using buttons to move the row (she/he already has that) but how to drag-and-drop rows.

    @Vijaya - [url http://forums.oracle.com/forums/thread.jspa?threadID=1086157]Here is a post on dragging-and-dropping nodes in a tree which may give you some ideas.

    John
  • 3. Re: Moving the row of the table up/down/start/end
    782792 Newbie
    Currently Being Moderated
    Hi John,

    True, As per Alexbie "Commit" is going to get the user trouble. Even I can not not have new column added in everytable where this functionality is needed -

    I have already seen the link provided by you but not able to move further. I have sourceRow, tagretRow, sourceRowIndex, targetRowIndex but unable to move further -

    Code -
    public class DragAndDrop {
    public DnDAction dragAndDrop(DropEvent dropEvent) { // dropSite will be null if dropped on the table but not on a row

    RichTable richTable = null;
    int souceRowIndex, targetRowIndex = 0;
    Row sourceRow, targetRow = null;
    richTable = (RichTable)dropEvent.getDragComponent();


    DataFlavor<RowKeySet> df = DataFlavor.getDataFlavor(RowKeySet.class);
    RowKeySet droppedValue = dropEvent.getTransferable().getData(df);
    Object[] keys = droppedValue.toArray();
    oracle.jbo.domain.Number[] empKeys =
    new oracle.jbo.domain.Number[keys.length];
    for (int i = 0; i < keys.length; i++) {
    List list = (List)keys;
    richTable.setRowKey(list);
    souceRowIndex = richTable.getRowIndex();
    FacesCtrlHierNodeBinding rwData = (FacesCtrlHierNodeBinding)richTable.getSelectedRowData();
    sourceRow = rwData.getRow();
    }

    List<Key> serverRowKey = (List<Key>)dropEvent.getDropSite();
    // // This time we use the tree itself to make it's current row that of the server row key (ie. the destination)
    richTable.setRowKey(serverRowKey);
    FacesCtrlHierNodeBinding rwData =
    (FacesCtrlHierNodeBinding)richTable.getSelectedRowData();

    // // And we retrieve that row's index
    targetRowIndex = richTable.getRowIndex();
    targetRow = rwData.getRowAtRangeIndex(targetRowIndex);
    return DnDAction.MOVE;

    }

    }
  • 4. Re: Moving the row of the table up/down/start/end
    767361 Journeyer
    Currently Being Moderated
    Hi John:
    Thanks for pointing out the not very correct usage of "commit" action. I was running into page refresh issue and "commit" can do that for me. I didn't take time to figure out why I had to do a "commit". But I am pretty sure it's because I had the 'displayOrder' column mapped to a database column. It can be something like transient, but the problem is next time when user log into system, the sorted records will lost the order you did last time. So such 'displayOrder' column has to be in database.
    Slight objection, Vijay didn't mention he has to use 'drag and drop'. Actually he said he want it work using image buttons. That's what exactly I was doing and it was working. The only flaw in my example is the "commit" action to refresh the page. And Vijay can try to executeQuery on the VO to see if it refreshes the page which is a bit better. Or he can try to embed a few 'javascript' codes to refresh the whole page or try harder on partial triggers.

    Thanks,
    Alex
  • 5. Re: Moving the row of the table up/down/start/end
    782792 Newbie
    Currently Being Moderated
    Hi John,

    Now I have created four Button - Move Row to First, OneLevelUp, OneLevelDown, Move Row to Last. So what ever the selected row user can click on the button as per there convenient --

    Code for moving One level Down -
    With the following code I am able to move the row one level down.
    In all the cases its working other than the following case -
    1) First Row is not moving to second Row
    2) Second Last Row is unable to move to last
    For both the cases I am getting exception. Any help appreciated ....

    public void processRowOneLvlDown(ActionEvent actionEvent) {
    JUCtrlHierNodeBinding rowdata = null;
    int rowIndex =0;
    RowKeySet sk = table.getSelectedRowKeys();
    Iterator selection =sk.iterator();
    while (selection.hasNext()) {
    Object rowKey = selection.next();
    table.setRowKey(rowKey);
    rowdata = (JUCtrlHierNodeBinding)table.getRowData();
    rowIndex = table.getRowIndex();
    }

    RowIterator rowItr = rowdata.getRowIterator();
    //removing the selected Row
    Row tempRow = rowItr.removeCurrentRowAndRetain();
    //moving the iterator to the next row
    rowItr.next();
    //Again inserting the row
    rowItr.insertRow(tempRow);

    }
  • 6. Re: Moving the row of the table up/down/start/end
    782792 Newbie
    Currently Being Moderated
    Hi John,

    I am able to Perform MoveUp, MoveDown, MoveLast.
    But while performing MoveFirst , I am getting error. Even moveup operation when it comes to replace first Row it throws error -

    Code -
    public void processToFirst(ActionEvent actionEvent) {
    JUCtrlHierNodeBinding rowdata = null;
    int rowIndex =0;
    RowKeySet sk = table.getSelectedRowKeys();
    Iterator selection =sk.iterator();
    while (selection.hasNext()) {
    Object rowKey = selection.next();
    table.setRowKey(rowKey);
    rowdata = (JUCtrlHierNodeBinding)table.getRowData();
    rowIndex = table.getRowIndex();
    }

    RowIterator rowItr = rowdata.getRowIterator();
    Row tempRow = rowItr.removeCurrentRowAndRetain();
    rowItr.first();
    rowItr.insertRow(tempRow);

    }

    Exception -
    java.util.NoSuchElementException
         at java.util.ArrayDeque.removeFirst(ArrayDeque.java:251)
         at java.util.ArrayDeque.pop(ArrayDeque.java:480)
         at oracle.adfinternal.view.faces.context.ApplicationContextManagerImpl.popContextChange(ApplicationContextManagerImpl.java:66)
         at oracle.adf.view.rich.component.fragment.UIXInclude.tearDownVisitingContext(UIXInclude.java:206)
         at oracle.adf.view.rich.component.fragment.UIXPageTemplate.tearDownVisitingContext(UIXPageTemplate.java:248)
         at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.encodeEnd(ContextSwitchingComponent.java:157)
         at org.apache.myfaces.trinidad.render.CoreRenderer.encodeChild(CoreRenderer.java:415)
  • 7. Re: Moving the row of the table up/down/start/end
    adfLearner Explorer
    Currently Being Moderated
    Hi

    Look at the following thread
    https://kr.forums.oracle.com/forums/thread.jspa?threadID=2194426

    or
    http://www.consideringred.com/files/oracle/2011/Thread2194426MoveRowApp-v0.01.zip


    Thanks,
    Rajdeep

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points