Difference between revisions of "SAP ABAP ALV OM"

From SapWiki
(Created page with "==Función para desplegar un LOG usando ALV OM== * ----- Output in Log -------------------------------------------------- TYPES: BEGIN OF ty_news, icon(4),...")
 
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==ALV SIMPLE==
 +
<nowiki>DATA: gr_table  TYPE REF TO cl_salv_table.
 +
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
 +
*&---------------------------------------------------------------------*
 +
*&      Form  display_alv
 +
*&---------------------------------------------------------------------*
 +
*      text
 +
*----------------------------------------------------------------------*
 +
FORM display_alv .
 +
 +
  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
 +
        lr_column  TYPE REF TO cl_salv_column.
 +
  DATA: lr_layout TYPE REF TO cl_salv_layout,
 +
        ls_key    TYPE salv_s_layout_key.
 +
 +
*... 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.
 +
 +
* 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
 +
*----------------------------------------------------------------------*
 +
  TRY.
 +
      lr_column = lr_columns->get_column( 'DDTEXT' ).
 +
      lr_column->set_long_text( 'Desc. Estatus' ).
 +
      lr_column->set_medium_text( 'Desc. Estatus' ).
 +
      lr_column->set_short_text( 'Desc.Estat' ).
 +
    CATCH cx_salv_not_found.
 +
    CATCH cx_salv_existing.
 +
    CATCH cx_salv_data_error.
 +
  ENDTRY.
 +
 +
*--------------------------------------------------------------------*
 +
* hide columns
 +
*--------------------------------------------------------------------*
 +
  TRY.
 +
      lr_column = lr_columns->get_column( 'FECHA_FIRMA' ).
 +
      lr_column->set_technical( 'X' ).
 +
    CATCH cx_salv_not_found.
 +
    CATCH cx_salv_existing.
 +
    CATCH cx_salv_data_error.
 +
  ENDTRY.
 +
 +
*... Display table
 +
  gr_table->display( ).
 +
 +
ENDFORM.                    " DISPLAY_ALV</nowiki>
 +
 
==Función para desplegar un LOG usando ALV OM==
 
==Función para desplegar un LOG usando ALV OM==
  * ----- Output in Log --------------------------------------------------
+
  <nowiki>*----- Output in Log --------------------------------------------------
TYPES: BEGIN OF ty_news,
+
TYPES: BEGIN OF ty_news,
          icon(4),
+
        icon(4),
          pernr  TYPE zhr_solicitudes-pernr,
+
        pernr  TYPE zhr_solicitudes-pernr,
          begda  TYPE zhr_solicitudes-begda,
+
        begda  TYPE zhr_solicitudes-begda,
          endda  TYPE zhr_solicitudes-endda,
+
        endda  TYPE zhr_solicitudes-endda,
          subty  TYPE zhr_solicitudes-subty,
+
        subty  TYPE zhr_solicitudes-subty,
          text    LIKE bapiret2-message,
+
        text    LIKE bapiret2-message,
        END OF ty_news.
+
      END OF ty_news.
+
 
DATA: news TYPE TABLE OF ty_news.
+
DATA: news TYPE TABLE OF ty_news.
  ..
+
..
  ..
+
..
 +
CALL FUNCTION 'Z_DISPLAY_LOG'
 +
  EXPORTING
 +
    START_COLUMN      = 8
 +
    END_COLUMN        = 120
 +
    START_LINE        = 3
 +
    END_LINE          = 15
 +
  CHANGING
 +
    i_news = news.
 +
 
 +
FUNCTION Z_DISPLAY_LOG.
 +
*"----------------------------------------------------------------------
 +
*"*"Interfase local
 +
*"  IMPORTING
 +
*"    REFERENCE(START_COLUMN) TYPE  I DEFAULT 8
 +
*"    REFERENCE(END_COLUMN) TYPE  I DEFAULT 120
 +
*"    REFERENCE(START_LINE) TYPE  I DEFAULT 3
 +
*"    REFERENCE(END_LINE) TYPE  I DEFAULT 15
 +
*"  CHANGING
 +
*"    REFERENCE(I_NEWS) TYPE  TABLE
 +
*"----------------------------------------------------------------------
 +
* SALV Data
 +
  DATA: gr_table  TYPE REF TO cl_salv_table.
 +
  DATA: gr_functions TYPE REF TO cl_salv_functions_list.
 +
  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
 +
        lr_column  TYPE REF TO cl_salv_column.
 +
 
 +
  try.
 +
* SALV Create Instance
 +
  CALL METHOD cl_salv_table=>factory
 +
  IMPORTING
 +
    r_salv_table = gr_table
 +
  CHANGING
 +
    t_table      = i_news.          "internal table
 +
 
 +
    CATCH cx_salv_msg.      "#EC NO_HANDLER
 +
  ENDTRY.
 +
 
 +
* setting default ALV generic funtions
 +
  gr_functions = gr_table->get_functions( ).
 +
*  gr_functions->set_detail( IF_SALV_C_BOOL_SAP=>TRUE ).
 +
 
 +
  gr_functions->set_all( ).
 +
*... §4.1 set the size and position of the Popup via coordinates
 +
  gr_table->set_screen_popup(
 +
  start_column = start_column
 +
  end_column  = end_column
 +
  start_line  = start_line
 +
  end_line    = end_line ).
 +
 
 +
* edit ALV columns
 +
  lr_columns = gr_table->get_columns( ).
 +
* optimize output
 +
  lr_columns->set_optimize( 'X' ).
 +
 
 +
  TRY.
 +
    lr_column = lr_columns->get_column( 'ICON' ).
 +
    lr_column->set_long_text( 'Status' ).
 +
    lr_column->set_medium_text( 'Status' ).
 +
    lr_column->set_short_text( 'Status' ).
 +
  CATCH cx_salv_not_found.  "#EC NO_HANDLER
 +
  CATCH cx_salv_existing.    "#EC NO_HANDLER
 +
  CATCH cx_salv_data_error. "#EC NO_HANDLER
 +
  ENDTRY.
 +
 
 +
*... Display table
 +
  gr_table->display( ).
 +
 
 +
ENDFUNCTION.</nowiki>
 +
 
 +
== USO de Check Box para seleccionar columnas==
 +
  <nowiki>*---------------------------------------------------------------------*
 +
*      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.
 +
 
 +
*----------------------------------------------------------------------*
 +
*      CLASS mycheckbox DEFINITION
 +
*----------------------------------------------------------------------*
 +
*
 +
*----------------------------------------------------------------------*
 +
CLASS mycheckbox DEFINITION.
 +
  PUBLIC SECTION.
 +
    CLASS-METHODS on_click_checkbox FOR EVENT link_click OF cl_salv_events_table
 +
      IMPORTING row column.
 +
ENDCLASS.                    "mycheckbox DEFINITION
 +
 
 +
*----------------------------------------------------------------------*
 +
*      CLASS mycheckbox IMPLEMENTATION
 +
*----------------------------------------------------------------------*
 +
*
 +
*----------------------------------------------------------------------*
 +
CLASS mycheckbox IMPLEMENTATION.
 +
  METHOD on_click_checkbox.
 +
    READ TABLE gt_data_col INDEX row INTO wa_data.
 +
    IF wa_data-sel IS INITIAL ."AND wa_data-icon <> icon_green_light.
 +
      wa_data-sel = 'X'.
 +
    ELSE.
 +
      CLEAR wa_data-sel.
 +
    ENDIF.
 +
    MODIFY gt_data_col FROM wa_data INDEX sy-tabix.
 +
    gr_table->refresh( refresh_mode = if_salv_c_refresh=>full ).
 +
 
 +
  ENDMETHOD.                    "on_click_checkbox
 +
ENDCLASS.                    "mycheckbox IMPLEMENTATION
 +
 
 +
 
 +
form display_alv...
 +
  DATA: lr_events TYPE REF TO cl_salv_events_table.
 +
..
 +
..
 +
  DATA mycolumn TYPE REF TO cl_salv_column_table.                     
 +
  TRY.                                                               
 +
      mycolumn ?= lr_columns->get_column( 'SEL' ).                   
 +
      mycolumn->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
 +
    CATCH cx_salv_not_found.                                         
 +
    CATCH cx_salv_existing.                                         
 +
    CATCH cx_salv_data_error.                                       
 +
  ENDTRY.           
 +
 
 +
* register to the events of cl_salv_table
 +
  lr_events = gr_table->get_event( ).
 +
  CREATE OBJECT gr_events.
 +
 
 +
  SET HANDLER mycheckbox=>on_click_checkbox FOR lr_events.</nowiki>
 +
 
 +
== ALV OM en ABAP 7.4==
 +
<nowiki>FORM display_log.
 +
 
 +
  TRY.
 +
      cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table)
 +
                              CHANGING t_table = gt_log ).
 +
    CATCH cx_salv_msg.
 +
  ENDTRY.
 +
 
 +
*... Enable Generic ALV functions
 +
  DATA(gr_functions) = gr_table->get_functions( ).
 +
**  gr_functions->set_default( ).
 +
  gr_functions->set_all( ).
 +
 
 +
  DATA(lr_columns) = gr_table->get_columns( ).
 +
 
 +
* set column text
 +
  TRY.
 +
      DATA(lr_column) = lr_columns->get_column( 'EQUIPMENT_O' ).
 +
      lr_column->set_long_text( 'Equipo Original' ).
 +
      lr_column->set_medium_text( 'Equi.Ori.' ).
 +
      lr_column->set_short_text( 'Equi.Ori.' ).
 +
    CATCH cx_salv_not_found.
 +
    CATCH cx_salv_existing.
 +
    CATCH cx_salv_data_error.
 +
  ENDTRY.
 +
 
 +
*... Display table
 +
  gr_table->display( ).
 +
 
 +
ENDFORM.                    "display_log</nowiki>
 +
 
 +
==[[SAP_ABAP_ALV_OM_EX01|Ejemplo con double click & user command]]==
 +
 
 +
==Habilitar/Deshabilitar columna como llave==
 +
  <nowiki>DATA: gt_columns TYPE salv_t_column_ref,
 +
      lr_tabcol  TYPE REF TO cl_salv_column_table,
 +
      sg_columns TYPE salv_s_column_ref.
 +
 
 +
  TRY.
 +
    CALL METHOD lr_columns->get
 +
      RECEIVING
 +
        value = gt_columns.
 +
 
 +
    LOOP AT gt_columns INTO sg_columns.
 +
      CASE sg_columns-columnname.
 +
        WHEN 'ID_TRAN'.
 +
          TRY.
 +
            lr_tabcol ?= sg_columns-r_column.
 +
            lr_tabcol->set_key( if_salv_c_bool_sap=>false ).
 +
          ENDTRY.
 +
      ENDCASE.
 +
    ENDLOOP.
 +
  ENDTRY.</nowiki>
 +
 
 +
==Ejemplo Top of List==
 +
<nowiki>
 +
  data lr_grid TYPE REF TO cl_salv_form_layout_grid.
 +
 
 +
  PERFORM create_top_of_list CHANGING lr_grid.
 +
 
 +
  gr_table->set_top_of_list( lr_grid ).
 +
 
 +
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,
 +
        l_text  TYPE string.
 +
 
 +
  l_text = sy-title.
  
CALL FUNCTION 'Z_DISPLAY_LOG'
+
  CREATE OBJECT lr_grid.
 +
  CALL METHOD lr_grid->create_header_information
 
     EXPORTING
 
     EXPORTING
     START_COLUMN       = 8
+
      row    = 1
    END_COLUMN         = 120
+
      column  = 1
    START_LINE         = 3
+
*    ROWSPAN =
    END_LINE          = 15
+
*    COLSPAN =
   CHANGING
+
      text    = l_text
     i_news = news.
+
      tooltip = sy-title.
 +
*********************************************
 +
**... add a row to the grid -> row 2
 +
*  lr_grid->add_row( ).
 +
 
 +
*... in the cell [2,1] create a grid
 +
  DATA(lr_grid_1) = lr_grid->create_grid(
 +
                  row    = 2
 +
                  column = 1 ).
 +
 
 +
*... in the cell [1,1] of the second Grid create a label
 +
  DATA(lr_label) = lr_grid_1->create_label(
 +
    row    = 1
 +
    column  = 1
 +
    text    = 'Fecha:'(t02)
 +
    tooltip = 'Fecha:'(t02) ).
 +
 
 +
  l_text = |{ sy-datum DATE = USER }|.
 +
 
 +
*... in the cell [1,2] of the second Grid create a text
 +
  DATA(lr_text) = lr_grid_1->create_text(
 +
    row    = 1
 +
    column  = 2
 +
    text    = l_text
 +
    tooltip = l_text ).
 +
 
 +
  lr_label->set_label_for( lr_text ).
 +
 
 +
************************************************
 +
*... in the cell [2,1] of the second Grid create a label
 +
  l_text = TEXT-t01.
 +
  lr_label = lr_grid_1->create_label(
 +
    row    = 2
 +
    column  = 1
 +
    text    = l_text
 +
    tooltip = l_text ).
 +
 
 +
*... in the cell [2,2] of the second Grid create a text
 +
  l_text = p_file.
 +
  lr_text = lr_grid_1->create_text(
 +
  row    = 2
 +
  column  = 2
 +
  text    = l_text
 +
  tooltip = l_text ).
 +
 
 +
  lr_label->set_label_for( lr_text ).
 +
************************************************
 +
ENDFORM.                    "create_top_of_list</nowiki>
 +
===Agregar logo===
 +
<nowiki>
 +
*--------------------------------------------------------------------*
 +
*      Create logo layout, set grid content on left and logo image on right
 +
*--------------------------------------------------------------------*
 +
  DATA  lr_logo  TYPE REF TO cl_salv_form_layout_logo.
 +
 
 +
  CREATE OBJECT lr_logo.
 +
  lr_logo->set_left_content( lr_grid ).
 +
*     *****
 +
  lr_logo->set_right_logo( 'ZLOGO' ). "'ENJOYSAP_LOGO'
 +
 
 +
*       Set element top_of_list
 +
  gr_table->set_top_of_list( lr_logo ). </nowiki>
 +
 
 +
==MACROS==
 +
  <nowiki>DATA: l_text_l TYPE scrtext_l,
 +
        l_text_m TYPE scrtext_m,
 +
        l_text_s TYPE scrtext_s.
 +
 
 +
*----------------------------------------------------------------------*
 +
*  Set column names
 +
*----------------------------------------------------------------------*
 +
  DEFINE m_column_names.
 +
    l_text_l = &4.
 +
    l_text_m = &3.
 +
    l_text_s = &2.
 +
    TRY.
 +
        lr_column = lr_columns->get_column( &1 ).
 +
        lr_column->set_long_text( l_text_l ).
 +
        lr_column->set_medium_text( l_text_m ).
 +
        lr_column->set_short_text( l_text_s ).
 +
      CATCH cx_salv_not_found.
 +
      CATCH cx_salv_existing.
 +
      CATCH cx_salv_data_error.
 +
    ENDTRY.
 +
  END-OF-DEFINITION.
 +
 
 +
  m_column_names 'DETALLE' 'Detalle'(t03) 'Detalle'(t03) 'Detalle'(t03).
 +
 
 +
  DEFINE m_set_technical.
 +
    TRY.
 +
         lr_column = lr_columns->get_column( &1 ).
 +
         lr_column->set_technical( if_salv_c_bool_sap=>true ).
 +
      CATCH cx_salv_not_found.
 +
    ENDTRY.
 +
  END-OF-DEFINITION.</nowiki>
 +
==Definir campo de moneda a campos tipo moneda (CURR)==
 +
<nowiki>
 +
TYPES BEGIN OF ty_data,
 +
  periodo type MAHD_ALV_PERIO,
 +
  bukrs type p0001-bukrs,
 +
  werks type p0001-werks,
 +
  pernr type p0001-pernr,
 +
  cname type p0002-cname,
 +
  tot_hab              type PAD_AMT7S, "CURR
 +
  tot_hab_imp_tri      type PAD_AMT7S, "CURR
 +
  tot_hab_imp_no_tri   type PAD_AMT7S, "CURR
 +
  tot_hab_no_imp_no_tri type PAD_AMT7S, "CURR
 +
  tot_hab_no_imp_tri    type PAD_AMT7S, "CURR
 +
 
 +
  waers type waers,
 +
END OF ty_data.
 +
 
 +
  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
 +
        lr_column  TYPE REF TO cl_salv_column.
 +
  data gt_data type table of ty_data.
 +
  data gs_data type ty_data.
 +
...
 +
...
 +
...
 +
  TRY.
 +
      cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table)
 +
                              CHANGING t_table = gt_data ).
 +
    CATCH cx_salv_msg.
 +
  ENDTRY.
 +
  lr_columns = gr_table->get_columns( ).
 +
  perform set_moneda.
 +
...
 +
...
 +
FORM set_moneda.
 +
  DATA lr_structdesc TYPE REF TO cl_abap_structdescr.
 +
  DATA: lt_campo TYPE cl_abap_structdescr=>component_table,
 +
        ls_campo TYPE abap_componentdescr.
 +
  DATA: lr_descr     TYPE REF TO cl_abap_elemdescr,
 +
        lt_ddic_info  TYPE dd_x031l_table,
 +
        lrs_ddic_info TYPE REF TO x031l.
 +
  DATA l_cname TYPE lvc_fname.
 +
  FIELD-SYMBOLS <fs_field> TYPE ANY.
  
FUNCTION Z_DISPLAY_LOG.
+
  lr_structdesc ?= cl_abap_typedescr=>describe_by_data( gs_data ).
*"----------------------------------------------------------------------
+
 
*"*"Interfase local
+
  lt_campo = lr_structdesc->get_components( ).
*"  IMPORTING
+
 
*"    REFERENCE(START_COLUMN) TYPE  I DEFAULT 8
+
  DO.
*"    REFERENCE(END_COLUMN) TYPE  I DEFAULT 120
+
     READ TABLE lt_campo INTO ls_campo INDEX sy-index.
*"    REFERENCE(START_LINE) TYPE  I DEFAULT 3
+
    IF sy-subrc = 0.
*"    REFERENCE(END_LINE) TYPE  I DEFAULT 15
+
      ASSIGN COMPONENT ls_campo-name OF STRUCTURE gs_data TO <fs_field>.
*"  CHANGING
+
      CASE ls_campo-type->type_kind. .
*"     REFERENCE(I_NEWS) TYPE  TABLE
+
        WHEN 'P'.
*"----------------------------------------------------------------------
+
          lr_descr ?= cl_abap_elemdescr=>describe_by_data( <fs_field> ).
* SALV Data
+
          lt_ddic_info = lr_descr->get_ddic_object( ).
  DATA: gr_table  TYPE REF TO cl_salv_table.
+
          READ TABLE lt_ddic_info INDEX 1 REFERENCE INTO lrs_ddic_info.
  DATA: gr_functions TYPE REF TO cl_salv_functions_list.
+
          IF sy-subrc = 0.
  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
+
            IF lrs_ddic_info->dtyp = 'CURR'.
        lr_column  TYPE REF TO cl_salv_column.
+
              l_cname = ls_campo-name.
+
              TRY.
  try.
+
                  lr_column = lr_columns->get_column( l_cname ).
* SALV Create Instance
+
                  lr_column->set_currency_column( 'WAERS' ).
  CALL METHOD cl_salv_table=>factory
+
                CATCH cx_salv_not_found.
  IMPORTING
+
                CATCH cx_salv_existing.
    r_salv_table = gr_table
+
                CATCH cx_salv_data_error.
  CHANGING
+
              ENDTRY.
    t_table      = i_news.           "internal table
+
            ENDIF.
+
          ENDIF.
    CATCH cx_salv_msg.     "#EC NO_HANDLER
+
      ENDCASE.
  ENDTRY.
+
    ELSE.
+
      EXIT.
* setting default ALV generic funtions
+
    ENDIF.
  gr_functions = gr_table->get_functions( ).
+
  ENDDO.
*  gr_functions->set_detail( IF_SALV_C_BOOL_SAP=>TRUE ).
+
 
+
ENDFORM.                    "set_moneda
  gr_functions->set_all( ).
+
</nowiki>
*... §4.1 set the size and position of the Popup via coordinates
 
  gr_table->set_screen_popup(
 
  start_column = start_column
 
  end_column  = end_column
 
  start_line  = start_line
 
  end_line    = end_line ).
 
 
* edit ALV columns
 
  lr_columns = gr_table->get_columns( ).
 
* optimize output
 
  lr_columns->set_optimize( 'X' ).
 
 
  TRY.
 
    lr_column = lr_columns->get_column( 'ICON' ).
 
    lr_column->set_long_text( 'Status' ).
 
    lr_column->set_medium_text( 'Status' ).
 
    lr_column->set_short_text( 'Status' ).
 
  CATCH cx_salv_not_found.   "#EC NO_HANDLER
 
  CATCH cx_salv_existing.   "#EC NO_HANDLER
 
  CATCH cx_salv_data_error. "#EC NO_HANDLER
 
  ENDTRY.
 
 
*... Display table
 
  gr_table->display( ).
 
 
 
 
 
ENDFUNCTION.
 

Latest revision as of 20:03, 1 December 2021

ALV SIMPLE

DATA: gr_table  TYPE REF TO cl_salv_table.
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_alv .

  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
        lr_column  TYPE REF TO cl_salv_column.
  DATA: lr_layout TYPE REF TO cl_salv_layout,
        ls_key    TYPE salv_s_layout_key.
 
*... 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.
 
* 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
*----------------------------------------------------------------------*
  TRY.
      lr_column = lr_columns->get_column( 'DDTEXT' ).
      lr_column->set_long_text( 'Desc. Estatus' ).
      lr_column->set_medium_text( 'Desc. Estatus' ).
      lr_column->set_short_text( 'Desc.Estat' ).
    CATCH cx_salv_not_found.
    CATCH cx_salv_existing.
    CATCH cx_salv_data_error.
  ENDTRY.

*--------------------------------------------------------------------*
* hide columns
*--------------------------------------------------------------------*
  TRY.
      lr_column = lr_columns->get_column( 'FECHA_FIRMA' ).
      lr_column->set_technical( 'X' ).
    CATCH cx_salv_not_found.
    CATCH cx_salv_existing.
    CATCH cx_salv_data_error.
  ENDTRY.

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

ENDFORM.                    " DISPLAY_ALV

Función para desplegar un LOG usando ALV OM

*----- Output in Log --------------------------------------------------
TYPES: BEGIN OF ty_news,
         icon(4),
         pernr   TYPE zhr_solicitudes-pernr,
         begda   TYPE zhr_solicitudes-begda,
         endda   TYPE zhr_solicitudes-endda,
         subty   TYPE zhr_solicitudes-subty,
         text    LIKE bapiret2-message,
       END OF ty_news.

DATA: news TYPE TABLE OF ty_news.
..
..
CALL FUNCTION 'Z_DISPLAY_LOG'
   EXPORTING
    START_COLUMN       = 8
    END_COLUMN         = 120
    START_LINE         = 3
    END_LINE           = 15
  CHANGING
    i_news = news.

FUNCTION Z_DISPLAY_LOG.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(START_COLUMN) TYPE  I DEFAULT 8
*"     REFERENCE(END_COLUMN) TYPE  I DEFAULT 120
*"     REFERENCE(START_LINE) TYPE  I DEFAULT 3
*"     REFERENCE(END_LINE) TYPE  I DEFAULT 15
*"  CHANGING
*"     REFERENCE(I_NEWS) TYPE  TABLE
*"----------------------------------------------------------------------
* SALV Data
  DATA: gr_table  TYPE REF TO cl_salv_table.
  DATA: gr_functions TYPE REF TO cl_salv_functions_list.
  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
        lr_column  TYPE REF TO cl_salv_column.

  try.
* SALV Create Instance
  CALL METHOD cl_salv_table=>factory
  IMPORTING
    r_salv_table = gr_table
  CHANGING
    t_table      = i_news.           "internal table

    CATCH cx_salv_msg.      "#EC NO_HANDLER
  ENDTRY.

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

  gr_functions->set_all( ).
*... §4.1 set the size and position of the Popup via coordinates
  gr_table->set_screen_popup(
  start_column = start_column
  end_column   = end_column
  start_line   = start_line
  end_line     = end_line ).

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

  TRY.
    lr_column = lr_columns->get_column( 'ICON' ).
    lr_column->set_long_text( 'Status' ).
    lr_column->set_medium_text( 'Status' ).
    lr_column->set_short_text( 'Status' ).
  CATCH cx_salv_not_found.   "#EC NO_HANDLER
  CATCH cx_salv_existing.    "#EC NO_HANDLER
  CATCH cx_salv_data_error.  "#EC NO_HANDLER
  ENDTRY.

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

ENDFUNCTION.

USO de Check Box para seleccionar columnas

*---------------------------------------------------------------------*
*       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.

*----------------------------------------------------------------------*
*       CLASS mycheckbox DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mycheckbox DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS on_click_checkbox FOR EVENT link_click OF cl_salv_events_table
      IMPORTING row column.
ENDCLASS.                    "mycheckbox DEFINITION

*----------------------------------------------------------------------*
*       CLASS mycheckbox IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mycheckbox IMPLEMENTATION.
  METHOD on_click_checkbox.
    READ TABLE gt_data_col INDEX row INTO wa_data.
    IF wa_data-sel IS INITIAL ."AND wa_data-icon <> icon_green_light.
      wa_data-sel = 'X'.
    ELSE.
      CLEAR wa_data-sel.
    ENDIF.
    MODIFY gt_data_col FROM wa_data INDEX sy-tabix.
    gr_table->refresh( refresh_mode = if_salv_c_refresh=>full ).

  ENDMETHOD.                    "on_click_checkbox
ENDCLASS.                    "mycheckbox IMPLEMENTATION


form display_alv...
  DATA: lr_events TYPE REF TO cl_salv_events_table.
..
..
  DATA mycolumn TYPE REF TO cl_salv_column_table.                      
  TRY.                                                                 
      mycolumn ?= lr_columns->get_column( 'SEL' ).                     
      mycolumn->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
    CATCH cx_salv_not_found.                                           
     CATCH cx_salv_existing.                                           
     CATCH cx_salv_data_error.                                         
  ENDTRY.            
  
* register to the events of cl_salv_table
  lr_events = gr_table->get_event( ).
  CREATE OBJECT gr_events.
  
  SET HANDLER mycheckbox=>on_click_checkbox FOR lr_events.

ALV OM en ABAP 7.4

FORM display_log.

  TRY.
      cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table)
                               CHANGING t_table = gt_log ).
    CATCH cx_salv_msg.
  ENDTRY.

*... Enable Generic ALV functions
  DATA(gr_functions) = gr_table->get_functions( ).
**  gr_functions->set_default( ).
  gr_functions->set_all( ).

  DATA(lr_columns) = gr_table->get_columns( ).

* set column text
  TRY.
      DATA(lr_column) = lr_columns->get_column( 'EQUIPMENT_O' ).
      lr_column->set_long_text( 'Equipo Original' ).
      lr_column->set_medium_text( 'Equi.Ori.' ).
      lr_column->set_short_text( 'Equi.Ori.' ).
    CATCH cx_salv_not_found.
    CATCH cx_salv_existing.
    CATCH cx_salv_data_error.
  ENDTRY.

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

ENDFORM.                    "display_log

Ejemplo con double click & user command

Habilitar/Deshabilitar columna como llave

 DATA: gt_columns TYPE salv_t_column_ref,
       lr_tabcol  TYPE REF TO cl_salv_column_table,
       sg_columns TYPE salv_s_column_ref.

  TRY.
    CALL METHOD lr_columns->get
      RECEIVING
        value = gt_columns.

    LOOP AT gt_columns INTO sg_columns.
      CASE sg_columns-columnname.
        WHEN 'ID_TRAN'.
          TRY.
            lr_tabcol ?= sg_columns-r_column.
            lr_tabcol->set_key( if_salv_c_bool_sap=>false ).
          ENDTRY.
      ENDCASE.
    ENDLOOP.
  ENDTRY.

Ejemplo Top of List

  data lr_grid TYPE REF TO cl_salv_form_layout_grid.

  PERFORM create_top_of_list CHANGING lr_grid.

  gr_table->set_top_of_list( lr_grid ).

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,
         l_text  TYPE string.

  l_text = sy-title.

  CREATE OBJECT lr_grid.
  CALL METHOD lr_grid->create_header_information
    EXPORTING
      row     = 1
      column  = 1
*     ROWSPAN =
*     COLSPAN =
      text    = l_text
      tooltip = sy-title.
*********************************************
**... add a row to the grid -> row 2
*  lr_grid->add_row( ).

*... in the cell [2,1] create a grid
  DATA(lr_grid_1) = lr_grid->create_grid(
                  row    = 2
                  column = 1 ).

*... in the cell [1,1] of the second Grid create a label
  DATA(lr_label) = lr_grid_1->create_label(
    row     = 1
    column  = 1
    text    = 'Fecha:'(t02)
    tooltip = 'Fecha:'(t02) ).

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

*... in the cell [1,2] of the second Grid create a text
  DATA(lr_text) = lr_grid_1->create_text(
    row     = 1
    column  = 2
    text    = l_text
    tooltip = l_text ).

  lr_label->set_label_for( lr_text ).

************************************************
*... in the cell [2,1] of the second Grid create a label
  l_text = TEXT-t01.
  lr_label = lr_grid_1->create_label(
    row     = 2
    column  = 1
    text    = l_text
    tooltip = l_text ).

*... in the cell [2,2] of the second Grid create a text
  l_text = p_file.
  lr_text = lr_grid_1->create_text(
   row     = 2
   column  = 2
   text    = l_text
   tooltip = l_text ).

  lr_label->set_label_for( lr_text ).
************************************************
ENDFORM.                    "create_top_of_list

*--------------------------------------------------------------------*
*       Create logo layout, set grid content on left and logo image on right
*--------------------------------------------------------------------*
  DATA  lr_logo  TYPE REF TO cl_salv_form_layout_logo.

  CREATE OBJECT lr_logo.
  lr_logo->set_left_content( lr_grid ).
*      *****
  lr_logo->set_right_logo( 'ZLOGO' ). "'ENJOYSAP_LOGO'

*       Set element top_of_list
  gr_table->set_top_of_list( lr_logo ). 

MACROS

  DATA: l_text_l TYPE scrtext_l,
        l_text_m TYPE scrtext_m,
        l_text_s TYPE scrtext_s.

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

  m_column_names 'DETALLE' 'Detalle'(t03) 'Detalle'(t03) 'Detalle'(t03).

  DEFINE m_set_technical.
    TRY.
        lr_column = lr_columns->get_column( &1 ).
        lr_column->set_technical( if_salv_c_bool_sap=>true ).
      CATCH cx_salv_not_found.
    ENDTRY.
  END-OF-DEFINITION.

Definir campo de moneda a campos tipo moneda (CURR)

TYPES BEGIN OF ty_data,
  periodo type MAHD_ALV_PERIO,
  bukrs type p0001-bukrs,
  werks type p0001-werks,
  pernr type p0001-pernr,
  cname type p0002-cname,
  tot_hab               type PAD_AMT7S, "CURR
  tot_hab_imp_tri       type PAD_AMT7S, "CURR
  tot_hab_imp_no_tri    type PAD_AMT7S, "CURR
  tot_hab_no_imp_no_tri type PAD_AMT7S, "CURR
  tot_hab_no_imp_tri    type PAD_AMT7S, "CURR

  waers type waers,
END OF ty_data.

  DATA: lr_columns TYPE REF TO cl_salv_columns_table,
        lr_column  TYPE REF TO cl_salv_column.
  data gt_data type table of ty_data.
  data gs_data type ty_data.
...
...
...
  TRY.
      cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table)
                               CHANGING t_table = gt_data ).
    CATCH cx_salv_msg.
  ENDTRY.
  lr_columns = gr_table->get_columns( ).
  perform set_moneda.
...
...
FORM set_moneda.
  DATA lr_structdesc TYPE REF TO cl_abap_structdescr.
  DATA: lt_campo TYPE cl_abap_structdescr=>component_table,
        ls_campo TYPE abap_componentdescr.
  DATA: lr_descr      TYPE REF TO cl_abap_elemdescr,
        lt_ddic_info  TYPE dd_x031l_table,
        lrs_ddic_info TYPE REF TO x031l.
  DATA l_cname TYPE lvc_fname.
  FIELD-SYMBOLS <fs_field> TYPE ANY.

  lr_structdesc ?= cl_abap_typedescr=>describe_by_data( gs_data ).

  lt_campo = lr_structdesc->get_components( ).

  DO.
    READ TABLE lt_campo INTO ls_campo INDEX sy-index.
    IF sy-subrc = 0.
      ASSIGN COMPONENT ls_campo-name OF STRUCTURE gs_data TO <fs_field>.
      CASE ls_campo-type->type_kind. .
        WHEN 'P'.
          lr_descr ?= cl_abap_elemdescr=>describe_by_data( <fs_field> ).
          lt_ddic_info = lr_descr->get_ddic_object( ).
          READ TABLE lt_ddic_info INDEX 1 REFERENCE INTO lrs_ddic_info.
          IF sy-subrc = 0.
            IF lrs_ddic_info->dtyp = 'CURR'.
              l_cname = ls_campo-name.
              TRY.
                  lr_column = lr_columns->get_column( l_cname ).
                  lr_column->set_currency_column( 'WAERS' ).
                CATCH cx_salv_not_found.
                CATCH cx_salv_existing.
                CATCH cx_salv_data_error.
              ENDTRY.
            ENDIF.
          ENDIF.
      ENDCASE.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.

ENDFORM.                    "set_moneda