Author: Thomas Jung
Submitted: April 12, 2007
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.
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.
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.
- 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.
- So now in my application, all new rows automatically have the AA value for the Carrier ID as soon as they are inserted.
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.