15.2    Interactive Reports

Similar to classical reports that can be made interactive in order to respond to user actions on the list output, ALV reports also support various interactive features. Unlike classical reports, in which list events are maintained in the program directly to make the report interactive, ALV requires you to pass the subroutine name of the calling program that should be called for the event.

The output screen of the ALV report belongs to the SLVC_FULLSCREEN function group; when the user performs any action on the screen, it triggers the PAI module of the function group, in which the user action is evaluated and the respective subroutine of the calling program is called using dynamic programming techniques.

If we dig into the ALV function group code, we’ll see the dynamic subroutine call, as shown in Figure 15.8.

Dynamic Subroutine Call of Calling Program from ALVALVdynamic subroutine

Figure 15.8    Dynamic Subroutine Call of Calling Program from ALV

The code shown in Figure 15.8 uses the information supplied to the I_CALLBACK_PROGRAM and I_CALLBACK_USER_COMMAND parameters of the ALV function modules to execute the code in the supplied subroutine.

Now, let’s look at some of the interactive elements that can be added to ALV reports.

15.2.1    Loading a Custom SAP GUI Status

ALV provides standard SAP GUI buttons, which can be replaced or supplemented by a custom SAP GUI status in the program. The SAP GUI status must first be defined for the program using Menu Painter before it can be set. You can also set the SAP GUI status defined for another program. When you pass your custom SAP GUI status to ALV, it will completely replace the default ALV SAP GUI status.

If you want to use the standard features provided by the default ALV SAP GUI status and supplement it with your custom buttons, then we recommend copying the standard ALV SAP GUI status and modifying it. The SAPLSLVC_FULLSCREEN ALV function group contains the STANDARD_FULLSCREEN SAP GUI status, which can be copied to your program and modified.

Follow these steps to copy the SAP GUI status:

  1. On the Menu Painter initial screen (Transaction SE41), enter the name of the function group, SAPLSLVC_FULLSCREEN, and the SAP GUI status, STANDARD_FULLSCREEN, that you intend to copy, as shown in Figure 15.9. Click the Copy Status button in the application toolbar.
    Menu Painter: Initial ScreenMenu Painterload custom SAP GUI status

    Figure 15.9    Menu Painter: Initial Screen

  2. In the Copy Status dialog box that appears, enter your program name and the SAP GUI status, as shown in Figure 15.10. Click Copy on the current and subsequent windows to copy the status.
    Copy Status Window

    Figure 15.10    Copy Status Window

  3. Once the status is copied, you can edit it to add or remove any function codes. In Figure 15.11, we added a PDF icon to the application toolbar with PDF as the function code. Note that all the standard ALV function codes start with &; this helps ALV to distinguish between standard function codes (which are part of ALV) and user-defined function codes. When you select a standard function code, ALV automatically handles the function code, but you need to handle custom function codes in your program.
    Adding Function Codes to Copied Status

    Figure 15.11    Adding Function Codes to Copied Status

To set an SAP GUI status, you need to pass the program name to the I_CALLBACK_PROGRAM parameter and a subroutine name to the I_CALLBACK_PF_STATUS_SET parameter of the ALV function module. The subroutine name that is passed to the I_CALLBACK_PF_STATUS_SET formal parameter should be maintained in the program with one formal parameter of type SLIS_T_EXTAB. This formal parameter will contain the function codes to be hidden from the UI.

Listing 15.7 shows sample code to set an SAP GUI status.

*&--------------------------------------------------------------*
*& Form SHOW_OUTPUT
*&--------------------------------------------------------------*
FORM show_output.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = 'ZDEMO_INTERACTIVE_REPORT'
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
I_GRID_TITLE = 'ALV Grid Display'
IS_LAYOUT = layout
IT_FIELDCAT = it_fieldcat
TABLES
t_outtab = it_sflight
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
ENDFORM.
*&--------------------------------------------------------------*
*& Form SET_PF_STATUS
*&--------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'GUIALV'.
ENDFORM.

Listing 15.7    Setting Custom SAP GUI Status

In Listing 15.7, the calling program name and the subroutine name of the program are passed to the I_CALLBACK_PROGRAM and I_CALLBACK_PF_STATUS_SET parameters, respectively. The subroutine name 'SET_PF_STATUS' is passed to the ALV function module and is maintained in the calling program with the rt_extab formal parameter, which is of type SLIS_T_EXTAB.

In this example, the RT_EXTAB formal parameter of the SET_PF_STATUS subroutine can be used to hide the function codes in the output, using the EXCLUDING addition with the SET PF-STATUS statement.

For example, we defined the PDF icon in the currently displayed SAP GUI status, as shown in Figure 15.12.

Showing Custom PDF IconPDF

Figure 15.12    Showing Custom PDF Icon

However, say you want to hide this icon from the display if the output has no records; in that case, you can use the code shown in Listing 15.8. This code will ensure that the PDF icon is displayed only if the output table IT_SFLIGHT is not initial, as shown in Figure 15.13. We’re clearing the internal table RT_EXTAB to ensure no other function codes are excluded.

*&--------------------------------------------------------------*
*& Form SET_PF_STATUS
*&--------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
DATA rw_extab LIKE LINE OF rt_extab.
IF it_sflight IS INITIAL.
CLEAR rt_extab.
rw_extab-fcode = 'PDF'.
APPEND rw_extab to rt_extab.
SET PF-STATUS 'GUIALV' EXCLUDING rt_extab.
ELSE.
SET PF-STATUS 'GUIALV'.
ENDIF.
ENDFORM.

Listing 15.8    Excluding Function Codes

PDF Icon Hidden from User

Figure 15.13    PDF Icon Hidden from User

15.2.2    Reacting to User Actions

When we provide additional function codes using a custom SAP GUI status, we need to handle the related functionality in the program. For example, if we add a PDF button to allow a user to download the output as a PDF, then we need to write code in the program to download the output as a PDF. This requires identifying the user action on the screen and writing the respective logic.

To achieve this, we need to pass the subroutine name to the I_CALLBACK_USER_COMMAND parameter. This subroutine should be maintained in the program with two formal parameters, one of type SY-UCOMM and another of type SLIS_SELFIELD, as shown:

FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
ENDFORM.

When this subroutine is called from the ALV function module (refer back to Figure 15.8), it will pass these parameters to the calling program. Here, R_UCOMM will contain the function code, and RS_SELFIELD will contain information about the output table, the current row, the user selected, and so on.

Listing 15.9 shows sample code to identify if the user has selected the PDF icon we created previously. Multiple user actions can be evaluated within the subroutine using the CASE statement. The standard function code '&IC1' is passed to the R_UCOMM formal parameter if the user double-clicks the row. The rs_selfield-fieldname and RS_SELFIELD-VALUE fields will contain the column name in which the user double-clicked the displayed output and the value in the cell, respectively.

FORM user_command  USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN 'PDF'.
*write appropriate logic.
WHEN '&IC1'.
*write appropriate logic.
ENDCASE.
ENDFORM.

Listing 15.9    Identifying User Action

15.2.3    Printing TOP-OF-PAGE

To print the TOP-OF-PAGE information, we can pass the subroutine name to the I_CALLBACK_TOP_OF_PAGE parameter. Within this subroutine, the REUSE_ALV_COMMENTARY_WRITE function module is called to print TOP-OF-PAGE, as shown in Listing 15.10.

Refer to the function module documentation for the REUSE_ALV_COMMENTARY_WRITE function module to learn more about the parameter interface.

FORM top_of_page.
DATA: lt_cw TYPE slis_t_listheader,
lw_cw TYPE slis_listheader.
lw_cw-typ = 'H'.
lw_cw-info = 'Flight Information'.
APPEND lw_cw TO lt_cw.

lw_cw-typ = 'S'.
lw_cw-key = P_CARRID.
lw_cw-info = 'Selection criteria'.
APPEND lw_cw TO lt_cw.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = lt_cw.
ENDFORM.

Listing 15.10    Printing TOP-OF-PAGE

ALV function modules support various other events, such as USER_COMMAND, TOP_OF_COVERPAGE, END_OF_COVERPAGE, and so on, depending on the type of list generated. These events allow you to provide additional information to ALV when the report is running. The REUSE_ALV_EVENTS_GET function module can be used to get the supported events for the list, and the subroutine names can be passed for different events using the IT_EVENTS parameter.