Reorder rows in an ADF table. As shown in the images below,
applying the code in this post will allow you to re-arrange rows within a
table using "up and down" buttons Its simply and change rank attribute
value of swiped rows and change in the indexing of the iterator.
Selecting a row and move it to a step up or down ,then will show it in its new position within the table
and you may commit later
Note that
reordering the rows in the table will only have an effect to the
DCIteratorBinding (more precise, its an effect to the RowSetIterator it
decorates) in the Pagedef file.
Managed Bean
/** The selected row takes the position of the previous row.
* synchronize the underlying ADF model I needed to accept a
* "flicker" of the table after the action *
* @param actionEvent Event passed in from ADF Faces at the end of the action
* */
public void upButton(ActionEvent actionEvent) {
// Add event code here...
Row currentRow;
Row previousRow;
currentRow =
ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();
Number tRank = new Number();
try {
tRank = (Number)currentRow.getAttribute("Rank");
} catch (Exception e) {
}
if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasPrevious()) {
previousRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().previous();
currentRow.setAttribute("Rank", previousRow.getAttribute("Rank"));
previousRow.setAttribute("Rank", tRank);
int indexOfPreviousRow = ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(previousRow);
//remove selected row from collection so it can be added back
currentRow.removeAndRetain();
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfPreviousRow,currentRow);
//make row current in ADF iterator.
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfPreviousRow);
}
}
/** The selected row takes the position of the next row.
* synchronize the underlying ADF model I needed to accept a
* "flicker" of the table after the action *
* @param actionEvent Event passed in from ADF Faces at the end of the action
* */
public void downButton(ActionEvent actionEvent) {
Row currentRow;
Row nextRow;
currentRow =
ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();
System.out.println(currentRow.getAttribute("Rank"));
Number tRank = new Number();
try {
tRank = (Number)currentRow.getAttribute("Rank");
} catch (Exception e) {
}
if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasNext()) {
nextRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().next();
currentRow.setAttribute("Rank", nextRow.getAttribute("Rank"));
nextRow.setAttribute("Rank", tRank);
int indexOfSelectedRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(currentRow);
//remove selected row from collection so it can be added back
nextRow.removeAndRetain();
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfSelectedRow, nextRow);
//make row current in ADF iterator.
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfSelectedRow + 1);
}
}
Managed Bean
/** The selected row takes the position of the previous row.
* synchronize the underlying ADF model I needed to accept a
* "flicker" of the table after the action *
* @param actionEvent Event passed in from ADF Faces at the end of the action
* */
public void upButton(ActionEvent actionEvent) {
// Add event code here...
Row currentRow;
Row previousRow;
currentRow =
ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();
Number tRank = new Number();
try {
tRank = (Number)currentRow.getAttribute("Rank");
} catch (Exception e) {
}
if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasPrevious()) {
previousRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().previous();
currentRow.setAttribute("Rank", previousRow.getAttribute("Rank"));
previousRow.setAttribute("Rank", tRank);
int indexOfPreviousRow = ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(previousRow);
//remove selected row from collection so it can be added back
currentRow.removeAndRetain();
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfPreviousRow,currentRow);
//make row current in ADF iterator.
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfPreviousRow);
}
}
/** The selected row takes the position of the next row.
* synchronize the underlying ADF model I needed to accept a
* "flicker" of the table after the action *
* @param actionEvent Event passed in from ADF Faces at the end of the action
* */
public void downButton(ActionEvent actionEvent) {
Row currentRow;
Row nextRow;
currentRow =
ADFUtils.findIterator("TreeTableView1Iterator").getCurrentRow();
System.out.println(currentRow.getAttribute("Rank"));
Number tRank = new Number();
try {
tRank = (Number)currentRow.getAttribute("Rank");
} catch (Exception e) {
}
if (ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().hasNext()) {
nextRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().next();
currentRow.setAttribute("Rank", nextRow.getAttribute("Rank"));
nextRow.setAttribute("Rank", tRank);
int indexOfSelectedRow =
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().getRangeIndexOf(currentRow);
//remove selected row from collection so it can be added back
nextRow.removeAndRetain();
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().insertRowAtRangeIndex(indexOfSelectedRow, nextRow);
//make row current in ADF iterator.
ADFUtils.findIterator("TreeTableView1Iterator").getViewObject().setCurrentRowAtRangeIndex(indexOfSelectedRow + 1);
}
}
The JDeveloper 11.1.1.7.0 workspace of the sample application and script file for this post can be downloaded from here
Thanks a lot karim, It saved a lot of time for me.
ReplyDeleteyou are welcome
DeleteThank you very much karim,this helps us allot and saves our time
ReplyDeleteyou are welcome
DeleteKarim, This only works on the first time you move the row. if you move the same row again for second time it does not. Do you have any suggestions for this?
ReplyDeletePlease debug and check why it works for first time only at yours and let me know if I could help
Delete