Sunday, June 6, 2010

Deleting multi-selected rows from adf table

Hi all,

Back from little trip from Karachi and today i will show how to delete multiple selected rows from adf table.

In this example i will selected rows using
1)  CTRL or SHIFT with mouse click
2)  Checkbox at the row
and press Delete button for ease of understanding i created 2 separate buttons in the examples.

Before start i assumed you already setup a Application with EO,VO and AM also using HR Connection

- Add a Transient Attribute to VO of Boolean type named "SelectedRow" We will use this to select row
Here the layout looks like



- Drag a Collection from DataControl as ADF Table to JSF page
- Dont Check RowSelection Checkbox
- Set the table RowSelection property to multiple
- Set EditingMode property to clickToEdit
- Surround your table with af:PanelCollection for look and feel give table a id "myTab"
- Add a toolbar in toolbar facet of Panelcollection
- Add 2 toolbar buttons to your toolbar
- Add partial triggers to your table reference both buttons on toolbar
- Create a backing bean
- Add a RichTable object to your bean with getter and setter methods.
- Set the binding property of the af:table to your Managed Bean
- Add Methods in Backing bean similar to the following code
- Link a method deleteAction to your "Delete Selected Rows" button and DeleteCheckedRows to other button.

Download example for more info

Note: Don't forget to commit your rows after delete :)

More Reading:
- Providing Multiselect capabilities Fusion developer guide

-How to add Transient Checkbox to Table A good example can also be found here
http://sameh-nassar.blogspot.com/2009/12/use-checkbox-for-selecting-multiple.html
http://baigsorcl.blogspot.com/2010/05/adding-boolean-checkbox-to-table.html

Processing on multi Rows another example from Mr.Steve Muench

Happy JDeveloping,
Baig

17 comments:

  1. Hi,

    I implemented the method and its deleting the row from the table. But its not deleting from the database. How to delete records programatically from database.

    Thanks & Regards,
    raj

    ReplyDelete
  2. Well Raj Thanks for comments you have to use the Commit Operation after deleting the rows

    Simply Drag the Commit operation from data control to the layout

    ReplyDelete
  3. Hello Zeeshan,
    Let's say I have a database table...instead of deleting row from the table, Is there anything ADF (11g) provides through which I can hide a database table row based on certain event (event maybe let's say when I click a command button, it should hide a single row or multiple rows (just on front end of web application) which I select and also change one of the database table column field value(s) to something else based on that event (clicking of a command button) for the row(s) I select ?
    also my email: asimran@gmail.com ...if possible to email ?
    Thanks and regards,
    -Ahmad

    ReplyDelete
  4. Zeeshan Baig ✆
    to asimran

    show details 5:47 PM (2 hours ago)

    Hi,

    Yes you can do this by updating a delete flag (custom column) instead of deleting rows...

    Fusion developer guide has cover this use case http://download.oracle.com/docs/cd/E12839_01/web.1111/b31974/bcadveo.htm#CEGIBHBC

    Hope it helps.

    ReplyDelete
  5. Hi, Thanks for the example it works well but I have a little problem. If my table fetch more rows than the limit configured in the iterator, when I row the selected key, the next exception is raised: ADFv: Could not find row: __$ $__ with key: oracle.jbo.Key

    Could you help me??

    ReplyDelete
  6. Hi Baig,
    I was going through your example. But when the table has multi selection enabled and all rows are selected through ctrl+A, then delete does not work properly i.e all the rows are not getting deleted.[Ex: if 10 rows are selectd only 7 of them gets deleted]. But if all the rows are selected manually through ctrl then it works fine.
    Please let us know if it a bug.

    Regards,
    Praveen

    ReplyDelete
  7. Hi Praveen,

    You can try to debug, put some message in the loop to see if rows are coming or not.

    I will take a look later.

    ZB

    ReplyDelete
  8. Hi Baig,

    helping me much, but same goes to Praveen, try my best to fix somewhere but really hoping for your helps. Thanks.

    ReplyDelete
  9. I tried the same implementation but getting the exception
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.apache.myfaces.trinidad.model.CollectionModel

    ReplyDelete
  10. Hi Baig,
    I have adf table ,which has exportCollectionActionListener in that one column is of type transient as selectBooleanCheckbox ,when we exported to excel it contains the selectBooleanCheckbox value as 'True' or 'False' how to remove this column value....

    Reply

    ReplyDelete
  11. Hi Megha,

    So far there is no control to hide any columns declaratively.

    Programmatic solution is possible. check this link http://easyadf.blogspot.com/2009/05/export-dvtpivottable-to-excel-yes-its.html

    Other option is (i havent tried yet) is to set Visible false of the component then do exportcollection and set visible true again.

    ZB

    ReplyDelete
  12. Hi Zeeshan,when we select multiple check boxes ,, and press delete, it does not delete all the rows,it only deletes some of them,,,i checked in the lopp,rows r getting,but when i try to print something after r.remove(),,,it is displayed for some rows and not for others.....means deleting some rows..kindly look into this.
    Idris

    ReplyDelete
  13. Hi Idris,

    One example will delete only selected rows if you press selected rows button.

    Other example will delete rows that you mark checked.

    Which JDEV version are you using i guess it was developed using PS4

    Hope it help,
    ZB

    ReplyDelete
    Replies
    1. Hi Zeeshan,
      I am a regular visitor of your blog for some great stuff.
      I think Idris has a point. And I was facing this problem too. The issue is with the getEstimatedRowCount() method which decreases with each removal of record leaving the last few records to be left undeleted even though they are checked. I could not find a neat explanation elsewhere so wrote this blog.
      http://adffunda.blogspot.in/2013/07/deleting-multiple-rows-in-table.html

      Satya

      Delete
  14. hi...

    i created one table..inside this table one column is boolean checkbox..so if i click on column checkbox inside that column all the rows should check..if i deselect it all row should deselect..for that i created a ValueChangeListener and managedbean..so inside the managedbean i code the below...

    public void SelectAllBoolean(ValueChangeEvent valueChangeEvent) {
    // Add event code here...
    System.out.println("SelectAllBoolean"+
    valueChangeEvent.getNewValue());
    //RepSetTableDemoVO1Iterator THIS IS MY ITERATOR NAME

    boolean isSelected = ((Boolean)valueChangeEvent.getNewValue()).booleanValue();
    DCBindingContainer dcb = (DCBindingContainer) evaluateEL("#{bindings}");
    DCIteratorBinding dciter =dcb.findIteratorBinding("RepSetTableDemoVO1Iterator");

    ViewObject vo = dciter.getViewObject();
    int i = 0;
    Row row = null;
    vo.reset();
    while (vo.hasNext()) {
    if (i == 0)
    row = vo.first();
    else
    row = vo.next();
    // System.out.println("Changing row 1: " +
    // row.getAttribute("Name"));
    //System.out.println("xdebug c2: Changing row 2: " +
    //row.getAttribute("ExportFromOffice"));
    //ExportFromOffice THIS IS MY COLUMN NAME FOR BOOLEAN CHECKBOX
    if(isSelected)
    row.setAttribute("ExportFromOffice", "Y");
    else
    row.setAttribute("ExportFromOffice", "N");
    i++;
    }
    }

    private static Object evaluateEL(String el) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =
    facesContext.getApplication().getExpressionFactory();
    ValueExpression exp =
    expressionFactory.createValueExpression(elContext, el,
    Object.class);
    return exp.getValue(elContext);

    }

    PLEASE TELL ME THIS IS CORRECT OR NOT...tell me the code for that problem...this is my iterator name RepSetTableDemoVO1Iterator.. this is my VO name RepSetTableDemoVO... this is my column name to set a boolean checkbox ExportFromOffice...
    how to do this..

    ReplyDelete
  15. Hi Baig,

    I have implemented the the excample you provided above and it works fine. However I have an additional requirement: when user selects the check box on a row, I want to select all the related rows (programatically). Is there any way to do that?

    ReplyDelete
    Replies
    1. Hi,

      You can use cascading delete feature of association. Also write a custom method to retrieve all related rows and remove them.

      Check this entry to get little hint http://www.baigzeeshan.com/2011/01/how-to-prevent-delete-on-master-if.html

      Zeeshan

      Delete