SCN Wiki upgrade to version 6.13 on Tuesday 19.11.2019 - beginning 07:30 am CET

During upgrade process new content will not be transported to new Wiki version

Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Author: Thomas Jung
Submitted: April 12, 2007

Description

The other day I got a very interesting question about the use of the ALV component in Web Dynpro ABAP.  The person wanted to use the standard button APPEND/INSERT/DELETE to add or remove rows from the table displayed in the ALV.  However whenever a new row was inserted, they wanted to automatically calculate some default values for the new row before it was displayed to the user.

It turns out that it is possible to hook an event handler for the standard events exposed by the ALV Component.  This example is also a good opportunity to learn more about Component Reuse and cross-Component communication.  In my opinion, Component Usage is a tremendously powerful aspect of Web Dynpro development.  The ability to map context nodes, register events, and access methods between components provides lots of possibilities.  In this case we will take a method in our own component and register it as a callback method for an event triggered within the reusable ALV Component.

Solution

I had never tried to tie into the standard insert/delete functions of an ALV before. My first though was that you can register a view event handler for the standard functions of the ALV. I though perhaps the ON_STD_FUNCTION_AFTER  could be used to trap the event and insert the initial values in the new row. Unfortunately I tested and the APPEND/INSERT/DELETE buttons of the ALV do NOT trigger this event. That wouldn't have been any fun anyway because it would have made the solution too easy. (smile)

I ultimately ended up with a similar approach using the ON_DATA_CHECK event instead and that is I will detail here:

  • First I hosted the ALV Component within a View_Container in a view.

    -

    So far nothing new that hasn't been covered in some of the basic ALV/Web Dynpro tutorials.
  • Next we need to make some configuration setting changes to the ALV Component. In the DOINIT method of my component controller I get a reference to the component usage for the ALV. I then set the table as available for update. But most importantly I change the data check event to trigger on Cell Events. This will cause the data check event to fire right after any of the standard APPEND/INSERT/DELETE buttons are pressed.
    method wddoinit .
      data isflight type standard table of sflight.
      select * from sflight into table isflight.data:
      node_sflight type ref to if_wd_context_node.
    
    *navigate from <CONTEXT> to <SFLIGHT> via lead selection
      node_sflight = wd_context->get_child_node( name = wd_this->wdctx_sflight ).
    *TODO handle not set lead selection
      if ( node_sflight is initial ).
        exit.
      endif.
      node_sflight->bind_table( isflight ).
    
      data: l_ref_cmp_usage type ref to if_wd_component_usage.
      l_ref_cmp_usage = wd_this->wd_cpuse_alv( ).
      if l_ref_cmp_usage->has_active_component( ) is initial.
        l_ref_cmp_usage->create_component( ).
      endif.
      data l_salv_wd_table type ref to iwci_salv_wd_table.
      l_salv_wd_table = wd_this->wd_cpifc_alv( ).
      data l_table type ref to cl_salv_wd_config_table.
      l_table = l_salv_wd_table->get_model( ).
    
      l_table->if_salv_wd_table_settings~set_read_only( abap_false ).
      l_table->if_salv_wd_std_functions~set_edit_insert_row_allowed( ).
      l_table->if_salv_wd_table_settings~set_data_check( if_salv_wd_c_table_settings=>data_check_on_cell_event ).
    
    endmethod.
    
  • I create an event handler method in my view. Using the Value help on the Event column you can select an event from the local component or any component usage (even though the field is not ready for input). I choose the ON_DATA_CHECK event of the ALV Component Usage. I know have a local method in my view that is a callback handler for the event that is raised by the external ALV Component.
  • I'm going to keep this example simple. I am showing the rows from the sample table SFLIGHT in my ALV. Whenever someone appends or inserts a new field I want to default the Carrier ID to the value 'AA' (I want to get bonus Frequently Flyer miles afterall).
  • The R_PARAM parameter will be generated for the event handler, ON_DATA_CHECK, with a type of IF_SALV_WD_TABLE_DATA_CHECK. You can use the attribute of this object, t_inserted_rows, to process any of the newly inserted rows. This object is read only however. Even if you alter of the values of the r_value attribute, they will not be reflected in the source context. Therefore I used the index to read the context and update the value of the field I wanted to place a default value into.
    
    METHOD on_alv_insert .
    
      FIELD-SYMBOLS: <wa_row> LIKE LINE OF r_param->t_inserted_rows.
      DATA isflight TYPE REF TO sflight.
      LOOP AT r_param->t_inserted_rows ASSIGNING <wa_row>.
        isflight ?= <wa_row>-r_value.
        IF isflight->carrid IS INITIAL.
          DATA:
            node_sflight                        TYPE REF TO if_wd_context_node,
            elem_sflight                        TYPE REF TO if_wd_context_element,
            stru_sflight                        TYPE wd_this->element_sflight .
    * navigate from <CONTEXT> to <SFLIGHT> via lead selection
          node_sflight = wd_context->get_child_node( name = wd_this->wdctx_sflight ).
    * alternative access  via index
          elem_sflight = node_sflight->get_element( index = <wa_row>-index ).
          elem_sflight->set_attribute(
            EXPORTING
              value = 'AA'
              name  = 'CARRID' ).
        ENDIF.
      ENDLOOP.
    
    ENDMETHOD.
    
  • So now in my application, all new rows automatically have the AA value for the Carrier ID as soon as they are inserted.

Source Code

If you would like to import the entire Web Dynpro Component of this example; the following is a link to a SAPlink SLNK file that contains the project. You will need at least the 1.3 version of SAPlink and the Web Dynpro Component plug-in to import the SLNK.
http://abap-sdn-examples-tpj.googlecode.com/files/WDYN_ZINSERT_TEST.slnk

4 Comments

  1. Guest

    Hi,

    I tried to catch the events ON_STD_FUNCTION_AFTER and ON_STD_FUNCTION_BEFORE but the method is not called when I press INSERT_ROW, APPEND_ROW or DELETE_ROW.

    Only the event ON_DATA_CHECK is called.

    Best regards

    Marcus

  2. Marcus, your experience is correct.  In fact I wrote exactly that within this WIKI Code Samples:

    I though perhaps the ON_STD_FUNCTION_AFTER  could be used to trap the event and insert the initial values in the new row. Unfortunately I tested and the APPEND/INSERT/DELETE buttons of the ALV do NOT trigger this event. That wouldn't have been any fun anyway because it would have made the solution too easy. (smile)

    Given that the purpose of this code sample is to provide a solution using ON_DATA_CHECK, I'm not sure what your comment is refering to.

  3. Former Member

    Hello,

    In this example, it sets the attribute directly to 'AA'

    Is it possible to set the attribute as dropdown list?

    If yes, how?

    I've been trying to refire ALV all over again here and set the dropdown using dropdownbykey, same thing as with my other fields that have dropdown values that are not dependent on other fields, but it isn't working

    My requirement is to have an editable ALV, user then enters data to it and then save to database

    Some columns are dropdowns

    While inputting data to ALV table, some column's dropdown value will depend on the value of other columns

    Example: I entered PO number in one column, the next column's dropdown values should contain the PO's line items

    Thanks

  4.  

    Thanks Thomas for the Blog - I was able to default ALV field value on press of Insert button on Webdynpro ALV, using these steps !!! (smile)