TOP-OF-PAGE in ALV using CL_SALV_TABLE

0
9087

In this tutorial, we will add TOP-OF-PAGE to the ALV report created using factory class CL_SALV_TABLE.

To add Top Of Page to the ALV report,

  • we need to create instance for the class CL_SALV_FORM_LAYOUT_GRID.
  • Create Label and Text to add content to the Top Of Page.
  • Call the method SET_TOP_OF_LIST to set the Top of page.

Let see with an example. Create a program in SE38 (ABAP Editor) and copy the below code.

REPORT  zsl_demo_oop_alv.
*----------------------------------------------------------------------*
*      TOP-OF-PAGE in ALV report using factory class CL_SALV_TABLE     *
*----------------------------------------------------------------------*
*                    www.saplearners.com                               *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
*       CLASS lcl_sflight DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight DEFINITION.

  PUBLIC SECTION.
    TYPES: BEGIN OF lty_sflight,
           carrid	         TYPE s_carr_id,
           connid	         TYPE s_conn_id,
           fldate	         TYPE s_date,
           price           TYPE s_price,
           currency	       TYPE s_currcode,
           planetype       TYPE s_planetye,
           seatsmax	       TYPE s_seatsmax,
           seatsocc	       TYPE s_seatsocc,
           END OF lty_sflight.

    METHODS: get_sflight_data,
             get_alv_instance,
             display.

    DATA: lo_alv      TYPE REF TO cl_salv_table,
          g_t_sflight TYPE STANDARD TABLE OF lty_sflight.

ENDCLASS.                    "lcl_sflight DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_sflight IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight IMPLEMENTATION.

* Get SFLIGHT data
  METHOD get_sflight_data.
    SELECT carrid connid fldate price currency planetype seatsmax
           seatsocc INTO TABLE me->g_t_sflight
                    FROM sflight.
  ENDMETHOD.                    "get_sflight_data

* Get ALV instance
  METHOD get_alv_instance.
    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = lo_alv
          CHANGING
            t_table      = g_t_sflight.
      CATCH cx_salv_msg.
    ENDTRY.

  ENDMETHOD.                    "get_alv_instance

* Display ALV
  METHOD display.
    CALL METHOD lo_alv->display.
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_sflight IMPLEMENTATION

START-OF-SELECTION.
  DATA: lo_cl_sflight     TYPE REF TO lcl_sflight.

  CREATE OBJECT lo_cl_sflight.
  lo_cl_sflight->get_sflight_data( ).
  lo_cl_sflight->get_alv_instance( ).

  DATA: lo_grid   TYPE REF TO cl_salv_form_layout_grid,
        lo_label  TYPE REF TO cl_salv_form_label,
        lo_text   TYPE REF TO cl_salv_form_text,
        lo_header TYPE REF TO cl_salv_form_element.

* Create an instance
  CREATE OBJECT lo_grid.

* 1st Row in Header
  lo_label = lo_grid->create_label(
    row     = 1
    column  = 1
    text    = 'Label Text(1)' ).

  lo_text = lo_grid->create_text(
    row     = 1
    column  = 2
    text    = 'Some Headet Text(2)' ).
  lo_label->set_label_for( lo_text ).

* 2nd Row in Header
  lo_label = lo_grid->create_label(
    row     = 2
    column  = 1
    text    = 'Label Text(2)' ).

  lo_text = lo_grid->create_text(
    row     = 2
    column  = 2
    text    = 'Some Headet Text(2)' ).
  lo_label->set_label_for( lo_text ).

* Set Top OF Page to the ALV
  CALL METHOD lo_cl_sflight->lo_alv->set_top_of_list
    EXPORTING
      value = lo_grid.

  lo_cl_sflight->display( ).

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!