效果图:

ALV demo 03:cl_salv_table :Add Header (Top of page_java


code:

*&---------------------------------------------------------------------*
*& Report  ZLM_ALV002
*&
*&---------------------------------------------------------------------*
*& 
*&
*&---------------------------------------------------------------------*
REPORT ZLM_ALV002.

*----------------------------------------------------------------------*
*       Code to Apply Filters to SALV Table Display
*&      sapliumeng  SAP干货铺
*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS LCL_REPORT DEFINITION.
*
PUBLIC SECTION.
*

*
  DATAGT_SPFLI TYPE STANDARD TABLE OF SPFLI.
*
*   ALV reference
  DATAO_ALV TYPE REF TO CL_SALV_TABLE.
*
  METHODS:
*     data selection
    GET_DATA,
*
*     Generating output
    GENERATE_OUTPUT.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
*    In this section we will define the private methods which can
*      be implemented to set the properties of the ALV and can be
*      called in the
*
PRIVATE SECTION.
  METHODS:
    SET_PF_STATUS
      CHANGING
        CO_ALV TYPE REF TO CL_SALV_TABLE.
*
  METHODS:
    SET_FILTERS
      CHANGING
        CO_ALV TYPE REF TO CL_SALV_TABLE.


*   Set Top of page
  METHODS:
    SET_TOP_OF_PAGE
      CHANGING
        CO_ALV TYPE REF TO CL_SALV_TABLE.
*
*   Set End of page
  METHODS:
    SET_END_OF_PAGE
      CHANGING
        CO_ALV TYPE REF TO CL_SALV_TABLE.

*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS.                    "lcl_report DEFINITION
*
*
START-OF-SELECTION.
  DATALO_REPORT TYPE REF TO LCL_REPORT.
*
  CREATE OBJECT LO_REPORT.
*
  LO_REPORT->GET_DATA).
*
  LO_REPORT->GENERATE_OUTPUT).
*
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS LCL_REPORT IMPLEMENTATION.
*
  METHOD GET_DATA.
*   data selection
    SELECT INTO TABLE GT_SPFLI
           FROM  SPFLI.
*
    ENDMETHOD.                    "get_data
*
*.......................................................................
    METHOD GENERATE_OUTPUT.
* New ALV instance
*   We are calling the static Factory method which will give back
*   the ALV object reference.
*
* exception class
      DATALX_MSG TYPE REF TO CX_SALV_MSG.
      TRY.
          CL_SALV_TABLE=>FACTORY(
            IMPORTING
              R_SALV_TABLE O_ALV
            CHANGING
              T_TABLE      GT_SPFLI ).
        CATCH CX_SALV_MSG INTO LX_MSG.
      ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
*    In this area we will call the methods which will set the
*      different properties to the ALV
*
*   Set default PF status
      CALL METHOD SET_PF_STATUS
        CHANGING
          CO_ALV O_ALV.
*
*   Set Filters
      CALL METHOD SET_FILTERS
        CHANGING
          CO_ALV O_ALV.

*   Calling the top of page method
      CALL METHOD ME->SET_TOP_OF_PAGE
        CHANGING
          CO_ALV O_ALV.
*
*   Calling the End of Page method
      CALL METHOD ME->SET_END_OF_PAGE
        CHANGING
          CO_ALV O_ALV.


*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen
      O_ALV->DISPLAY).
*
    ENDMETHOD.                    "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
*    In this area we will implement the methods which are defined in
*      the class definition
*
*
    METHOD SET_PF_STATUS.
*
      DATALO_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS_LIST.
*
      LO_FUNCTIONS CO_ALV->GET_FUNCTIONS).
      LO_FUNCTIONS->SET_DEFAULTABAP_TRUE ).
*
    ENDMETHOD.                    "set_pf_status
*
    METHOD SET_FILTERS.
*
      DATALO_FILTERS TYPE REF TO CL_SALV_FILTERS.
*
      LO_FILTERS CO_ALV->GET_FILTERS).
*
*   Set the filter for the column ERDAT
*     the filter criteria works exactly same as any
*     RANGE or SELECT-OPTIONS works.
      TRY.
          CALL METHOD LO_FILTERS->ADD_FILTER
            EXPORTING
              COLUMNNAME 'CARRID'
              SIGN       'I'
              OPTION     'EQ'
              LOW        'LH'
*             high       =
            .
        CATCH CX_SALV_NOT_FOUND .                       "#EC NO_HANDLER
        CATCH CX_SALV_DATA_ERROR .                      "#EC NO_HANDLER
        CATCH CX_SALV_EXISTING .                        "#EC NO_HANDLER
      ENDTRY.
*
*
    ENDMETHOD.                    "set_filters
*
    METHOD SET_TOP_OF_PAGE.
*
      DATALO_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 Lable 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_LABELROW COLUMN ).
      LO_H_LABEL->SET_TEXT'Header in Bold' ).
*
*   information in tabular format
      LO_H_FLOW LO_HEADER->CREATE_FLOWROW 2  COLUMN ).
      LO_H_FLOW->CREATE_TEXTTEXT 'This is text of flow' ).
*
      LO_H_FLOW LO_HEADER->CREATE_FLOWROW 3  COLUMN ).
      LO_H_FLOW->CREATE_TEXTTEXT 'Number of Records in the output' ).
*
      LO_H_FLOW LO_HEADER->CREATE_FLOWROW 3  COLUMN ).
      LO_H_FLOW->CREATE_TEXTTEXT ).
*
*   set the top of list using the header for Online.
      CO_ALV->SET_TOP_OF_LISTLO_HEADER ).
*
*   set the top of list using the header for Print.
      CO_ALV->SET_TOP_OF_LIST_PRINTLO_HEADER ).
*
    ENDMETHOD.                    "set_top_of_page
*
    METHOD SET_END_OF_PAGE.
*
      DATALO_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_LABELROW COLUMN ).
      LO_F_LABEL->SET_TEXT'Footer .. here it goes' ).
*
*   tabular information
      LO_F_FLOW LO_FOOTER->CREATE_FLOWROW 2  COLUMN ).
      LO_F_FLOW->CREATE_TEXTTEXT 'This is text of flow in footer' ).
*
      LO_F_FLOW LO_FOOTER->CREATE_FLOWROW 3  COLUMN ).
      LO_F_FLOW->CREATE_TEXTTEXT 'Footer number' ).
*
      LO_F_FLOW LO_FOOTER->CREATE_FLOWROW 3  COLUMN ).
      LO_F_FLOW->CREATE_TEXTTEXT ).
*
*   Online footer
      CO_ALV->SET_END_OF_LISTLO_FOOTER ).
*
*   Footer in print
      CO_ALV->SET_END_OF_LIST_PRINTLO_FOOTER ).
*
    ENDMETHOD.                    "set_end_of_page

*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS.                    "lcl_report IMPLEMENTATIO