How to Show the ALV Output and the Selection Screen on the same Screen?

1
17245
ALV and Selection Screen Together

Normally the ALV output is displayed in a new screen. Not on the same screen where you have the selection parameters and select options. But, sometimes client want to see what they input in the screen and what they get output in the ALV in the same screen. It just helps them perform better, report better and analyse better. This requirement and scenario is not new. We have solution for it from back then in 2008.

If the solution is already there, then why again a new article? SAPYard has to be different. 🙂 The earlier solution provided uses SALV class “CL_SALV_TABLE“. We will use “CL_GUI_ALV_GRID” class due to its enhanced features.

Expectations

  1. Selection Screen and ALV on the Same Screen.
  2. Add a Button on the ALV Toolbar.
  3. Handle User Command.

For more information on the usage and functioning of the class CL_GUI_ALV_GRID, you can refer to the demo programs provided by SAP with the name like “BCALV_GRID* “.

Sample programs provided by SAP on the usage of CL_GUI_ALV_GRID.

Output

Simple Selection Screen with one Select-Options S_CARRID of type SFLIGHT-CARRID

Below is the Final Output which the Client is Expecting.

Selection screen and ALV on same screen

Do you know, how to check the consistency of any ALV Report? Check this – Just a key and two clicks for ALV consistency check

Let’s Design

High Level Programming Paradigm:

In other words, Pseudo Code for this requirement. 😉

  1. Docking Container needs to be created in the INITIALIZATION event.
  2. ALV needs to be created on that Docking Container.
  3. Export the SELECTed Output Table Data to ABAP Memory (this part we do not like. But till we find a better solution, let’s use it).
  4. Import data from ABAP Memory and Generate the Output.

Global Data Declarations

CLASS lcl_report DEFINITION DEFERRED.
DATA: lo_report TYPE REF TO lcl_report. "Reference object of the local class
DATA: alv_container TYPE REF TO cl_gui_docking_container, "ALV Container
      alv_grid      TYPE REF TO cl_gui_alv_grid,          "ALV Grid
      layout        TYPE lvc_s_layo.                      "Layout Options               
DATA gv_carrid TYPE sflight-carrid. "Variable for Select-options declaration.

Selection Screen Declarations

SELECTION-SCREEN: BEGIN OF BLOCK block_1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_carrid FOR gv_carrid.
SELECTION-SCREEN: END   OF BLOCK block_1.

Local Class Definition

CLASS lcl_report DEFINITION .

  PUBLIC SECTION.
    DATA: gt_data TYPE STANDARD TABLE OF sflight.
    METHODS :
      get_data,
      generate_output,
      toolbar      FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object, "To add some buttons on the ALV toolbar
      user_command FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm.

ENDCLASS.

Local Class Implementation

  • Method: GET_DATA
CLASS lcl_report IMPLEMENTATION.
  "Method Get Data
  METHOD get_data.
*   data selection
    SELECT * FROM sflight
           INTO  TABLE me->gt_data
           WHERE carrid IN s_carrid.
    IF sy-dbcnt IS INITIAL.
      MESSAGE s398(00) WITH 'No data selected'.
    ENDIF.
*
*   Export to memory
    EXPORT data = me->gt_data TO MEMORY ID sy-cprog.
  ENDMETHOD.
  • Method: GENERATE_OUTPUT
"Method generate_output
  METHOD generate_output.
*   Local data
    DATA: variant TYPE  disvariant.
    DATA: repid   TYPE sy-repid.

*   Import output table from the memory and free afterwards
    IMPORT data = me->gt_data FROM MEMORY ID sy-cprog.
    FREE MEMORY ID sy-cprog.
*
*   Only if there is some data
    CHECK me->gt_data IS NOT INITIAL.

    repid = sy-repid.
    variant-report = sy-repid.
    variant-username = sy-uname.
    layout-zebra = 'X'.

    CHECK alv_container IS INITIAL.
    CREATE OBJECT alv_container
      EXPORTING
        repid     = repid
        dynnr     = sy-dynnr
        side      = alv_container->dock_at_bottom
        extension = 200.
    CREATE OBJECT alv_grid
      EXPORTING
        i_parent = alv_container.
*  ALV Specific. Data selection.
    SET HANDLER : lo_report->toolbar      FOR alv_grid.
    SET HANDLER : lo_report->user_command FOR alv_grid.
    CALL METHOD alv_grid->set_table_for_first_display
      EXPORTING
        is_layout        = layout
        is_variant       = variant
        i_save           = 'U'
        i_structure_name = 'SFLIGHT'
      CHANGING
        it_outtab        = me->gt_data.

  ENDMETHOD.

Also Check Just 4 Versions of the same program to understand OOPs ABAP

  • Method: TOOLBAR

In this method, we will create one Push-Button (just as an example-no functionality) for example to save a record, in case we had created this ALV as editable.

"Method Tool-bar
  METHOD toolbar.
    DATA: lv_toolbar TYPE stb_button.

* Push Button "For Example SAVE
    CLEAR lv_toolbar.
    MOVE 'FC_SAVE' TO lv_toolbar-function. "Function Code
    lv_toolbar-icon = icon_system_save.    "Save Icon
    MOVE 'Save'(100) TO lv_toolbar-text.   "Push button Text
    MOVE 'Save'(100) TO lv_toolbar-quickinfo. "Push button Quick-Info
    MOVE space TO lv_toolbar-disabled. "Push button enabled
    APPEND lv_toolbar TO e_object->mt_toolbar.
  ENDMETHOD. 
  • Method: USER_COMMAND

This method will be used to handle all the commands given by the user, for example if user press on the Save button we created above etc. I have kept this section as blank, you can add the logic as required.

"Method User_command
  METHOD user_command.
    IF e_ucomm = ' '.

    ENDIF.
  ENDMETHOD.                    "USER_COMMAND

ENDCLASS.                    "lcl_report IMPLEMENTATION

In case you set the break-point at above method and press the Save button on the ALV, the execution will stop. Please check the below screenshot:

User Command captured when clicked on “Save” button.

Finally, the Events of the program execution

INITIALIZATION.
* object for the report
  CREATE OBJECT lo_report.
* generate output
  lo_report->generate_output( ).

START-OF-SELECTION.
* Get data
  lo_report->get_data( ).

Conclusion

In this blog post we learnt the basics of creating an ALV on the selection screen with the usage of CL_GUI_ALV_GRID class.

If you just copy the above code snippets and paste it to your SE38 editor and activate it. Just like one of our lazy editors at SAPYard did. You will find the output like below. 🙂 Yes!! It is a working code. And you do not need to design any screen elements. 😉

Why did we need to use ABAP Memory when all data is there in the program and we are displaying them in the same program?

When the program control reaches INITIALIZATION event, all memory is REFRESHED and global data is made FREE. After all everything should be initialized in the INITIALIZATION event. That is the job of INITIALIZATION event. Therefore, we have to have the ALV data somewhere to pull it back when we are in INITIALIZATION event. Now you know why we were forced to take help of the ABAP Memory. 😛

SAPYard takes pride in publishing our own work. The original solution was done using cl_salv_table but we are using CL_GUI_ALV_GRID . We referred to the below article. But our class ( CL_GUI_ALV_GRID ) is different. Our destination might be same, but the route is different and thus it makes all the difference.

CreditsHow to achieve the same function using cl_salv_table?

COMMENTS & QUESTIONS PLEASE!!

Do join 6060+ SAP Technical Consultants in this Telegram SAP Technical Discuss Group. Ask, Answer and Learn is our Motto. You need to install Telegram App first in your mobile or desktop and then click the joining link.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Please follow our LinkedIn Page, LinkedIn Group, Facebook Page, Twitter and Instagram.

Save our number +1-646-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.

Some more Tweaks & Tips on ALV

1 COMMENT

  1. Thank you for this fantastic posting.
    The option to select the rows in the report output is not available unless I make the layout editable. Is there anyway to make the rows selectable without make the layout editable?
    Thank you

LEAVE A REPLY

Please enter your comment!
Please enter your name here