SAP ABAP ALV OM

From SapWiki
Revision as of 21:19, 2 June 2020 by WikiSysop (talk | contribs)

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.