SAP Classical ALV: Change Subtotal

Posted by Krishh Webworld | 6:11 PM | , , | 1 comments »

Sometimes we need to modify the subtotals on the ALV, specially when we have to give the average of the percentages or something like that. To change the subtotal we need to follow certain steps:

1. we need to get the ALV object form the ALV function module. We can use the FM GET_GLOBALS_FROM_SLVC_FULLSCR to get the Global data of the ALV. From this FM we will get the ALV object.

2. After getting the ALV object, we need to get the subtotal using the method GET_SUBTOTALS of the ALV object. We will get the first level subtotal using the parameter EP_COLLECT01.

3. Now, we need to modify the subtotal. Here we need to take help of Field-symbols since the EP_COLLECT01 is reference to data.

4. We need to refresh the Table display. For this purpose we can use the method REFRESH_TABLE_DISPLAY.

Here is the code snippet which performs all the steps mentioned above:


Code Snippet to change the Subtotal
  
DATA: lo_grid TYPE REF TO cl_gui_alv_grid.
*
* get the global reference
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lo_grid.
*
* get the subtotal
DATA: it_01 TYPE REF TO data.
*
CALL METHOD lo_grid->get_subtotals
IMPORTING
ep_collect01 = it_01.
*
* change the data
FIELD-SYMBOLS: TYPE ANY TABLE,
TYPE ANY,
TYPE ANY.
ASSIGN it_01->* TO .
*
LOOP AT ASSIGNING .
ASSIGN COMPONENT 'PER' OF STRUCTURE TO .
= '100'.
ENDLOOP.
*
* Refresh the table display
CALL METHOD lo_grid->refresh_table_display
EXPORTING
i_soft_refresh = 'X'.



To be able to use the FM GET_GLOBALS_FROM_SLVC_FULLSCR, we need to find a spot in our program which is being called after the ALV's main FM e.g. REUSE_ALV_GRID_DISPLAY. For this purpose, I have implemented the TOP_OF_PAGE event because it will always be called after the FM. In the subroutine for the TOP_OF_PAGE, I used the code snippet to change the Subtotal.

You can download the entire source code of my test program from here --> http://smartform.googlecode.com/files/ZALV_SUBTOT_CHANGE.txt .

Archives

Subscribe Now: Feed Icon