ALV tutorial 02 – PF-STATUS, GUI title and exception lights

SAPIn this step we’ll customize our ALV with custom PF-STATUS (buttons, menu, …), GUI title and we’ll also add a column with exception lights (Red/Yellow/Green)

  1. We need to modify our table gt_data with SFLIGHT data to hold one more information – exception light.
    TYPES: BEGIN OF ty_data.
            INCLUDE STRUCTURE sflight.
    TYPES:   exc_light(1) TYPE c,        " new field to hold the exception light
           END OF ty_data.
    
    DATA: gt_data TYPE TABLE OF ty_data.
  2. Then we have to fill this info field with data – most probably during data retrieval so in our case in FORM get_data:
    FORM read_data.
      DATA:
        l_color TYPE c.
    
      FIELD-SYMBOLS:
        <fs_data> TYPE ty_data.
    
      SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
        FROM sflight
        UP TO c_max_rows ROWS.
    
      LOOP AT gt_data ASSIGNING <fs_data>.
        IF <fs_data>-seatsocc = <fs_data>-seatsmax OR
           <fs_data>-seatsocc_b = <fs_data>-seatsmax_b OR
          <fs_data>-seatsocc_f = <fs_data>-seatsmax_f.
          <fs_data>-exc_light = 2.             " Yellow - some class is full
        ELSEIF <fs_data>-seatsocc = <fs_data>-seatsmax AND
               <fs_data>-seatsocc_b = <fs_data>-seatsmax_b AND
               <fs_data>-seatsocc_f = <fs_data>-seatsmax_f.
          <fs_data>-exc_light = 1.             " Red - the flight is full
        ELSE.
          <fs_data>-exc_light = 3.             " Green - seats available
        ENDIF.
      ENDLOOP.
    ENDFORM.
  3. We have to tell our ALV which field holds the exception light information so we have to add this information to layout (new variable ls_layout) of ALV used during it’s display:
    FORM display_grid.
      DATA: ls_layout TYPE lvc_s_layo.
    
      ls_layout-excp_fname = 'EXC_LIGHT'.
    
      CREATE OBJECT g_grid
        EXPORTING
          i_parent = cl_gui_container=>default_screen.
    
      CALL METHOD g_grid->set_table_for_first_display
        EXPORTING
          i_structure_name              = 'SFLIGHT'
          is_layout                     = ls_layout
        CHANGING
          it_outtab                     = gt_data
        EXCEPTIONS
          invalid_parameter_combination = 1
          program_error                 = 2
          too_many_lines                = 3
          OTHERS                        = 4.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    ENDFORM.
  4. We are now done with the exception light and we can step into adding the PF-STATUS (modify the toolbar, define response on user actions, …)
    First of all we will separate the PF-STATUS and titlebar modification from our code so we’ll add new module into screen 0100 PBO section (we’ll put it’s definition into main program).
    At the same time we add new module for handling user actions to the PAI section (we’ll create it’s definition in the main program)

    PROCESS BEFORE OUTPUT.
      MODULE status_0100.
      MODULE display_grid.
    *
    PROCESS AFTER INPUT.
      MODULE user_command_0100.
  5. We’ll take it step-by step so the first will be MODULE status_0100
    MODULE status_0100 OUTPUT.
      SET PF-STATUS 'MAIN'.
      SET TITLEBAR 'ALV_EXAMPLES'.
    ENDMODULE.

    In the above code you can see we set the TITLEBAR to ALV_EXAMPLES – double click on ALV_EXAMPLES, create new GUI title object and set your desired title text. We alse set name of the PF-STATUS 'MAIN'. We’ll create this status in object tree by right-click on the program name -> Create -> GUI status and name it MAIN. In our program we won’t handle any special user commands so we’ll implement only the BACK, EXIT and CANCEL actions.
    ALV PF-STATUS creation

  6. The next is new MODULE display_grid where we moved our data handling and calling the ALV display (separation into modules depends only on your choice)
    MODULE display_grid OUTPUT.
      PERFORM read_data.
      PERFORM display_grid.
    ENDMODULE.
  7. The last part is handling user input in MODULE user_command_0100
    MODULE user_command_0100 INPUT.
    *   to react on oi_custom_events:
        call method cl_gui_cfw=>dispatch.
      CASE sy-ucomm.
        WHEN 'BACK' OR 
             'EXIT' OR 
             'CANCEL'.
          LEAVE PROGRAM.
        WHEN OTHERS.
    *     do nothing
      ENDCASE.  
    ENDMODULE.
  8. Output of our program will be like on the following picturePF-STATUS, GUI title and Exception lights

3 thoughts on “ALV tutorial 02 – PF-STATUS, GUI title and exception lights

  1. Hello,

    Thank you very much for your tutorial. Their is mistake on the condition of step 2, you have to reverse the condition test of yellow and red light otherwise we will never go to red condition test.

  2. Programa Z04_RESERVAS_ALV linea 36
    Field “C_MAX_ROWS” is unknown. It is neither in one of the specified
    tables nor defined by a “DATA” statement . . . . . . . . . .

  3. Programa Z04_RESERVAS_ALV linea 60
    Field “G_GRID” is unknown. It is neither in one of the specified tables
    nor defined by a “DATA” statement . . . . . . . . . .

Leave a Reply to Youssef.BCancel reply