SAP Tech Pro

SAP Technical Blog

How to add PF-Status, Header and Footer in ALV using class CL_SALV_TABLE

How to add PF-Status, Header and Footer in ALV using class CL_SALV_TABLE

ABAP developer’s life becomes easy with the introduction of the class CL_SALV_TABLE. This is really easy and fast way to display the data in ALV compared to the function module REUSE_ALV_GRID_DISPLAY or REUSE_ALV_LIST_DISPLAY.

In this article find out how to set PF-Status, Header and Footer to the ALV which is developed using class CL_SALV_TABLE.

Code Snippet


REPORT zstp_alv_pf_status_header.

*&---------------------------------------------------------------------*
*& Report  ZSTP_ALV_PF_STATUS_HEADER
*&
*&---------------------------------------------------------------------*
*& SAPTechPro.com
*&
*&---------------------------------------------------------------------*

CLASS lcl_report DEFINITION.
  PUBLIC SECTION.
*----------------------------------------------------------------------*
* Final Output Table
*----------------------------------------------------------------------*

    DATA: o_alv TYPE REF TO cl_salv_table,              " ALV Reference
          i_sflight TYPE STANDARD TABLE OF sflight.
*----------------------------------------------------------------------*
* Methods to Fetch Data and Generate Output
*----------------------------------------------------------------------*
    METHODS: get_data,                                  "Data Selection
             generate_output.                           "Generating Output
  PRIVATE SECTION.
*----------------------------------------------------------------------*
* Methods to Set PF-Status, Header and Footer
*----------------------------------------------------------------------*
    METHODS: set_pf_status
                  CHANGING
                      co_alv TYPE REF TO cl_salv_table, " Default Pf Status
            set_top_of_page
                  CHANGING
                      co_alv TYPE REF TO cl_salv_table, " Set Top of page
            set_end_of_page
                  CHANGING
                      co_alv TYPE REF TO cl_salv_table. " Set End of page
ENDCLASS.                    "lcl_report DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*----------------------------------------------------------------------*
* Data selection
*----------------------------------------------------------------------*
  METHOD get_data.
    SELECT * INTO TABLE i_sflight
           FROM sflight UP TO 100 ROWS.
  ENDMETHOD.                    "get_data
*----------------------------------------------------------------------*
* Generating Output
*----------------------------------------------------------------------*
  METHOD generate_output.
*Exception Class
    DATA: lc_msg TYPE REF TO cx_salv_msg.
*----------------------------------------------------------------------*
* We are calling the static Factory method which will give back
* the ALV object reference.
*----------------------------------------------------------------------*
    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = o_alv
          CHANGING
            t_table      = i_sflight.
      CATCH cx_salv_msg INTO lc_msg .
    ENDTRY.
************************************************************************
* In this area we will call the methods which will set the
* different properties to the ALV
************************************************************************
* Calling Set PF status method
    CALL METHOD set_pf_status
      CHANGING
        co_alv = o_alv.       "set_end_of_page
* Calling the top of page method
    CALL METHOD set_top_of_page
      CHANGING
        co_alv = o_alv.
* Calling the End of page method
    CALL METHOD set_end_of_page
      CHANGING
        co_alv = o_alv.
************************************************************************
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
************************************************************************
    o_alv->display( ).
  ENDMETHOD.                    "generate_output
************************************************************************
*    In this area we will implement the methods which are defined in
*    the class definition
************************************************************************
* Setting Default PF-Status
  METHOD set_pf_status.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
* Default functions
    lo_functions = co_alv->get_functions( ).
*    lo_functions->set_default( abap_true ).
    lo_functions->set_all( abap_true ).
  ENDMETHOD.                    "set_pf_status


* Setting Top_of_page
  METHOD set_top_of_page.
    DATA: lo_header TYPE REF TO cl_salv_form_layout_grid,
          lo_h_label TYPE REF TO cl_salv_form_label,
          lo_h_flow  TYPE REF TO cl_salv_form_layout_flow.
* Header object
    CREATE OBJECT lo_header.
*----------------------------------------------------------------------*
* To create a Label or Flow we have to specify the target
* row and column number where we need to set up the output
* text.
*----------------------------------------------------------------------*
* Information in Bold
    lo_h_label = lo_header->create_label( row = 1 column = 1 ).
    lo_h_label->set_text('Header of the ALV Output in Bold').
* Information in tabular format
    lo_h_flow = lo_header->create_flow( row = 2 column = 1 ).
    lo_h_flow->create_text( text = 'This is text in Header' ).
    lo_h_flow = lo_header->create_flow( row = 3 column = 1 ).
    lo_h_flow->create_text( text = 'Date' ).
    lo_h_flow = lo_header->create_flow( row = 3 column = 2 ).
    lo_h_flow->create_text( text = sy-datum ).
* Set the top of list using the header for Online
    co_alv->set_top_of_list( lo_header ).
* Set the top of list using the header for Print
    co_alv->set_top_of_list_print( lo_header ).
  ENDMETHOD.                    "set_top_of_page


* Setting End_Of_page
  METHOD set_end_of_page.
    DATA: lo_footer  TYPE REF TO cl_salv_form_layout_grid,
          lo_f_label TYPE REF TO cl_salv_form_label,
          lo_f_flow  TYPE REF TO cl_salv_form_layout_flow.
* Footer Object
    CREATE OBJECT lo_footer.
* Information in Bold
    lo_f_label = lo_footer->create_label( row = 1 column = 1 ).
    lo_f_label->set_text('Footer of the ALV in Bold').
* Tabular Information
    lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ).
    lo_f_flow->create_text( text = 'This is text in footer' ).


* Set the end of list using the header for Online
    co_alv->set_end_of_list( lo_footer ).
* Set the End of list using the header for Print
    co_alv->set_end_of_list_print( lo_footer ).
  ENDMETHOD.                    "set_end_of_page
ENDCLASS.                    "lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
  DATA: lo_report TYPE REF TO lcl_report.
  CREATE OBJECT lo_report.
  lo_report->get_data( ).
  lo_report->generate_output( ).
 

Output:

How to add PF-Status, Header and Footer in ALV using class CL_SALV_TABLE

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in