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

From SapWiki
Line 2: Line 2:
  
 
===YTEST_ALV_GRID===
 
===YTEST_ALV_GRID===
  *----------------------------------------------------------------------*
+
  <nowiki> *----------------------------------------------------------------------*
 
  * Report  YTEST_ALV_GRID
 
  * Report  YTEST_ALV_GRID
 
  * Author: .....
 
  * Author: .....
Line 51: Line 51:
 
   INCLUDE y_test_alv_grid_f01.
 
   INCLUDE y_test_alv_grid_f01.
 
   INCLUDE y_test_alv_grid_o01.
 
   INCLUDE y_test_alv_grid_o01.
   INCLUDE y_test_alv_grid_i01.
+
   INCLUDE y_test_alv_grid_i01.</nowiki>
  
 
===Y_TEST_ALV_GRID_TOP===
 
===Y_TEST_ALV_GRID_TOP===
* Y_TEST_ALV_GRID_TOP
+
  <nowiki>* Y_TEST_ALV_GRID_TOP
 
  *--------------------------------------------------------------------*
 
  *--------------------------------------------------------------------*
 
  DATA: ok_code            LIKE sy-ucomm,
 
  DATA: ok_code            LIKE sy-ucomm,
Line 199: Line 199:
 
   ENDMETHOD.
 
   ENDMETHOD.
 
   
 
   
  ENDCLASS.
+
  ENDCLASS.</nowiki>
  
 
===Y_TEST_ALV_GRID_F01===
 
===Y_TEST_ALV_GRID_F01===
  *----------------------------------------------------------------------*
+
  <nowiki> *----------------------------------------------------------------------*
 
  ***Y_TEST_ALV_GRID_F01
 
  ***Y_TEST_ALV_GRID_F01
 
  *----------------------------------------------------------------------*
 
  *----------------------------------------------------------------------*
Line 422: Line 422:
 
       ENDLOOP.
 
       ENDLOOP.
 
   ENDCASE.
 
   ENDCASE.
  ENDFORM.
+
  ENDFORM.</nowiki>
  
 
===Y_TEST_ALV_GRID_I01===
 
===Y_TEST_ALV_GRID_I01===
  *----------------------------------------------------------------------*
+
  <nowiki> *----------------------------------------------------------------------*
 
  ***INCLUDE Y_TEST_ALV_GRID_I01
 
  ***INCLUDE Y_TEST_ALV_GRID_I01
 
  *----------------------------------------------------------------------*
 
  *----------------------------------------------------------------------*
Line 465: Line 465:
 
   
 
   
 
   
 
   
  ENDMODULE.                " USER_COMMAND_0100  INPUT
+
  ENDMODULE.                " USER_COMMAND_0100  INPUT</nowiki>
  
 
===Y_TEST_ALV_GRID_O01===
 
===Y_TEST_ALV_GRID_O01===
  *----------------------------------------------------------------------*
+
  <nowiki> *----------------------------------------------------------------------*
 
  ***INCLUDE Y_TEST_ALV_GRID_O01
 
  ***INCLUDE Y_TEST_ALV_GRID_O01
 
  *----------------------------------------------------------------------*
 
  *----------------------------------------------------------------------*
Line 489: Line 489:
 
     CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = G_GRID.
 
     CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = G_GRID.
 
   ENDIF.
 
   ENDIF.
  ENDMODULE.                " STATUS_0100  OUTPUT
+
  ENDMODULE.                " STATUS_0100  OUTPUT</nowiki>
 
 
 
 
  
 
===Dynpro 0100===
 
===Dynpro 0100===

Revision as of 14:32, 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.