Thursday, June 7, 2012

ADF 11g Table Row Selection

By default, when the 'Row Selection' property of the ADF af:table component is set to 'Single', the first row rendered in the table at run-time is 'Selected'. This is all fine and works as expected. The issue occurs in a scenario where the end-user wants to choose exactly the same row that ADF renders as 'Selected' especially when there is a back end logic to fetch and process the selected row values. This is because the 'selection listener' on the table is invoked only when a row state changes from 'unselected' to 'selected' - hence the behaviour.

In order to overcome this situation and ensure that 'NO' rows are 'rendered' selected by default, clear the 'SelectedRowKeys' property under the 'Advanced' section. This will ensure that at runtime ADF renders the table with no rows selected.

On the other hand, if you override the 'Selection Listener' with a custom listener (to process selected row values) you might want to use the following code snippet in your backing bean to make the current row selected;

        ADFUtil.invokeEL("#{bindings.CI_CA_HDR_PNG.collectionModel.makeCurrent}",
                                 new Class[] { SelectionEvent.class },
                                 new Object[] { selectionEvent });

where #{bindings.CI_CA_HDR_PNG.collectionModel.makeCurrent} is nothing but the default 'Selection Listener' value.

20 comments:

  1. Hi Sathyam,
    Many thanks for the nice post.
    I'm a novice to ADF and have a difficulty. I want to display an alert message, once a (specific)table cell been clicked.
    I have added commandLink to perform the action (display alert) to the table.
    My observation is row select event occurs/has precedence first over the cell/command link click.
    I want to have command link functionality to run first (ahead of row select event/row selection listener).
    Could you please suggest work-around to achieve this?

    ReplyDelete
    Replies
    1. Hi Mandar,

      I think you should be using a 'goLink' or 'commandButton' component instead of 'commandlink' the reason being commandLink component generates an action event on the server. There are two things that you can do;
      1. Use the button or goLink component to generate the popup message
      2. On the rowSelectionListener method, invoke the popup programmatically. On how to do this, refer the ADF documentation below;

      http://docs.oracle.com/cd/E16162_01/web.1112/e16181/af_dialog.htm#BEJIHFFH

      Hope this helps

      Delete
  2. HI sathya,
    My question not related to this post. I have a master-detail table. I am implementing CRUD functionality in both the tables. It is working fine. When I insert a new row in the child table, I am auto populating tow column values, the third column value has to entered by the user. In my case, it is not happening. The third column is becoming non-editable. If I don’t auto populate those two columns, I am able to enter the value for the third column. How to achieve this? Please help me.

    ReplyDelete
    Replies
    1. Check if the EO/VO is 'updatable'. I would like to know the technique which you are using to auto populate certain columns. One thing that you can try is to drag drop the data control to the page as ADF table. Select the columns (which you would auto populate) and set the read only property of input text to 'true'. This way you prevent users from entering values in the auto-populated columns at the same time allow them to enter values for other columns.

      Delete
    2. HI Sathya,
      Thanks for the kind reply. Let me explain my requirement. I have table, which has 3 columns. In that, 2 are output text, 1 is input text. I am auto populating those 2 columns. First one, I have hard coded it. For the second column, I am calling the implementation class, executing the package and returning value. That will be there in the managed bean of the 'ADD' action.If I auto populate this, the input text column is becoming non-editable. If I auto hard code this, then I am able to edit the input text column. And yes, both EO/VO are updatable.

      Delete
  3. Hi Sathya,

    In my application I search with some data and get the result set in a table and by default the first row is selected. My problem is when I select another row in the table it is highlighted. Now if I search again the highlighted row remains the previously selected one and the selected row is first row only since I am selecting it by my managed bean.

    ReplyDelete
    Replies
    1. Hi, If you follow the post it has been clearly mentioned. You will have to clear the 'selectedRowKeys' property under the 'Advanced' tab of ADF table. This will get rid of the 'default' first row selection. As for the selected row being 'selected' even after 2nd search, you can probably try to add a simple line of code in your managed bean on the table binding getter method to manually clear the selectedRowkeys property. This will ensure that every time the table results are loaded, the previous selection will be discarded.

      Hope it helps.

      Delete
  4. Hi Satya,

    I have a custom selectionListener that I am using to set some values in bean based on the selected row. I also have a setDefaultRow method in my bean(I have a requirement to make the first row selected by default) that I am using to set the default row in the table on UI. But when I select any other row on UI after setting the default row, the selectionEvent is persisting the default selected row values but not the newly selected row values.
    Hence my selectionListener is always fetching me the default row values. How can I make the selectionEvent that I get from selectionListener fetch me the data of newly selected row but not the default row always. Thanks in advance.

    Sanju


    ReplyDelete
    Replies
    1. Sanju,

      ADF is behaving the way it should. Every time you click on a row in your table, the custom selection listener gets invoked - calls the set default row which is nothing but the first row of your table. You don't have to write code in your bean to make the first row of the table selected. By default ADF table renders the first row selected. But if the end user wants to choose the first row (which is selected by default) selection listener will not be invoked - that's where this post will help you overcome the issue.

      Delete
    2. Thanks much for the prompt reply. Do you mean to say it is selected by default in the backend? If the first row is already selected by default, why doesn't it show as selected(highlighted) when page loads. (I hope it is selected by default even when you have your own selectionListener.) How can I get it highlighted when the page loads, if it is already selected by default. See, my requirement is that first row should be selected(and displayed so on UI) by default and I have my own custom Listener on the table. How to do this? Also, to overcome the issue of first row not invoking selListener, should I put the above code in the selListener method? What will be the CI_CA_HDR_PNG value in a regular managed bean scenario? Sorry, I am relatively new to ADF,as you can see.

      Sanju

      Delete
  5. Also, btw, you said it will be selected by default. When I load the page and check the value in my selListener as follows, the tableList size is zero where as at should be 1 when a row gets selected.

    List tableList = TableUtilBean.getSelectedRowData(ev); // ev is SelectionEvent

    sanju

    ReplyDelete
  6. Hi Satya,

    I have a mail.jsff page inside it included another jsff file which contains a popup. My main.jsff contains a table in this table one of the row contains commandimageLink. When i click this link inside the listener action i would like to access the Popup(Included Page). I am not able to findout. Will you please pour your suggetions on this. Thanks in advance

    ReplyDelete
  7. HI Sathya,

    My question is apart from this topic....Can you please tell me how to fetch the records of same id in bpel??

    Thanks,
    Wasia

    ReplyDelete
    Replies
    1. Hi Wasia,

      Your query is so open ended. Can you please elaborate?

      Delete
  8. Dear Satya,
    I have a requirement such that my first row should get selected and call the selection listener. Is there any way to achieve it?
    I also have master detail relationship.

    ReplyDelete
    Replies
    1. Do you mean on page load? Then you can do this in backing bean easily.. but selection listener triggers only on user-selection. It can't trigger on itself automatically with page load.

      Delete
  9. hi sathya

    I want the application to change the current row of the table with tab. It keeps the current row as the first row even though with using tab i am on the fifth row. please help.

    ReplyDelete
  10. Hi sathya,
    I have a Backing bean method for Selection Listener on ADF Table. Row is not getting invoked for First Row.(also in case of one and only one row in the result set of the table).
    What will be the solution for this scenario?
    Thanks
    Shashank

    ReplyDelete
  11. Hi Sathya,
    I have a table in page1 which has cust id as one of its columns. On clicking the that cust id i navigate to page 2 which renders a table with cust id as one of its colulmn. what i need is when i land in page 2 the row that has cust id that i clicked on page 1 needs to be selected by default.

    Thanks.

    ReplyDelete
  12. Hi Sathya,
    Nice post but my table is dynamically binded means my table is constructed by List not by any VO. So I cannot override the selection listener as you suggested in this post. Can you please suggest in this case?

    ReplyDelete