SAP ABAP ALV OM EX01

From SapWiki
*&---------------------------------------------------------------------*
*& Report YALV
*&---------------------------------------------------------------------*
*& ejemplo alv con user command y double click
*&---------------------------------------------------------------------*
REPORT yalv.
TYPES: BEGIN OF ty_data,
         vbeln TYPE vbrk-vbeln,
         fkart TYPE vbrk-fkart,
         fkdat TYPE vbrk-fkdat,
         netwr TYPE vbrk-netwr,
         waerk TYPE vbrk-waerk,
       END OF ty_data.

DATA gt_data TYPE TABLE OF ty_data.
DATA wa_data LIKE LINE OF gt_data.

**********************************************************************
* ALV data
**********************************************************************
DATA: gr_table  TYPE REF TO cl_salv_table.
DATA: gr_selections TYPE REF TO cl_salv_selections,
      gt_rows       TYPE salv_t_row.
DATA: gr_functions TYPE REF TO cl_salv_functions_list.

*DATA : gt_color TYPE lvc_t_scol,
*       st_color TYPE lvc_s_scol.

*---------------------------------------------------------------------*
*       CLASS lcl_handle_events DEFINITION
*---------------------------------------------------------------------*
*   define a local class for handling events of cl_salv_table
*---------------------------------------------------------------------*
CLASS lcl_handle_events DEFINITION.
  PUBLIC SECTION.
    METHODS:
      on_user_command FOR EVENT added_function OF cl_salv_events
        IMPORTING e_salv_function,

      on_double_click FOR EVENT double_click OF cl_salv_events_table
        IMPORTING row column.

ENDCLASS.                    "lcl_handle_events DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_handle_events IMPLEMENTATION
*---------------------------------------------------------------------*
* §5.2 implement the events for handling the events of cl_salv_table
*---------------------------------------------------------------------*
CLASS lcl_handle_events IMPLEMENTATION.
  METHOD on_user_command.
    PERFORM user_command USING e_salv_function.
  ENDMETHOD.                    "on_user_command

  METHOD on_double_click.
    PERFORM double_click USING row column.
  ENDMETHOD.                    "on_double_click

ENDCLASS.                    "lcl_handle_events IMPLEMENTATION
* for handling the events of cl_salv_table
DATA: gr_events TYPE REF TO lcl_handle_events.
**********************************************************************
*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*
SELECT-OPTIONS s_vbeln FOR wa_data-vbeln.

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

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data FROM vbrk UP TO 100 ROWS
                                                     WHERE vbeln IN s_vbeln.

  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*      Displays the content based on the selection criteria.
*----------------------------------------------------------------------*
FORM user_command USING i_function TYPE salv_de_function.

  gr_selections = gr_table->get_selections( ).

* set selection mode
  gr_selections->set_selection_mode(
  if_salv_c_selection_mode=>row_column ).

  gt_rows = gr_selections->get_selected_rows( ).

  IF lines( gt_rows ) = 0.
    MESSAGE i000(0k) WITH 'Debe seleccionar al menos un registro'(i01).
    RETURN.
  ENDIF.

  CLEAR wa_data.
  LOOP AT gt_rows INTO DATA(l_tabix).

    READ TABLE gt_data INDEX l_tabix INTO wa_data.

    CASE i_function.
      WHEN 'PRINT'. "ICON_PRINT
        MESSAGE i000(0k) WITH 'N° factura:' wa_data-vbeln.
    ENDCASE.

  ENDLOOP.

  IF i_function <> 'LOG'.
    gr_table->refresh( refresh_mode = if_salv_c_refresh=>full ).
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  double_click
*&---------------------------------------------------------------------*
*       Diplays the popup for the selected row.
*----------------------------------------------------------------------*
FORM double_click USING i_row    TYPE i
                        i_column TYPE lvc_fname.

  READ TABLE gt_data INTO wa_data INDEX i_row.

  CASE i_column.
    WHEN 'VBELN'.
      SET PARAMETER ID 'VF' FIELD wa_data-vbeln.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
  ENDCASE.

ENDFORM.                    " double_click

FORM display_alv.
  DATA: lr_events TYPE REF TO cl_salv_events_table,
        lr_columns TYPE REF TO cl_salv_columns_table,
        lr_column  TYPE REF TO cl_salv_column,
        lr_grid TYPE REF TO cl_salv_form_layout_grid,
        lr_layout TYPE REF TO cl_salv_layout,
        ls_key    TYPE salv_s_layout_key,
        lr_sorts TYPE REF TO cl_salv_sorts,
        lr_sort  TYPE REF TO cl_salv_sort.

*... Create Instance
  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = gr_table
        CHANGING
          t_table      = gt_data.
    CATCH cx_salv_msg.
  ENDTRY.

  gr_table->set_screen_status(
    pfstatus      =  'STAT_ALV'  "copiar de SALV_DEMO_TABLE_LAYOUT    SALV_STANDARD
    report        =  sy-repid
    set_functions = gr_table->c_functions_all ).

* setting default ALV generic funtions
  gr_functions = gr_table->get_functions( ).

  gr_functions->set_all( ).

* set layout
  lr_layout = gr_table->get_layout( ).
* set the Layout Key
  ls_key-report = sy-repid.
  lr_layout->set_key( ls_key ).

*  LR_LAYOUT->SET_SAVE_RESTRICTION( 3 ).

  lr_layout->set_save_restriction( ).
  lr_layout->set_default( 'X' ).

* edit ALV columns
  lr_columns = gr_table->get_columns( ).
* optimize output
  lr_columns->set_optimize( 'X' ).

*----------------------------------------------------------------------*
*  Set column names
*----------------------------------------------------------------------*
  DEFINE m_column_names.
    TRY.
        lr_column = lr_columns->get_column( &1 ).
        lr_column->set_long_text( &2 ).
        lr_column->set_medium_text( &2 ).
        lr_column->set_short_text( &3 ).
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.
      CATCH cx_salv_data_error.
    ENDTRY.
  END-OF-DEFINITION.

  DEFINE m_no_display.
    TRY.
        lr_column = lr_columns->get_column( '&1' ).
        lr_column->set_visible( if_salv_c_bool_sap=>false ).
      CATCH cx_salv_not_found.
    ENDTRY.
  END-OF-DEFINITION.

  DEFINE m_set_moneda.
    TRY.
        lr_column = lr_columns->get_column( '&1' ).
        lr_column->set_currency_column( 'WAERK' ).
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.
      CATCH cx_salv_data_error.
    ENDTRY.
  END-OF-DEFINITION.

  m_set_moneda netwr.

* register to the events of cl_salv_table
  lr_events = gr_table->get_event( ).
  CREATE OBJECT gr_events.

* register to the event USER_COMMAND
  SET HANDLER gr_events->on_user_command FOR lr_events.

* register to the event DOUBLE_CLICK
  SET HANDLER gr_events->on_double_click FOR lr_events.

  gr_selections = gr_table->get_selections( ).

* set selection mode
  gr_selections->set_selection_mode(
  if_salv_c_selection_mode=>row_column ).
*----------------------------------------------------------------------*
*METHOD OF CREATING TOP-OF-LIST
*----------------------------------------------------------------------*
  PERFORM create_top_of_list CHANGING lr_grid.
  gr_table->set_top_of_list( lr_grid ).

*... Display table
  gr_table->display( ).

ENDFORM.                    "display_alv

FORM create_top_of_list CHANGING lr_grid TYPE REF TO cl_salv_form_layout_grid.
  DATA : lr_flow  TYPE REF TO cl_salv_form_layout_flow,
         lr_label TYPE REF TO cl_salv_form_label,
         lr_text  TYPE REF TO cl_salv_form_text,
         l_text   TYPE string.

  l_text = 'Reporte Facturas'.

  CREATE OBJECT lr_grid.
  CALL METHOD lr_grid->create_header_information
    EXPORTING
      row     = 1
      column  = 1
*     ROWSPAN =
*     COLSPAN =
      text    = l_text
      tooltip = sy-title.
*********************************************
  CALL METHOD lr_grid->create_flow
    EXPORTING
      row     = 2
      column  = 1
    RECEIVING
      r_value = lr_flow.

  CALL METHOD lr_flow->create_label
    EXPORTING
      text    = 'Fecha:'
      tooltip = 'Fecha:'
    RECEIVING
      r_value = lr_label.

  l_text = |{ sy-datum DATE = USER }|.

  CALL METHOD lr_flow->create_text
    EXPORTING
      text    = l_text
      tooltip = l_text
    RECEIVING
      r_value = lr_text.
************************************************
ENDFORM.                    "create_top_of_list