Thursday, March 17, 2011

ALV using Class cl_salv_table

 A sample program to create ALV using class cl_salv_table.


*&---------------------------------------------------------------------*
*& Report  ZOOPS_SALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zoops_salv.

*----------------------------------------------------------------------*
*       CLASS lcl_head DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_head DEFINITION.
  PUBLIC SECTION.

    TYPES: BEGIN OF ty_mara,
             matnr TYPE matnr,
             ersda TYPE ersda,
             matkl TYPE matkl,
             mtart TYPE mtart,
             lvorm TYPE lvorm,
            END OF ty_mara.

    DATA: o_alv TYPE REF TO cl_salv_table,
          t_mara TYPE STANDARD TABLE OF ty_mara.

    METHODS : get_data,
              disp_output.

  PRIVATE SECTION.
    METHODS : set_pf_status
              CHANGING co_alv TYPE REF TO cl_salv_table,
              set_top_of_page
              CHANGING co_alv TYPE REF TO cl_salv_table,
              set_end_of_page
              CHANGING co_alv TYPE REF TO cl_salv_table.
ENDCLASS.                    "lcl_head DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_head IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS  lcl_head IMPLEMENTATION.

  METHOD get_data.
    SELECT matnr ersda matkl mtart lvorm INTO TABLE t_mara
           FROM mara UP TO 20 ROWS.
  ENDMETHOD.                    "GET_DATA

  METHOD disp_output.
    DATA: lc_msg TYPE REF TO cx_salv_msg.

    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = o_alv
          CHANGING
            t_table      = t_mara.
      CATCH cx_salv_msg INTO lc_msg .
    ENDTRY.

    CALL METHOD set_pf_status
      CHANGING
        co_alv = o_alv.
    CALL METHOD set_top_of_page
      CHANGING
        co_alv = o_alv.
    CALL METHOD set_end_of_page
      CHANGING
        co_alv = o_alv.

    o_alv->display( ).

  ENDMETHOD.                    "disp_putput

  METHOD set_pf_status.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    lo_functions = co_alv->get_functions( ).
    lo_functions->set_default( abap_true ).
  ENDMETHOD.                    "set_pf_status

  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.
    CREATE OBJECT lo_header.

    lo_h_label = lo_header->create_label( row = 1 column = 1 ).
    lo_h_label->set_text('Header of the ALV Output in Bold').
    lo_h_flow = lo_header->create_flow( row = 2 column = 1 ).
    lo_h_flow->create_text( text = 'This is text of flow in Header' ).
    lo_h_flow = lo_header->create_flow( row = 3 column = 1 ).
    lo_h_flow->create_text( text = 'Date of List Generation' ).
    lo_h_flow = lo_header->create_flow( row = 3 column = 2 ).
    lo_h_flow->create_text( text = sy-datum ).
    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

  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.
    lo_f_label = lo_footer->create_label( row = 1 column = 1 ).
    lo_f_label->set_text('Footer of the ALV in Bold').
    lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ).
    lo_f_flow->create_text( text = 'This is text of flow 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_pageENDCLASS.
ENDCLASS.                    "lcl_head IMPLEMENTATION

START-OF-SELECTION.

  DATA: lo_report TYPE REF TO lcl_head.
  CREATE OBJECT lo_report.
  lo_report->disp_output( ).

  "lcl_head IMPLEMENTATION



1 comment: