Emre Göçmen Blog

How to Create a Popup ALV in SAP ABAP?

5 min. read
3823 views
0 comments

Emre Göçmen

Author

How to Create a Popup ALV in SAP ABAP?

How to Create a Popup ALV in SAP ABAP

Popup ALV (ABAP List Viewer) is a powerful SAP component that allows users to interactively examine data lists without leaving the main screen. In this comprehensive guide, we will cover all the steps needed to create a fully functional Popup ALV in ABAP.


What is a Popup ALV?

A Popup ALV is an ALV Grid component displayed within a dialog box. This component allows users to analyze data lists using standard ALV functions such as sorting, filtering, grouping, and exporting. It significantly improves the user experience by allowing data analysis without leaving the main screen.

Popup ALVs are particularly useful in the following situations:

• Showing details of a selection from the main screen
• Displaying intermediate results in a multi-step process
• Allowing users to make selections from a list
• Providing temporary windows for detailed data examination


Step-by-Step Guide to Creating a Popup ALV

1. Define the Data Structure and Internal Table

The first step is to define an internal table that will contain the data to be displayed in the ALV:

* Define the data structure and internal table
TYPES: BEGIN OF ty_data,
         matnr TYPE matnr,        " Material number
         maktx TYPE maktx,        " Material description
         werks TYPE werks_d,      " Plant
         lgort TYPE lgort_d,      " Storage location
         menge TYPE menge_d,      " Quantity
         meins TYPE meins,        " Unit of measure
         dmbtr TYPE dmbtr,        " Amount
         waers TYPE waers,        " Currency
       END OF ty_data.

DATA: lt_data TYPE TABLE OF ty_data,
      ls_data TYPE ty_data.

2. Fill the Data

The second step is to gather the data to be displayed in the ALV. In this example, we'll get some material data from the MARA and MARC tables:

* Sample data gathering
SELECT m~matnr m~maktx c~werks c~lgort
  FROM mara AS m
  INNER JOIN marc AS c ON m~matnr = c~matnr
  INTO CORRESPONDING FIELDS OF TABLE lt_data
  UP TO 100 ROWS
  WHERE m~mtart = 'FERT'            " Only finished products
    AND c~werks IN ('1000', '2000') " Specific plants
  ORDER BY m~matnr.

* Fill in quantity and value information randomly as an example
LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
  <fs_data>-menge = ( sy-tabix + 1 ) * 10.
  <fs_data>-dmbtr = <fs_data>-menge * 25.
  <fs_data>-meins = 'PC'.
  <fs_data>-waers = 'USD'.
ENDLOOP.

3. Create the Field Catalog

The field catalog defines the appearance and behavior of columns in the ALV:

* Define variables needed for the field catalog
DATA: lt_fieldcat TYPE lvc_t_fcat,
      ls_fieldcat TYPE lvc_s_fcat,
      ls_layout   TYPE lvc_s_layo.

* Call the function that creates the field catalog
PERFORM create_fieldcatalog CHANGING lt_fieldcat.

* Field catalog creation form
FORM create_fieldcatalog CHANGING pt_fieldcat TYPE lvc_t_fcat.
  
  * Material number
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MATNR'.
  ls_fieldcat-coltext   = 'Material No'.
  ls_fieldcat-key       = 'X'.            " Key field
  ls_fieldcat-outputlen = 10.
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Material description
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MAKTX'.
  ls_fieldcat-coltext   = 'Material Description'.
  ls_fieldcat-outputlen = 30.
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Plant
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'WERKS'.
  ls_fieldcat-coltext   = 'Plant'.
  ls_fieldcat-outputlen = 5.
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Storage location
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LGORT'.
  ls_fieldcat-coltext   = 'Storage Loc.'.
  ls_fieldcat-outputlen = 5.
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Quantity
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname  = 'MENGE'.
  ls_fieldcat-coltext    = 'Quantity'.
  ls_fieldcat-outputlen  = 10.
  ls_fieldcat-datatype   = 'QUAN'.       " Quantity data type
  ls_fieldcat-no_zero    = 'X'.          " Don't show zeros
  ls_fieldcat-emphasize  = 'C500'.       " Coloring (light yellow)
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Unit of measure
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MEINS'.
  ls_fieldcat-coltext   = 'UoM'.
  ls_fieldcat-outputlen = 3.
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Amount
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname  = 'DMBTR'.
  ls_fieldcat-coltext    = 'Amount'.
  ls_fieldcat-outputlen  = 12.
  ls_fieldcat-datatype   = 'CURR'.       " Currency data type
  ls_fieldcat-no_zero    = 'X'.          " Don't show zeros
  ls_fieldcat-emphasize  = 'C300'.       " Coloring (light green)
  APPEND ls_fieldcat TO pt_fieldcat.
  
  * Currency
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname  = 'WAERS'.
  ls_fieldcat-coltext    = 'Currency'.
  ls_fieldcat-outputlen  = 3.
  APPEND ls_fieldcat TO pt_fieldcat.
  
ENDFORM.

4. Configure Layout Settings

Define layout settings for the ALV Grid:

* Configure layout settings
CLEAR ls_layout.
ls_layout-zebra      = 'X'.       " Zebra pattern background
ls_layout-cwidth_opt = 'X'.       " Automatically optimize column width
ls_layout-grid_title = 'Material Stock List'.  " Grid title
ls_layout-sel_mode   = 'A'.       " Row selection mode (A: all types of selection)

5. Create the Popup Window and ALV Grid

Now let's create the popup window and the ALV Grid inside it:

* Define the necessary objects for Popup ALV
DATA: lo_popup        TYPE REF TO cl_gui_dialogbox_container,
      lo_alv_grid     TYPE REF TO cl_gui_alv_grid,
      lv_screen_start TYPE i VALUE 100,
      lv_screen_end   TYPE i VALUE 100.

* Create the popup window
CREATE OBJECT lo_popup
  EXPORTING
    width                       = 800   " Width in pixels
    height                      = 400   " Height in pixels
    top                         = 50    " Position from top of screen
    left                        = 50    " Position from left of screen
    caption                     = 'Material Stock Status'  " Window title
    lifetime                    = cl_gui_control=>lifetime_dynpro.

* Attach the ALV Grid to the popup container
CREATE OBJECT lo_alv_grid
  EXPORTING
    i_parent = lo_popup.

* Register event handlers for the ALV Grid
SET HANDLER lcl_event_handler=>on_double_click FOR lo_alv_grid.
SET HANDLER lcl_event_handler=>on_toolbar     FOR lo_alv_grid.
SET HANDLER lcl_event_handler=>on_user_command FOR lo_alv_grid.

* Set up the first display of the ALV Grid
CALL METHOD lo_alv_grid->set_table_for_first_display
  EXPORTING
    i_save          = 'A'            " Save layout (A: automatic)
    is_layout       = ls_layout
  CHANGING
    it_outtab       = lt_data
    it_fieldcatalog = lt_fieldcat.

* Call the popup screen and start processing
CALL SCREEN lv_screen_start STARTING AT 10 10
                            ENDING AT 110 40.

6. Add Event Handling Class

Create an event handler class to process ALV events:

* Define the event handling class
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      on_double_click FOR EVENT double_click OF cl_gui_alv_grid
        IMPORTING
          e_row e_column es_row_no,
      on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object,
      on_user_command FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.
  METHOD on_double_click.
    DATA: lv_matnr TYPE matnr,
          lv_werks TYPE werks_d.
          
    READ TABLE lt_data INDEX es_row_no-row_id INTO ls_data.
    IF sy-subrc = 0.
      lv_matnr = ls_data-matnr.
      lv_werks = ls_data-werks.
      
      MESSAGE s000(0k) WITH 'Selected material:' lv_matnr
                           'Plant:' lv_werks.
    ENDIF.
  ENDMETHOD.
  
  METHOD on_toolbar.
    DATA: ls_toolbar TYPE stb_button.
    
    " Add show details button
    CLEAR ls_toolbar.
    ls_toolbar-function  = 'DETAIL'.
    ls_toolbar-icon      = icon_detail.
    ls_toolbar-text      = 'Details'.
    ls_toolbar-quickinfo = 'Show material details'.
    ls_toolbar-butn_type = 0.
    APPEND ls_toolbar TO e_object->mt_toolbar.
  ENDMETHOD.
  
  METHOD on_user_command.
    DATA: lt_selected_rows TYPE lvc_t_row,
          ls_selected_row  TYPE lvc_s_row.
    
    CASE e_ucomm.

      WHEN 'DETAIL'.
        " Get selected rows
        CALL METHOD lo_alv_grid->get_selected_rows
          IMPORTING
            et_index_rows = lt_selected_rows.
            
        IF lt_selected_rows IS INITIAL.
          MESSAGE 'Please select a row' TYPE 'I'.
        ELSE.
          READ TABLE lt_selected_rows INTO ls_selected_row INDEX 1.
          READ TABLE lt_data INDEX ls_selected_row-index INTO ls_data.
          IF sy-subrc = 0.
            MESSAGE s000(0k) WITH 'Details:' ls_data-matnr
                                 ls_data-maktx ls_data-werks.
          ENDIF.
        ENDIF.
    ENDCASE.
  ENDMETHOD.
ENDCLASS.

7. Define Screen Operations

Define PBO and PAI modules for the popup screen:

* PBO and PAI modules
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'POPUP'.
  SET TITLEBAR 'TITLE_100'.
ENDMODULE.

MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      " Close the popup window
      IF lo_popup IS BOUND.
        CALL METHOD lo_popup->free
          EXCEPTIONS
            OTHERS = 1.
      ENDIF.
      
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.

8. Making It a Function Module

Convert your code into a reusable function module:

FUNCTION z_display_popup_alv.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IT_DATA) TYPE ANY TABLE
*"     REFERENCE(IV_TITLE) TYPE STRING OPTIONAL
*"     REFERENCE(IV_WIDTH) TYPE I DEFAULT 800
*"     REFERENCE(IV_HEIGHT) TYPE I DEFAULT 400
*"----------------------------------------------------------------------
  DATA: lo_popup    TYPE REF TO cl_gui_dialogbox_container,
        lo_alv_grid TYPE REF TO cl_gui_alv_grid,
        lt_fieldcat TYPE lvc_t_fcat,
        ls_layout   TYPE lvc_s_layo.

  " Create popup container
  CREATE OBJECT lo_popup
    EXPORTING
      width    = iv_width
      height   = iv_height
      top      = 50
      left     = 50
      caption  = iv_title.

  " Create ALV Grid
  CREATE OBJECT lo_alv_grid
    EXPORTING
      i_parent = lo_popup.

  " Automatically create field catalog
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name = cl_abap_typedescr=>describe_by_data( it_data )
    CHANGING
      ct_fieldcat      = lt_fieldcat
    EXCEPTIONS
      OTHERS           = 1.

  " Configure layout settings
  ls_layout-zebra      = 'X'.
  ls_layout-cwidth_opt = 'X'.
  ls_layout-grid_title = iv_title.
  ls_layout-sel_mode   = 'A'.

  " Display the ALV
  CALL METHOD lo_alv_grid->set_table_for_first_display
    EXPORTING
      is_layout       = ls_layout
    CHANGING
      it_outtab       = it_data
      it_fieldcatalog = lt_fieldcat.

  " Wait for screen interaction
  DATA: lv_dummy TYPE c.
  cl_gui_cfw=>flush( ).
  cl_gui_cfw=>dispatch( ).
  READ LINE.

  " Free resources when closing
  IF lo_popup IS BOUND.
    CALL METHOD lo_popup->free.
  ENDIF.

ENDFUNCTION.

Implementation Example

Here's a complete example of how to use it in a program:

REPORT zpopup_alv_demo.

START-OF-SELECTION.
  PERFORM get_data_and_display.

FORM get_data_and_display.
  TYPES: BEGIN OF ty_material,
           matnr TYPE matnr,
           maktx TYPE maktx,
           werks TYPE werks_d,
           lgort TYPE lgort_d,
           menge TYPE menge_d,
           meins TYPE meins,
           dmbtr TYPE dmbtr,
           waers TYPE waers,
         END OF ty_material.

  DATA: lt_materials TYPE TABLE OF ty_material,
        ls_material  TYPE ty_material.

  " Gather sample data
  SELECT m~matnr m~maktx c~werks c~lgort
    FROM mara AS m
    INNER JOIN marc AS c ON m~matnr = c~matnr
    INTO CORRESPONDING FIELDS OF TABLE lt_materials
    UP TO 50 ROWS
    WHERE m~mtart = 'FERT'
    ORDER BY m~matnr.

  " Fill example values
  LOOP AT lt_materials ASSIGNING FIELD-SYMBOL(<fs_mat>).
    <fs_mat>-menge = ( sy-tabix + 1 ) * 10.
    <fs_mat>-dmbtr = <fs_mat>-menge * 25.
    <fs_mat>-meins = 'PC'.
    <fs_mat>-waers = 'USD'.
  ENDLOOP.

  " Call the Popup ALV function
  CALL FUNCTION 'Z_DISPLAY_POPUP_ALV'
    EXPORTING
      it_data  = lt_materials
      iv_title = 'Material Stock Status'.

ENDFORM.

Tips and Best Practices

Some tips to consider when creating Popup ALVs:

Memory management: Make sure to free all objects when the popup is closed
Window sizing: Use an appropriate window size for the amount of data displayed
Coloring: Use the emphasize property to highlight important fields
Buttons: Add useful toolbar buttons to enhance the user experience
Filtering: Enable filtering capabilities for large data sets
Column widths: Enable the cwidth_opt property for efficient use of space


Conclusion

Popup ALVs are powerful components that can significantly enhance the user experience of your SAP applications. In this guide, we have learned in detail how to create, customize, and use a functional Popup ALV. With this knowledge, you can develop more interactive and user-friendly applications for your users.

By implementing these techniques in your projects, you can provide your users with a more efficient and visually appealing data display experience.

Comments

0

You must be logged in to comment.

No comments yet.

Be the first to comment.

Emre Göçmen

Author & Developer

I write about my experiences as a SAP ABAP & Full Stack developer.

Category

SAP

SAP

Subscribe to Newsletter

Subscribe to my newsletter to get notified about new articles.