Difference between revisions of "SAP ABAP ALV CL GUI ALV GRID"

From SapWiki
Line 203: Line 203:
 
===Y_TEST_ALV_GRID_F01===
 
===Y_TEST_ALV_GRID_F01===
 
  <nowiki> *----------------------------------------------------------------------*
 
  <nowiki> *----------------------------------------------------------------------*
***Y_TEST_ALV_GRID_F01
+
***Y_TEST_ALV_GRID_F01
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
FORM fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
+
FORM fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
  DATA lt_fcat_save TYPE lvc_t_fcat.
+
  DATA lt_fcat_save TYPE lvc_t_fcat.
 
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
 
    EXPORTING
 
      i_structure_name      = 'PA0002'
 
      i_client_never_display = 'X'
 
    CHANGING
 
      ct_fieldcat            = lt_fcat_save
 
    EXCEPTIONS
 
      inconsistent_interface = 1
 
      program_error          = 2
 
      OTHERS                = 3.
 
  IF sy-subrc <> 0.
 
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
 
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 
  ENDIF.
 
 
* colums names
 
*  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>).
 
*    CASE <wa>-fieldname.
 
*      WHEN 'ICON'.
 
*        <wa>-scrtext_l = 'Estatus'.
 
*        <wa>-scrtext_m = 'Estatus'.
 
*        <wa>-scrtext_s = <wa>-reptext = 'Estatus'.
 
*  endloop.
 
 
* hide, solo se desplegan columnas PERNR, VORNA, NACHN & NAME2
 
  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>) WHERE fieldname <> 'PERNR'
 
                                        AND fieldname <> 'VORNA'
 
                                        AND fieldname <> 'NACHN'
 
                                        AND fieldname <> 'NAME2'.
 
    <wa>-tech = 'X'.
 
  ENDLOOP.
 
  
* enable VORNA para editar
+
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  READ TABLE lt_fcat_save ASSIGNING <wa> WITH KEY fieldname = 'VORNA'.
+
    EXPORTING
  <wa>-edit = 'X'.
+
      i_structure_name      = 'PA0002'
+
      i_client_never_display = 'X'
  pt_fieldcat[] = lt_fcat_save[].
+
    CHANGING
+
      ct_fieldcat            = lt_fcat_save
ENDFORM.
+
    EXCEPTIONS
*----------------------------------------------------------------------*
+
      inconsistent_interface = 1
*      FORM style
+
      program_error          = 2
*----------------------------------------------------------------------*
+
      OTHERS                = 3.
* Habilita/Des-Habilita edición de columna VORNA
+
  IF sy-subrc <> 0.
*----------------------------------------------------------------------*
+
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*  -->  p1        text
+
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*  <-- p2        text
+
  ENDIF.
*----------------------------------------------------------------------*
+
 
FORM style CHANGING p_edit.
+
* colums names
  DATA: ls_celltab TYPE lvc_s_styl.
+
*  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>).
  DATA: lt_celltab TYPE lvc_t_styl.
+
*    CASE <wa>-fieldname.
  DATA l_tabix TYPE sy-tabix.
+
*      WHEN 'ICON'.
+
*        <wa>-scrtext_l = 'Estatus'.
  LOOP AT gt_data INTO wa_data.
+
*        <wa>-scrtext_m = 'Estatus'.
    l_tabix = sy-tabix.
+
*        <wa>-scrtext_s = <wa>-reptext = 'Estatus'.
    CLEAR lt_celltab[].
+
*  endloop.
    CLEAR wa_data-celltab[].
+
 
+
* hide, solo se desplegan columnas PERNR, VORNA, NACHN & NAME2
    IF p_edit = 'X'.
+
  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>) WHERE fieldname <> 'PERNR'
    ELSE.
+
                                        AND fieldname <> 'VORNA'
      ls_celltab-fieldname = 'VORNA'.
+
                                        AND fieldname <> 'NACHN'
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
+
                                        AND fieldname <> 'NAME2'.
      INSERT ls_celltab INTO TABLE lt_celltab.
+
    <wa>-tech = 'X'.
    ENDIF.
+
  ENDLOOP.
+
 
    INSERT LINES OF lt_celltab INTO TABLE wa_data-celltab.
+
* enable VORNA para editar
    MODIFY gt_data FROM wa_data INDEX l_tabix.
+
  READ TABLE lt_fcat_save ASSIGNING <wa> WITH KEY fieldname = 'VORNA'.
  ENDLOOP.
+
  <wa>-edit = 'X'.
+
 
  IF p_edit = 'X'.
+
  pt_fieldcat[] = lt_fcat_save[].
    p_edit = space.
+
 
  ELSE.
+
ENDFORM.
    p_edit = 'X'.
+
*----------------------------------------------------------------------*
  ENDIF.
+
*      FORM style
+
*----------------------------------------------------------------------*
ENDFORM.
+
* Habilita/Des-Habilita edición de columna VORNA
+
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
+
*  -->  p1        text
*&      Form  CREATE_AND_INIT_ALV
+
*  <--  p2        text
*&---------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*      text
+
FORM style CHANGING p_edit.
*----------------------------------------------------------------------*
+
  DATA: ls_celltab TYPE lvc_s_styl.
*      <--P_GT_DATA[]  text
+
  DATA: lt_celltab TYPE lvc_t_styl.
*      <--P_GT_FIELDCAT  text
+
  DATA l_tabix TYPE sy-tabix.
*----------------------------------------------------------------------*
+
 
+
  LOOP AT gt_data INTO wa_data.
FORM create_and_init_alv CHANGING pt_outtab TYPE STANDARD TABLE
+
    l_tabix = sy-tabix.
                                  pt_fieldcat TYPE lvc_t_fcat.
+
    CLEAR lt_celltab[].
+
    CLEAR wa_data-celltab[].
  DATA: lt_exclude TYPE ui_functions.
+
 
  DATA: ps_layout TYPE lvc_s_layo.
+
    IF p_edit = 'X'.
+
    ELSE.
  CREATE OBJECT g_custom_container
+
      ls_celltab-fieldname = 'VORNA'.
    EXPORTING
+
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      container_name = g_container.
+
      INSERT ls_celltab INTO TABLE lt_celltab.
  CREATE OBJECT g_grid
+
    ENDIF.
    EXPORTING
+
 
      i_parent = g_custom_container.
+
    INSERT LINES OF lt_celltab INTO TABLE wa_data-celltab.
+
    MODIFY gt_data FROM wa_data INDEX l_tabix.
  PERFORM fieldcat CHANGING pt_fieldcat.
+
  ENDLOOP.
+
 
  PERFORM exclude_tb_functions CHANGING lt_exclude.
+
  IF p_edit = 'X'.
+
    p_edit = space.
  ps_layout-zebra = 'X'.
+
  ELSE.
  ps_layout-cwidth_opt = 'X'.
+
    p_edit = 'X'.
  ps_layout-stylefname = 'CELLTAB'.
+
  ENDIF.
+
 
  g_variant1-report = sy-repid.
+
ENDFORM.
  g_variant1-handle = 'X'.
+
 
  g_variant1-username = sy-uname.
+
*&---------------------------------------------------------------------*
+
*&      Form  CREATE_AND_INIT_ALV
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
+
*&---------------------------------------------------------------------*
    EXPORTING
+
*      text
      i_save        = 'A'
+
*----------------------------------------------------------------------*
    CHANGING
+
*      <--P_GT_DATA[]  text
      cs_variant    = g_variant1
+
*      <--P_GT_FIELDCAT  text
    EXCEPTIONS
+
*----------------------------------------------------------------------*
      wrong_input  = 1
+
 
      not_found    = 2
+
FORM create_and_init_alv CHANGING pt_outtab TYPE STANDARD TABLE
      program_error = 3
+
                                  pt_fieldcat TYPE lvc_t_fcat.
      OTHERS        = 4.
+
 
+
  DATA: lt_exclude TYPE ui_functions.
  CALL METHOD g_grid->set_table_for_first_display
+
  DATA: ps_layout TYPE lvc_s_layo.
    EXPORTING
+
 
      i_save              = 'A'
+
  CREATE OBJECT g_custom_container
      is_variant          = g_variant1
+
    EXPORTING
      is_layout            = ps_layout
+
      container_name = g_container.
      it_toolbar_excluding = lt_exclude
+
  CREATE OBJECT g_grid
    CHANGING
+
    EXPORTING
      it_fieldcatalog      = pt_fieldcat
+
      i_parent = g_custom_container.
      it_outtab            = pt_outtab[].
+
 
+
  PERFORM fieldcat CHANGING pt_fieldcat.
  CALL METHOD g_grid->set_ready_for_input
+
 
    EXPORTING
+
  PERFORM exclude_tb_functions CHANGING lt_exclude.
      i_ready_for_input = 1.
+
 
+
  ps_layout-zebra = 'X'.
  CALL METHOD g_grid->register_edit_event
+
  ps_layout-cwidth_opt = 'X'.
    EXPORTING
+
  ps_layout-stylefname = 'CELLTAB'.
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.
+
 
+
  g_variant1-report = sy-repid.
*  CALL METHOD g_grid->save_variant.
+
  g_variant1-handle = 'X'.
+
  g_variant1-username = sy-uname.
  CREATE OBJECT g_event_receiver.
+
 
  SET HANDLER g_event_receiver->handle_user_command FOR g_grid.
+
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
  SET HANDLER g_event_receiver->handle_toolbar FOR g_grid.
+
    EXPORTING
  SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
+
      i_save        = 'A'
  CALL METHOD g_grid->set_toolbar_interactive.
+
    CHANGING
+
      cs_variant    = g_variant1
+
    EXCEPTIONS
ENDFORM.                    " CREATE_AND_INIT_ALV
+
      wrong_input  = 1
+
      not_found    = 2
*&---------------------------------------------------------------------*
+
      program_error = 3
*&      Form  EXCLUDE_TB_FUNCTIONS
+
      OTHERS        = 4.
*&---------------------------------------------------------------------*
+
 
*      text
+
  CALL METHOD g_grid->set_table_for_first_display
*----------------------------------------------------------------------*
+
    EXPORTING
*      -->PT_EXCLUDE text
+
      i_save              = 'A'
*----------------------------------------------------------------------*
+
      is_variant          = g_variant1
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
+
      is_layout            = ps_layout
  DATA ls_exclude TYPE ui_func.
+
      it_toolbar_excluding = lt_exclude
+
    CHANGING
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
+
      it_fieldcatalog      = pt_fieldcat
  APPEND ls_exclude TO pt_exclude.
+
      it_outtab            = pt_outtab[].
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
+
 
  APPEND ls_exclude TO pt_exclude.
+
  CALL METHOD g_grid->set_ready_for_input
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
+
    EXPORTING
  APPEND ls_exclude TO pt_exclude.
+
      i_ready_for_input = 1.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
+
 
  APPEND ls_exclude TO pt_exclude.
+
  CALL METHOD g_grid->register_edit_event
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
+
    EXPORTING
  APPEND ls_exclude TO pt_exclude.
+
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
+
 
  APPEND ls_exclude TO pt_exclude.
+
*  CALL METHOD g_grid->save_variant.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
+
 
  APPEND ls_exclude TO pt_exclude.
+
  CREATE OBJECT g_event_receiver.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
+
  SET HANDLER g_event_receiver->handle_user_command FOR g_grid.
  APPEND ls_exclude TO pt_exclude.
+
  SET HANDLER g_event_receiver->handle_toolbar FOR g_grid.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
+
  SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
  APPEND ls_exclude TO pt_exclude.
+
  CALL METHOD g_grid->set_toolbar_interactive.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
+
 
  APPEND ls_exclude TO pt_exclude.
+
 
+
ENDFORM.                    " CREATE_AND_INIT_ALV
ENDFORM.                    " EXCLUDE_TB_FUNCTIONS
+
 
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  GRABAR
+
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*  -->  p1        text
+
*      -->PT_EXCLUDE text
*  <--  p2        text
+
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
FORM grabar .
+
  DATA ls_exclude TYPE ui_func.
  CALL METHOD g_grid->refresh_table_display.
+
 
ENDFORM.                    " GRABAR
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
+
  APPEND ls_exclude TO pt_exclude.
*&---------------------------------------------------------------------*
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
*&      Form  user_command
+
  APPEND ls_exclude TO pt_exclude.
*&---------------------------------------------------------------------*
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
*      user command de CONTAINER
+
  APPEND ls_exclude TO pt_exclude.
*----------------------------------------------------------------------*
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
*      -->I_FUNCTION text
+
  APPEND ls_exclude TO pt_exclude.
*----------------------------------------------------------------------*
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
FORM user_command USING i_function TYPE salv_de_function.
+
  APPEND ls_exclude TO pt_exclude.
  DATA: gt_rows TYPE lvc_t_row.
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
+
  APPEND ls_exclude TO pt_exclude.
  CALL METHOD g_grid->get_selected_rows
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
    IMPORTING
+
  APPEND ls_exclude TO pt_exclude.
      et_index_rows = gt_rows.
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
+
  APPEND ls_exclude TO pt_exclude.
  CASE i_function.
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
    WHEN 'APRO'. "do something
+
  APPEND ls_exclude TO pt_exclude.
      LOOP AT gt_rows INTO DATA(l_tabix).
+
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
        READ TABLE gt_data INDEX l_tabix INTO wa_data.
+
  APPEND ls_exclude TO pt_exclude.
      ENDLOOP.
+
 
  ENDCASE.
+
ENDFORM.                    " EXCLUDE_TB_FUNCTIONS
ENDFORM.</nowiki>
+
*&---------------------------------------------------------------------*
 +
*&      Form  GRABAR
 +
*&---------------------------------------------------------------------*
 +
*      text
 +
*----------------------------------------------------------------------*
 +
*  -->  p1        text
 +
*  <--  p2        text
 +
*----------------------------------------------------------------------*
 +
FORM grabar .
 +
  CALL METHOD g_grid->refresh_table_display.
 +
ENDFORM.                    " GRABAR
 +
 
 +
*&---------------------------------------------------------------------*
 +
*&      Form  user_command
 +
*&---------------------------------------------------------------------*
 +
*      user command de CONTAINER
 +
*----------------------------------------------------------------------*
 +
*      -->I_FUNCTION text
 +
*----------------------------------------------------------------------*
 +
FORM user_command USING i_function TYPE salv_de_function.
 +
  DATA: gt_rows TYPE lvc_t_row.
 +
 
 +
  CALL METHOD g_grid->get_selected_rows
 +
    IMPORTING
 +
      et_index_rows = gt_rows.
 +
 
 +
  CASE i_function.
 +
    WHEN 'APRO'. "do something
 +
      LOOP AT gt_rows INTO DATA(l_tabix).
 +
        READ TABLE gt_data INDEX l_tabix INTO wa_data.
 +
      ENDLOOP.
 +
  ENDCASE.
 +
ENDFORM.</nowiki>
  
 
===Y_TEST_ALV_GRID_I01===
 
===Y_TEST_ALV_GRID_I01===

Revision as of 20:26, 4 April 2020

  • Ejemplo de CL_GUI_ALV_GRID con User Command en dynpro & Container y editar columna

YTEST_ALV_GRID

*----------------------------------------------------------------------*
* Report  YTEST_ALV_GRID
* Author: .....
*----------------------------------------------------------------------*
* ejemplo de CL_GUI_ALV_GRID con User Command en dynpro & Container
* y editar columna
*
*----------------------------------------------------------------------*
REPORT ytest_alv_grid.

TYPE-POOLS icon.

TYPES: BEGIN OF ty_data.
        INCLUDE TYPE p0002.
TYPES celltab TYPE lvc_t_styl.
TYPES     END OF ty_data.

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

DATA g_edit TYPE xfeld.

INCLUDE y_test_alv_grid_top.

*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*

SELECT-OPTIONS: s_pernr FOR wa_data-pernr MATCHCODE OBJECT prem.

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

  SELECT pernr vorna nachn name2 INTO CORRESPONDING FIELDS OF TABLE gt_data FROM pa0002 UP TO 100 ROWS
                              WHERE pernr IN s_pernr.

  gt_data_ori[] = gt_data[].

  PERFORM style CHANGING g_edit. "por defecto no se puede editar, ver user_command_0100 code EDIT
*--------------------------------------------------------------------*
END-OF-SELECTION.
*--------------------------------------------------------------------*
* display ALV
  CALL SCREEN 0100.  "en la dynpro crear container CC_ALV

  INCLUDE y_test_alv_grid_f01.
  INCLUDE y_test_alv_grid_o01.
  INCLUDE y_test_alv_grid_i01.

Y_TEST_ALV_GRID_TOP

* Y_TEST_ALV_GRID_TOP
*--------------------------------------------------------------------*
DATA: ok_code            LIKE sy-ucomm,
      save_ok            LIKE sy-ucomm,
      g_container        TYPE scrfname VALUE 'CC_ALV',
      g_grid             TYPE REF TO cl_gui_alv_grid,
      g_custom_container TYPE REF TO cl_gui_custom_container,
      gt_fieldcat        TYPE lvc_t_fcat,
      gs_layout          TYPE lvc_s_layo,
      gt_events TYPE slis_t_event.

DATA : g_variant1 TYPE disvariant,
       g_variant2 TYPE disvariant.

INCLUDE <icon>.
CLASS lcl_event_receiver DEFINITION DEFERRED.
DATA: g_event_receiver TYPE REF TO lcl_event_receiver.

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_toolbar
                    FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object e_interactive,

      handle_user_command
                    FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm.

    METHODS:
      handle_data_changed
                    FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING er_data_changed.

ENDCLASS.                    "lcl_event_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*   agregar botones a CONTAINER
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.

  METHOD handle_toolbar.
    DATA: ls_toolbar  TYPE stb_button.
* append a separator to normal toolbar
    CLEAR ls_toolbar.
    MOVE 3 TO ls_toolbar-butn_type.
    APPEND ls_toolbar TO e_object->mt_toolbar.
* append an icon to show booking table

*--------------------------------------------------------------------*
* se habilita botón Aprobar
*--------------------------------------------------------------------*
    CLEAR ls_toolbar.
    MOVE 'APRO' TO ls_toolbar-function.
    MOVE icon_execute_object TO ls_toolbar-icon.
    MOVE 'Aprobar' TO ls_toolbar-quickinfo.
    MOVE 'Aprobar' TO ls_toolbar-text.
    MOVE ' ' TO ls_toolbar-disabled.
    APPEND ls_toolbar TO e_object->mt_toolbar.

  ENDMETHOD.
*--------------------------------------------------------------------*
  METHOD handle_user_command.
*--------------------------------------------------------------------*
    PERFORM user_command USING e_ucomm.
  ENDMETHOD.

*--------------------------------------------------------------------*
  METHOD handle_data_changed.
*--------------------------------------------------------------------*
    DATA : ls_mod_cell TYPE lvc_s_modi,
           lv_value    TYPE lvc_value,
           lv_mask1    TYPE string,
           lv_mask2    TYPE string.

    DATA: l_nachn TYPE pa0002-nachn,
          l_vorna TYPE pa0002-vorna,
          l_name2 TYPE p0002-name2.
    DATA l_subrc TYPE sy-subrc.
    data l_dummy(200).

    DATA: lv_valor TYPE string.
    DATA: lv_mod TYPE i.

    SORT er_data_changed->mt_mod_cells BY row_id .

    LOOP AT er_data_changed->mt_good_cells INTO ls_mod_cell.
      IF ls_mod_cell-fieldname = 'VORNA'.

        CALL METHOD er_data_changed->get_cell_value
          EXPORTING
            i_row_id    = ls_mod_cell-row_id
            i_fieldname = 'VORNA'
          IMPORTING
            e_value     = l_vorna.

        READ TABLE gt_data_ori INTO wa_data INDEX ls_mod_cell-row_id.
        IF wa_data-vorna <> l_vorna.
            if l_vorna = 'Homero'.
              l_subrc = 1.
              MESSAGE e000(0k) with 'No se permiten Homeros' into l_dummy.
            endif.
        ELSE.
          l_subrc = 0.
        ENDIF.

        IF l_subrc <> 0.
          DATA(error_in_data) = 'X'.
          CALL METHOD er_data_changed->add_protocol_entry
            EXPORTING
              i_msgid     = sy-msgid
              i_msgno     = sy-msgno
              i_msgty     = sy-msgty
              i_msgv1     = sy-msgv1
              i_msgv2     = sy-msgv2
              i_msgv3     = sy-msgv3
              i_msgv4     = sy-msgv4
              i_fieldname = ls_mod_cell-fieldname
              i_row_id    = ls_mod_cell-row_id.

          CALL METHOD er_data_changed->modify_cell
            EXPORTING
              i_row_id    = ls_mod_cell-row_id
              i_fieldname = 'VORNA'
              i_value     = wa_data-vorna.
          CONTINUE.

        ENDIF.
      ENDIF.
    ENDLOOP.

    CALL METHOD g_grid->refresh_table_display.

    IF error_in_data EQ 'X'.
      CALL METHOD er_data_changed->display_protocol.
    ENDIF.
  ENDMETHOD.

ENDCLASS.

Y_TEST_ALV_GRID_F01

 *----------------------------------------------------------------------*
***Y_TEST_ALV_GRID_F01
*----------------------------------------------------------------------*
FORM fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
  DATA lt_fcat_save TYPE lvc_t_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'PA0002'
      i_client_never_display = 'X'
    CHANGING
      ct_fieldcat            = lt_fcat_save
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* colums names
*  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>).
*    CASE <wa>-fieldname.
*      WHEN 'ICON'.
*        <wa>-scrtext_l = 'Estatus'.
*        <wa>-scrtext_m = 'Estatus'.
*        <wa>-scrtext_s = <wa>-reptext = 'Estatus'.
*  endloop.

* hide, solo se desplegan columnas PERNR, VORNA, NACHN & NAME2
  LOOP AT lt_fcat_save ASSIGNING FIELD-SYMBOL(<wa>) WHERE fieldname <> 'PERNR'
                                        AND fieldname <> 'VORNA'
                                        AND fieldname <> 'NACHN'
                                        AND fieldname <> 'NAME2'.
    <wa>-tech = 'X'.
  ENDLOOP.

* enable VORNA para editar
  READ TABLE lt_fcat_save ASSIGNING <wa> WITH KEY fieldname = 'VORNA'.
  <wa>-edit = 'X'.

  pt_fieldcat[] = lt_fcat_save[].

ENDFORM.
*----------------------------------------------------------------------*
*       FORM style
*----------------------------------------------------------------------*
* Habilita/Des-Habilita edición de columna VORNA
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM style CHANGING p_edit.
  DATA: ls_celltab TYPE lvc_s_styl.
  DATA: lt_celltab TYPE lvc_t_styl.
  DATA l_tabix TYPE sy-tabix.

  LOOP AT gt_data INTO wa_data.
    l_tabix = sy-tabix.
    CLEAR lt_celltab[].
    CLEAR wa_data-celltab[].

    IF p_edit = 'X'.
    ELSE.
      ls_celltab-fieldname = 'VORNA'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      INSERT ls_celltab INTO TABLE lt_celltab.
    ENDIF.

    INSERT LINES OF lt_celltab INTO TABLE wa_data-celltab.
    MODIFY gt_data FROM wa_data INDEX l_tabix.
  ENDLOOP.

  IF p_edit = 'X'.
    p_edit = space.
  ELSE.
    p_edit = 'X'.
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_DATA[]  text
*      <--P_GT_FIELDCAT  text
*----------------------------------------------------------------------*

FORM create_and_init_alv CHANGING pt_outtab TYPE STANDARD TABLE
                                  pt_fieldcat TYPE lvc_t_fcat.

  DATA: lt_exclude TYPE ui_functions.
  DATA: ps_layout TYPE lvc_s_layo.

  CREATE OBJECT g_custom_container
    EXPORTING
      container_name = g_container.
  CREATE OBJECT g_grid
    EXPORTING
      i_parent = g_custom_container.

  PERFORM fieldcat CHANGING pt_fieldcat.

  PERFORM exclude_tb_functions CHANGING lt_exclude.

  ps_layout-zebra = 'X'.
  ps_layout-cwidth_opt = 'X'.
  ps_layout-stylefname = 'CELLTAB'.

  g_variant1-report = sy-repid.
  g_variant1-handle = 'X'.
  g_variant1-username = sy-uname.

  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save        = 'A'
    CHANGING
      cs_variant    = g_variant1
    EXCEPTIONS
      wrong_input   = 1
      not_found     = 2
      program_error = 3
      OTHERS        = 4.

  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_save               = 'A'
      is_variant           = g_variant1
      is_layout            = ps_layout
      it_toolbar_excluding = lt_exclude
    CHANGING
      it_fieldcatalog      = pt_fieldcat
      it_outtab            = pt_outtab[].

  CALL METHOD g_grid->set_ready_for_input
    EXPORTING
      i_ready_for_input = 1.

  CALL METHOD g_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.

*  CALL METHOD g_grid->save_variant.

  CREATE OBJECT g_event_receiver.
  SET HANDLER g_event_receiver->handle_user_command FOR g_grid.
  SET HANDLER g_event_receiver->handle_toolbar FOR g_grid.
  SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
  CALL METHOD g_grid->set_toolbar_interactive.


ENDFORM.                    " CREATE_AND_INIT_ALV

*&---------------------------------------------------------------------*
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PT_EXCLUDE text
*----------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
  DATA ls_exclude TYPE ui_func.

  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
  APPEND ls_exclude TO pt_exclude.

ENDFORM.                    " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*&      Form  GRABAR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM grabar .
  CALL METHOD g_grid->refresh_table_display.
ENDFORM.                    " GRABAR

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*       user command de CONTAINER
*----------------------------------------------------------------------*
*      -->I_FUNCTION text
*----------------------------------------------------------------------*
FORM user_command USING i_function TYPE salv_de_function.
  DATA: gt_rows TYPE lvc_t_row.

  CALL METHOD g_grid->get_selected_rows
    IMPORTING
      et_index_rows = gt_rows.

  CASE i_function.
    WHEN 'APRO'. "do something
      LOOP AT gt_rows INTO DATA(l_tabix).
        READ TABLE gt_data INDEX l_tabix INTO wa_data.
      ENDLOOP.
  ENDCASE.
ENDFORM.

Y_TEST_ALV_GRID_I01

 *----------------------------------------------------------------------*
 ***INCLUDE Y_TEST_ALV_GRID_I01
 *----------------------------------------------------------------------*
 *&---------------------------------------------------------------------*
 *&      Module  USER_COMMAND_0100  INPUT
 *&---------------------------------------------------------------------*
 *       user command de dynpro 0100
 *----------------------------------------------------------------------*
 MODULE user_command_0100 INPUT.
   save_ok = ok_code.
   CLEAR ok_code.
 
   CASE save_ok.
 *--------------------------------------------------------------------*
     WHEN '&F03' OR "back
          '&F15'  OR"leave
          '&F12'. "canc
 *--------------------------------------------------------------------*
       SET SCREEN 0.
       LEAVE SCREEN.
 
 *--------------------------------------------------------------------*
     WHEN 'EDIT'.  "Habilitar/Des-Habilitar Editar Columnas
 *--------------------------------------------------------------------*
       PERFORM style CHANGING g_edit.
 
       CALL METHOD g_grid->refresh_table_display.
 *--------------------------------------------------------------------*
     WHEN 'GRABAR'.
 *--------------------------------------------------------------------*
       PERFORM grabar.
 *--------------------------------------------------------------------*
     WHEN 'LOG'.
 *--------------------------------------------------------------------*
 
     WHEN OTHERS.
 *     do nothing
   ENDCASE.
 
 
 ENDMODULE.                 " USER_COMMAND_0100  INPUT

Y_TEST_ALV_GRID_O01

 *----------------------------------------------------------------------*
 ***INCLUDE Y_TEST_ALV_GRID_O01
 *----------------------------------------------------------------------*
 *&---------------------------------------------------------------------*
 *&      Module  STATUS_0100  OUTPUT
 *&---------------------------------------------------------------------*
 *       text
 *----------------------------------------------------------------------*
 MODULE status_0100 OUTPUT.
 
   SET PF-STATUS 'LIST_ALV'. ""copiar de SALV_DEMO_TABLE_LAYOUT
   SET TITLEBAR 'T01'.
 
   IF G_CUSTOM_CONTAINER IS INITIAL.
     PERFORM CREATE_AND_INIT_ALV CHANGING GT_DATA[]
                                          GT_FIELDCAT.
 
   ELSE.
 *    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY.
     CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = G_GRID.
   ENDIF.
 ENDMODULE.                 " STATUS_0100  OUTPUT

Dynpro 0100

  • Crear Custom Container con nombre CC_ALV
  • Agregar elemento OK_CODE de tipo OK
  • Crear Status GUI LIST_ALV copia de programa SALV_DEMO_TABLE_LAYOUT Status SALV_STANDARD
  • Agregar Código de función EDIT - Editar - ICON_TOGGLE_DISPLAY_CHANGE
  • Agregar Código de función GRABAR - Grabar - ICON_SYSTEM_SAVE
  • Crear Título T01 "Test CL_GUI_ALV_GRID"
PROCESS BEFORE OUTPUT.
  MODULE status_0100.
*
PROCESS AFTER INPUT.
  MODULE user_command_0100.