Add header to ALV grid

Add header to ALV grid

With class CL_GUI_ALV_GRID
 
With FM REUSE_ALV_GRID_DISPLAY


 

With class CL_GUI_ALV_GRID

Example of use :


REPORT zad_test_alv
       NO STANDARD PAGE HEADING
       MESSAGE-ID zcs_cs_001.

************************************************************************
*                    DATA DECLARATIONS
************************************************************************

************************************************************************
* Table Declarations:
************************************************************************
TABLES: mara.


************************************************************************
* Internal Tables:
************************************************************************
DATA:  i_fieldcat    TYPE lvc_t_fcat,
       i_fieldrows   TYPE lvc_t_row,
       i_output      TYPE STANDARD TABLE OF mara,


************************************************************************
* Work Areas:
************************************************************************
      w_fieldrows   LIKE LINE OF i_fieldrows,
      w_output      TYPE mara.

************************************************************************
* Constants Declarations:
************************************************************************
CONSTANTS:    c_x                        TYPE c VALUE 'X'.


*----------------------------------------------------------------------*
*             SELECTION SCREEN FIELDS
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b_0 WITH FRAME TITLE text-000.
SELECT-OPTIONS: s_mara FOR mara-matnr.
SELECTION-SCREEN END OF BLOCK b_0.



*----------------------------------------------------------------------*
*                   CLASS DECLARATIONS                                 *
*----------------------------------------------------------------------*

INCLUDE .
* Predefine a local class for event handling to allow the
* declaration of a reference variable before the class is defined.
CLASS lcl_event_receiver DEFINITION DEFERRED.

DATA : o_alvgrid          TYPE REF TO cl_gui_alv_grid ,
       o_dockingcontainer TYPE REF TO cl_gui_docking_container ,
       o_eventreceiver    TYPE REF TO lcl_event_receiver,
       v_split            TYPE REF TO cl_gui_easy_splitter_container,
       v_contnr_top       TYPE REF TO cl_gui_container,
       v_contnr_bot       TYPE REF TO cl_gui_container,
       v_grid_02          TYPE REF TO cl_gui_alv_grid,
       v_html             TYPE REF TO cl_dd_document,
       v_text01(255)      TYPE c VALUE 'This is the heading',
       v_font(50)         TYPE c VALUE '30',
*---------------------------------------------------------------------*
*       Work Area
*---------------------------------------------------------------------*
       w_layout TYPE lvc_s_layo ,
       w_variant TYPE disvariant.
*---------------------------------------------------------------------*
*       Constants
*---------------------------------------------------------------------*

CONSTANTS : c_a(1) TYPE c VALUE 'A' .                     " All Layouts
*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.

  PUBLIC SECTION.
    CLASS-METHODS:
* Hot Spot Click
       handle_hotspot
         FOR EVENT hotspot_click OF cl_gui_alv_grid
            IMPORTING e_row_id
                      e_column_id
                      es_row_no,
      handle_print_top_of_page
            FOR EVENT print_top_of_page OF
                      cl_gui_alv_grid,

      handle_top_of_page
           FOR EVENT top_of_page OF
                      cl_gui_alv_grid.
ENDCLASS.
* Implementation
CLASS lcl_event_receiver IMPLEMENTATION.
*&---------------------------------------------------------------------*
*&      Method handle_hotspot
*&---------------------------------------------------------------------*
* This method is called when the user clicks on a hotspot to drill down.
* The following types are exported from the ALV
* LVC_S_ROW
* LVC_S_COL
* LVC_S_ROID
*&---------------------------------------------------------------------*
  METHOD handle_hotspot.
* The hotspot processing coded in the form below.
    PERFORM f9802_handle_hotspot USING e_row_id
                                       e_column_id
                                       es_row_no.

  ENDMETHOD.
  METHOD handle_print_top_of_page.
    IF sy-pagno = 1.
      PERFORM f9803_top_of_page.
    ENDIF.
  ENDMETHOD.
  METHOD handle_top_of_page.
    PERFORM f9803_top_of_page.
  ENDMETHOD.
ENDCLASS.


***********************************************************************
* START-OF-SELECTION
***********************************************************************
START-OF-SELECTION.

  PERFORM f5000_get_data.

************************************************************************
** END-OF-SELECTION
************************************************************************

END-OF-SELECTION.
************************************************************************
  IF NOT i_output[] IS INITIAL.
    CALL SCREEN 9001.
  ELSE.
    MESSAGE i001 WITH text-051.
    " No documents were found for the selection
    LEAVE LIST-PROCESSING.
  ENDIF.


*----------------------------------------------------------------------*
*                           FORMS                                      *
*----------------------------------------------------------------------*
* This part has the various forms used in the program
************************************************************************

*&---------------------------------------------------------------------*
*&      Form  f5000_get_totals
*&---------------------------------------------------------------------*
*       To get Data
*----------------------------------------------------------------------*
FORM f5000_get_data.

  SELECT *
         INTO TABLE i_output UP TO 5 ROWS
         FROM mara.


ENDFORM.                    " f5000_get_data

*&---------------------------------------------------------------------*
*&      Form  f9000_objects_create
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f9000_objects_create.

  IF cl_gui_alv_grid=>offline( ) IS INITIAL.
    CREATE OBJECT o_dockingcontainer
      EXPORTING
        ratio                       = '95'
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        others                      = 6.
    IF sy-subrc NE 0.
      MESSAGE i001 WITH text-e01."Error in creating Docking container
      LEAVE LIST-PROCESSING.
    ENDIF.
    CREATE OBJECT v_split
         EXPORTING
           parent            = o_dockingcontainer
*          ORIENTATION       = 0
           sash_position     = 25
           with_border       = 0
         EXCEPTIONS
           cntl_error        = 1
           cntl_system_error = 2
           others            = 3.
    IF sy-subrc NE 0.
      MESSAGE i000 WITH text-e01."Error in creating Docking container
      LEAVE LIST-PROCESSING.
    ENDIF.
*   Get the containers of the splitter control
    v_contnr_top = v_split->top_left_container.
    v_contnr_bot = v_split->bottom_right_container.

    CREATE OBJECT o_alvgrid
   EXPORTING
     i_parent = o_dockingcontainer.

*   Create an instance of alv control
    CREATE OBJECT o_alvgrid
         EXPORTING i_parent = v_contnr_bot.

*   Object for display of selection parameters in HTML top container
    CREATE OBJECT v_html
         EXPORTING
           style = 'ALV_GRID'.


*   Must be after the SET HANDLER for TOP_OF_PAGE and foreground only
    CALL METHOD o_alvgrid->list_processing_events
                     EXPORTING i_event_name = 'TOP_OF_PAGE'
                               i_dyndoc_id  = v_html.
    CLEAR v_text01.

    v_text01     = 'Heading'.


    v_font = 'HEADING'.
    CALL METHOD v_html->add_gap
                EXPORTING
                  width         = 120.
    CALL METHOD v_html->add_text
           EXPORTING
             text           = v_text01
             sap_style      = v_font.

    CALL METHOD v_html->new_line.
* Display the data
    CALL METHOD v_html->display_document
      EXPORTING
         parent             = v_contnr_top.

*   Handle the event
    CALL METHOD o_alvgrid->list_processing_events
                        EXPORTING i_event_name = 'PRINT_TOP_OF_PAGE'.
  ENDIF.

* IF Program Running in Background, place a container.
  IF sy-batch = 'X'.
    CREATE OBJECT o_alvgrid
 EXPORTING
   i_parent = o_dockingcontainer.
  ENDIF.

ENDFORM.                    " f9000_objects_create

*&---------------------------------------------------------------------*
*&      Form  f9001_build_field_cat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_FIELDCAT  text
*      -->P_0021   text
*----------------------------------------------------------------------*
FORM f9001_build_field_cat TABLES   p_fieldcat STRUCTURE lvc_s_fcat
                      USING value(p_structure).

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name       = p_structure
       CHANGING
            ct_fieldcat            = p_fieldcat[]
       EXCEPTIONS
            inconsistent_interface = 1
            program_error          = 2
            OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE i001 WITH text-e05."Error in ALV field catalogue creation
    LEAVE LIST-PROCESSING.
  ENDIF.

ENDFORM.                    " f9001_build_field_cat
*&---------------------------------------------------------------------*
*&      Form  f9002_modify_field_cat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_FIELDCAT  text
*----------------------------------------------------------------------*
FORM f9002_modify_field_cat TABLES p_fieldcat STRUCTURE lvc_s_fcat.

  FIELD-SYMBOLS :  TYPE lvc_s_fcat.


ENDFORM.                    " f9002_modify_field_cat
*&---------------------------------------------------------------------*
*&      Form  f9003_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SY_TITLE  text
*      -->P_0030   text
*      -->P_0031   text
*      -->P_0032   text
*----------------------------------------------------------------------*
FORM f9003_layout USING  value(ptitle)
                         value(pzebra)
                         value(pmode)
                         value(pwidth).

  w_layout-grid_title  = ptitle.
  w_layout-zebra       = pzebra.
  w_layout-sel_mode    = pmode.
  w_layout-cwidth_opt  = pwidth.
  w_variant-report     = sy-repid.

ENDFORM.                    " f9003_layout
*&---------------------------------------------------------------------*
*&      Form  f9004_display_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_OUTPUT  text
*      -->P_I_FIELDCAT  text
*----------------------------------------------------------------------*
FORM f9004_display_data TABLES   p_output
                                 p_fieldcat.

  IF sy-batch = 'X'.
    CREATE OBJECT o_eventreceiver.
    SET HANDLER o_eventreceiver->handle_print_top_of_page FOR o_alvgrid.
  ENDIF.

  CALL METHOD o_alvgrid->set_table_for_first_display
    EXPORTING
       is_variant                    = w_variant
       i_save                        = c_a
       is_layout                     = w_layout
    CHANGING
       it_outtab                     = p_output[]
       it_fieldcatalog               = p_fieldcat[]
    EXCEPTIONS
       invalid_parameter_combination = 1
       program_error                 = 2
       too_many_lines                = 3
       OTHERS                        = 4.

  IF sy-subrc <> 0.
    MESSAGE i128 WITH text-e06."Error in ALV report display
    LEAVE LIST-PROCESSING.
  ENDIF.

  IF sy-batch NE 'X'.
    CREATE OBJECT o_eventreceiver.
    SET HANDLER o_eventreceiver->handle_print_top_of_page FOR o_alvgrid.
    SET HANDLER o_eventreceiver->handle_top_of_page FOR o_alvgrid.
  ENDIF.

ENDFORM.                    " f9004_display_data
*&---------------------------------------------------------------------*
*&      Form  f9005_free_objects
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_O_ALVGRID  text
*      -->P_0056   text
*      -->P_TEXT_E02  text
*----------------------------------------------------------------------*
FORM f9005_free_objects USING pobject
                        value(ptype)
                        value(ptext).
  CASE ptype.

    WHEN 'ALV'.
      DATA lobjectalv TYPE REF TO cl_gui_alv_grid.
      lobjectalv = pobject.
      IF NOT ( lobjectalv IS INITIAL ).
        CALL METHOD lobjectalv->free
          EXCEPTIONS
            cntl_error        = 1
            cntl_system_error = 2
            OTHERS            = 3.
        CLEAR: pobject ,
               lobjectalv.
        PERFORM f9006_error_handle USING ptext.
      ENDIF.

    WHEN 'DOCKING'.
      DATA lobjectdock TYPE REF TO cl_gui_docking_container.
      lobjectdock = pobject.
      IF NOT ( lobjectdock IS INITIAL ).
        CALL METHOD lobjectdock->free
          EXCEPTIONS
            cntl_error        = 1
            cntl_system_error = 2
            OTHERS            = 3.
        CLEAR: pobject ,
               lobjectdock.
        PERFORM f9006_error_handle USING ptext.
      ENDIF.

    WHEN 'CONTAINER'.
      DATA lobjectcontainer TYPE REF TO cl_gui_container.
      lobjectcontainer = pobject.
      IF NOT ( lobjectcontainer IS INITIAL ).
        CALL METHOD lobjectcontainer->free
          EXCEPTIONS
            cntl_error        = 1
            cntl_system_error = 2
            OTHERS            = 3.
        CLEAR: pobject ,
               lobjectcontainer.
        PERFORM f9006_error_handle USING ptext.
      ENDIF.

    WHEN OTHERS.
      sy-subrc = 1.
      PERFORM f9006_error_handle USING text-e04.
  ENDCASE.
ENDFORM.                    " f9005_free_objects
*&---------------------------------------------------------------------*
*&      Form  f9006_error_handle
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_PTEXT  text
*----------------------------------------------------------------------*
FORM f9006_error_handle USING value(ptext).

  IF sy-subrc NE 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              titel = text-e03
              txt2  = sy-subrc
              txt1  = ptext.
  ENDIF.

ENDFORM.                    " f9006_error_handle
*&---------------------------------------------------------------------*
*&      Form  f9802_handle_hotspot
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_E_ROW_ID  text
*      -->P_E_COLUMN_ID  text
*      -->P_ES_ROW_NO  text
*----------------------------------------------------------------------*
FORM f9802_handle_hotspot USING    p_row_id
                                   p_column_id
                                   p_row_no.


ENDFORM.                    " f9802_handle_hotspot
*&---------------------------------------------------------------------*
*&      Form  f9803_top_of_page
*&---------------------------------------------------------------------*
*      TOP-OF-PAGE
*----------------------------------------------------------------------*
FORM f9803_top_of_page.
* List heading
  WRITE:/ v_text01.
ENDFORM.                    " f9803_top_of_page



*----------------------------------------------------------------------*
*   INCLUDE ZCSGM_MFRECON_SUMMRPT_LD_SCRN                              *
************************************************************************
*&---------------------------------------------------------------------*
*&      Module  STATUS_1001  OUTPUT
*&---------------------------------------------------------------------*
*       This performs PBO
*----------------------------------------------------------------------*
MODULE  status_9001 OUTPUT.

  IF o_dockingcontainer IS INITIAL.
    SET PF-STATUS 'ZSTATUS'.
    SET TITLEBAR  'ZTITLE'.
*   Creating Object
    PERFORM f9000_objects_create.

*   Building the field catalog
    PERFORM f9001_build_field_cat TABLES i_fieldcat
                            USING 'MARA'.
  ENDIF.
*   Modifying the field catalog
  PERFORM f9002_modify_field_cat TABLES i_fieldcat.

*   For Layout
  PERFORM f9003_layout USING sy-title 'X' 'B' 'X' .
*   To display output
  PERFORM f9004_display_data TABLES i_output
                                    i_fieldcat.
* Set handlers for events
  SET HANDLER o_eventreceiver->handle_hotspot       FOR o_alvgrid.

ENDMODULE.                 " STATUS_9001  OUTPUT

*&---------------------------------------------------------------------
*&      Module  USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------
*       This performs PAI
*----------------------------------------------------------------------*

MODULE user_command_9001 INPUT.

  CASE sy-ucomm.
    WHEN 'EXIT' OR  'CANC'.
      PERFORM f9005_free_objects :
              USING o_alvgrid 'ALV' text-e02 ,
              USING o_dockingcontainer 'DOCKING' text-e01.
      LEAVE PROGRAM.
    WHEN 'BACK'.
      PERFORM f9005_free_objects :
              USING o_alvgrid 'ALV' text-e02 ,
              USING o_dockingcontainer 'DOCKING' text-e01.
      SET SCREEN '0'.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_9001  INPUT

Source: https://answers.sap.com/questions/1234056/create-heading-header-for-display-with-clguialvgri.html


 

With FM REUSE_ALV_GRID_DISPLAY

Use FM REUSE_ALV_COMMENTARY_WRITE.

Example of use :


  REPORT zwa_alv_editable_fm .

  TYPE-POOLS: slis.

  DATA it_fieldcat TYPE slis_fieldcat_alv OCCURS 0 WITH HEADER LINE.

  DATA: gs_layout TYPE slis_layout_alv.

  DATA v_repid LIKE sy-repid.


  DATA: BEGIN OF itab OCCURS 0.
          INCLUDE STRUCTURE cskt.
  DATA: END OF itab.


START-OF-SELECTION.

  v_repid = sy-repid.

*--- selection form cost center master table

  SELECT * FROM cskt
  INTO TABLE itab.

  PERFORM field_catalog.



  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     = v_repid
      it_fieldcat            = it_fieldcat[]
      is_layout              = gs_layout
      i_callback_top_of_page = 'TOP-OF-PAGE1'
      i_grid_title           = 'xyz'
      i_default              = 'X'
      i_save                 = 'U'
    TABLES
      t_outtab               = itab.


*&---------------------------------------------------------------------*
*&      Form  field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM field_catalog.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name     = v_repid
      i_internal_tabname = 'ITAB'
      i_inclname         = v_repid
    CHANGING
      ct_fieldcat        = it_fieldcat[].


ENDFORM.                    "field_catalog


*---------------------------------------------------------------------*
*       FORM top-of-page1                                             *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM top-of-page1.
  DATA: header TYPE slis_t_listheader,
  wa TYPE slis_listheader.

  wa-typ = 'S'.
  wa-info = 'dd'.
  APPEND wa TO header.

  wa-typ = 'S'.
  WRITE sy-datum TO wa-info MM/DD/YYYY.
  CONCATENATE 'abc' wa-info INTO wa-info SEPARATED BY space.
  APPEND wa TO header.

  wa-typ = 'S'.
  CONCATENATE 'cd' sy-uname INTO wa-info SEPARATED BY space.
  APPEND wa TO header.

  wa-typ = 'S'.
  CONCATENATE 'ee' sy-repid INTO wa-info SEPARATED BY space.
  APPEND wa TO header.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = header.




ENDFORM.                    "top-of-page1

Source: https://answers.sap.com/questions/1675542/adding-header-to-alv.html

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply