Difference between revisions of "SAP ABAP ALV REUSE ALV GRID DISPLAY"

From SapWiki
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Ejemplo con edición de columna, eventos USER_COMMAND y DATA_CHANGED==
 
==Ejemplo con edición de columna, eventos USER_COMMAND y DATA_CHANGED==
  *&---------------------------------------------------------------------*
+
  <nowiki>*&---------------------------------------------------------------------*
*& Report YALV
+
*& Report YALV
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*& ejemplo reuse_alv_grid_display con edición, eventos user_command y data_changed
+
*& ejemplo reuse_alv_grid_display con edición, eventos user_command y data_changed
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
REPORT yalv_grid.
+
REPORT yalv_grid.
TYPES: BEGIN OF ty_data,
+
TYPES: BEGIN OF ty_data,
          checkbox type checkbox,
+
        checkbox type checkbox,
          vbeln      TYPE vbrk-vbeln,
+
        vbeln      TYPE vbrk-vbeln,
          fkart      TYPE vbrk-fkart,
+
        fkart      TYPE vbrk-fkart,
          fkdat      TYPE vbrk-fkdat,
+
        fkdat      TYPE vbrk-fkdat,
          netwr      TYPE vbrk-netwr,
+
        netwr      TYPE vbrk-netwr,
          waerk      TYPE vbrk-waerk,
+
        waerk      TYPE vbrk-waerk,
        END OF ty_data.
+
      END OF ty_data.
+
 
DATA gt_data TYPE TABLE OF ty_data.
+
DATA gt_data TYPE TABLE OF ty_data.
DATA wa_data LIKE LINE OF gt_data.
+
DATA wa_data LIKE LINE OF gt_data.
+
 
**********************************************************************
+
**********************************************************************
*ALV DATA
+
*ALV DATA
**********************************************************************
+
**********************************************************************
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
+
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      gt_events  TYPE slis_t_event,
+
      gt_events  TYPE slis_t_event,
      gs_layout  TYPE slis_layout_alv.
+
      gs_layout  TYPE slis_layout_alv.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
*
+
*
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
SELECT-OPTIONS s_vbeln FOR wa_data-vbeln.
+
SELECT-OPTIONS s_vbeln FOR wa_data-vbeln.
+
 
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
START-OF-SELECTION.
+
START-OF-SELECTION.
*--------------------------------------------------------------------*
+
*--------------------------------------------------------------------*
+
 
  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data FROM vbrk UP TO 100 ROWS
+
  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data FROM vbrk UP TO 100 ROWS
                                                      WHERE vbeln IN s_vbeln.
+
                                                    WHERE vbeln IN s_vbeln.
+
 
  PERFORM display_alv.
+
  PERFORM display_alv.
+
 
+
 
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  SET_PF_STATUS
+
*&      Form  SET_PF_STATUS
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*      -->RT_EXTAB  text
+
*      -->RT_EXTAB  text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.        "#EC NEEDED
+
FORM set_pf_status USING rt_extab TYPE slis_t_extab.        "#EC NEEDED
+
 
  SET PF-STATUS 'STAT_ALV'. "copiar de SALV_DEMO_TABLE_LAYOUT    SALV_STANDARD
+
  SET PF-STATUS 'STAT_ALV'. "copiar de SALV_DEMO_TABLE_LAYOUT    SALV_STANDARD
+
 
ENDFORM.                    "SET_PF_STATUS
+
ENDFORM.                    "SET_PF_STATUS
+
 
FORM user_command USING r_ucomm LIKE sy-ucomm
+
FORM user_command USING r_ucomm LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.
+
                        rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
+
  CASE r_ucomm.
    WHEN 'PRINT'. "ICON_PRINT
+
    WHEN 'PRINT'. "ICON_PRINT
      loop at gt_data into wa_data where checkbox = 'X'.
+
    loop at gt_data into wa_data where checkbox = 'X'.
* do something
+
* do something
        MESSAGE i000(0k) WITH 'Valor neto:' wa_data-netwr.
+
      MESSAGE i000(0k) WITH 'Valor neto:' wa_data-netwr.
      endloop.
+
    endloop.
  ENDCASE.
+
  ENDCASE.
ENDFORM.
+
*rs_selfield-refresh = 'X'.
+
ENDFORM.
FORM top_of_page.
+
 
+
FORM top_of_page.
ENDFORM.
+
 
+
ENDFORM.
FORM build_fieldcat.
+
 
  data ls_fieldcat like LINE OF gt_fieldcat.
+
FORM build_fieldcat.
+
  data ls_fieldcat like LINE OF gt_fieldcat.
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
+
 
    EXPORTING
+
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      i_structure_name      = 'VBRK'
+
    EXPORTING
    CHANGING
+
      i_structure_name      = 'VBRK'
      ct_fieldcat            = gt_fieldcat
+
    CHANGING
    EXCEPTIONS
+
      ct_fieldcat            = gt_fieldcat
      inconsistent_interface = 1
+
    EXCEPTIONS
      program_error          = 2
+
      inconsistent_interface = 1
      OTHERS                = 3.
+
      program_error          = 2
  IF sy-subrc NE 0.
+
      OTHERS                = 3.
    EXIT.
+
  IF sy-subrc NE 0.
  ENDIF.
+
    EXIT.
+
  ENDIF.
  DELETE gt_fieldcat WHERE fieldname <> 'VBELN'
+
 
                        AND fieldname <> 'FKART'
+
  DELETE gt_fieldcat WHERE fieldname <> 'VBELN'
                        AND fieldname <> 'FKDAT'
+
                      AND fieldname <> 'FKART'
                        AND fieldname <> 'NETWR'
+
                      AND fieldname <> 'FKDAT'
                        AND fieldname <> 'WAERK'.
+
                      AND fieldname <> 'NETWR'
+
                      AND fieldname <> 'WAERK'.
  READ TABLE gt_fieldcat ASSIGNING FIELD-SYMBOL(<wa>) WITH KEY fieldname = 'NETWR'.
+
 
  <wa>-edit = 'X'.
+
  READ TABLE gt_fieldcat ASSIGNING FIELD-SYMBOL(<wa>) WITH KEY fieldname = 'NETWR'.
+
  <wa>-edit = 'X'.
ENDFORM.
+
 
*&---------------------------------------------------------------------*
+
ENDFORM.
*&      Form  BUILD_EVENTS
+
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
+
*&      Form  BUILD_EVENTS
*      Build events table
+
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      Build events table
FORM build_events.
+
*----------------------------------------------------------------------*
  DATA: ls_event TYPE slis_alv_event.
+
FORM build_events.
+
  DATA: ls_event TYPE slis_alv_event.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
+
 
    EXPORTING
+
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
      i_list_type = 0
+
    EXPORTING
    IMPORTING
+
      i_list_type = 0
      et_events  = gt_events[].
+
    IMPORTING
+
      et_events  = gt_events[].
  MOVE 'DATA_CHANGED' TO ls_event-form.
+
 
  MOVE 'DATA_CHANGED' TO ls_event-name.
+
  MOVE 'DATA_CHANGED' TO ls_event-form.
  APPEND ls_event TO gt_events.
+
  MOVE 'DATA_CHANGED' TO ls_event-name.
+
  APPEND ls_event TO gt_events.
ENDFORM.                    " BUILD_EVENTS
+
 
+
ENDFORM.                    " BUILD_EVENTS
*&---------------------------------------------------------------------*
+
 
*&      Form  BUILD_LAYOUT
+
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
+
*&      Form  BUILD_LAYOUT
*      Build layout for ALV grid report
+
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      Build layout for ALV grid report
FORM build_layout.
+
*----------------------------------------------------------------------*
  gs_layout-colwidth_optimize = 'X'.
+
FORM build_layout.
  gs_layout-header_text      = 'Facturas'.
+
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-box_fieldname    = 'CHECKBOX'.
+
  gs_layout-header_text      = 'Facturas'.
+
  gs_layout-box_fieldname    = 'CHECKBOX'.
ENDFORM.                    " BUILD_LAYOUT
+
 
+
ENDFORM.                    " BUILD_LAYOUT
*----------------------------------------------------------------------*
+
 
*      FORM .......
+
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      FORM .......
*      text
+
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      text
*  -->  p1        text
+
*----------------------------------------------------------------------*
*  <--  p2        text
+
*  -->  p1        text
*----------------------------------------------------------------------*
+
*  <--  p2        text
FORM display_alv.
+
*----------------------------------------------------------------------*
+
FORM display_alv.
  DATA : ls_grid_setting TYPE lvc_s_glay.
+
 
+
  DATA : ls_grid_setting TYPE lvc_s_glay.
  ls_grid_setting-edt_cll_cb = 'X'.
+
 
  DATA(l_repid) = sy-repid.
+
  ls_grid_setting-edt_cll_cb = 'X'.
+
  DATA(l_repid) = sy-repid.
  PERFORM build_fieldcat.
+
 
  PERFORM build_events.
+
  PERFORM build_fieldcat.
  PERFORM build_layout.
+
  PERFORM build_events.
+
  PERFORM build_layout.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
+
 
    EXPORTING
+
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      i_callback_program      = l_repid
+
    EXPORTING
*    i_callback_top_of_page  = 'TOP-OF-PAGE'  "see FORM
+
      i_callback_program      = l_repid
      i_callback_pf_status_set = 'SET_PF_STATUS'
+
*    i_callback_top_of_page  = 'TOP-OF-PAGE'  "see FORM
      i_callback_user_command  = 'USER_COMMAND'
+
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_grid_settings          = ls_grid_setting
+
      i_callback_user_command  = 'USER_COMMAND'
      is_layout                = gs_layout
+
      i_grid_settings          = ls_grid_setting
      it_fieldcat              = gt_fieldcat
+
      is_layout                = gs_layout
*    is_variant              = gv_variante
+
      it_fieldcat              = gt_fieldcat
      it_events                = gt_events
+
*    is_variant              = gv_variante
*    is_print                = gd_prntparams
+
      it_events                = gt_events
      i_save                  = 'X'
+
*    is_print                = gd_prntparams
    TABLES
+
      i_save                  = 'X'
      t_outtab                = gt_data
+
    TABLES
    EXCEPTIONS
+
      t_outtab                = gt_data
      program_error            = 1
+
    EXCEPTIONS
      OTHERS                  = 2.
+
      program_error            = 1
  IF sy-subrc <> 0.
+
      OTHERS                  = 2.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
+
  IF sy-subrc <> 0.
*        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
+
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  ENDIF.
+
*        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
+
  ENDIF.
ENDFORM.
+
 
+
ENDFORM.
*&---------------------------------------------------------------------*
+
 
*&      Form  data_changed
+
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
+
*&      Form  data_changed
*      text
+
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      text
*      -->RR_DATA    text
+
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
*      -->RR_DATA    text
FORM data_changed USING rr_data TYPE REF TO cl_alv_changed_data_protocol.
+
*----------------------------------------------------------------------*
  DATA : ls_mod_cell TYPE lvc_s_modi.
+
FORM data_changed USING rr_data TYPE REF TO cl_alv_changed_data_protocol.
  DATA: l_netwr TYPE vbrk-netwr.
+
  DATA : ls_mod_cell TYPE lvc_s_modi.
+
  DATA: l_netwr TYPE vbrk-netwr.
  SORT rr_data->mt_mod_cells BY row_id .
+
 
  LOOP AT rr_data->mt_mod_cells INTO ls_mod_cell.
+
  SORT rr_data->mt_mod_cells BY row_id .
    IF ls_mod_cell-fieldname = 'NETWR'.
+
  LOOP AT rr_data->mt_mod_cells INTO ls_mod_cell.
      CALL METHOD rr_data->get_cell_value
+
    IF ls_mod_cell-fieldname = 'NETWR'.
        EXPORTING
+
      CALL METHOD rr_data->get_cell_value
          i_row_id    = ls_mod_cell-row_id
+
        EXPORTING
          i_fieldname = 'NETWR'
+
          i_row_id    = ls_mod_cell-row_id
        IMPORTING
+
          i_fieldname = 'NETWR'
          e_value    = l_netwr.
+
        IMPORTING
+
          e_value    = l_netwr.
      IF l_netwr > 10000. "valida monto no mayor a 1.000.000
+
 
        CALL METHOD rr_data->add_protocol_entry
+
      IF l_netwr > 10000. "valida monto no mayor a 1.000.000
          EXPORTING
+
        CALL METHOD rr_data->add_protocol_entry
            i_msgid    = '0K'
+
          EXPORTING
            i_msgno    = '000'
+
            i_msgid    = '0K'
            i_msgty    = 'E'
+
            i_msgno    = '000'
            i_msgv1    = 'Monto mayor a 1.000.000'
+
            i_msgty    = 'E'
            i_msgv2    = ' '
+
            i_msgv1    = 'Monto mayor a 1.000.000'
            i_msgv3    = ' '
+
            i_msgv2    = ' '
            i_fieldname = ls_mod_cell-fieldname
+
            i_msgv3    = ' '
            i_row_id    = ls_mod_cell-row_id.
+
            i_fieldname = ls_mod_cell-fieldname
        CONTINUE.
+
            i_row_id    = ls_mod_cell-row_id.
      ENDIF.
+
        CONTINUE.
+
      ENDIF.
      ls_mod_cell-value = l_netwr.
+
 
+
      ls_mod_cell-value = l_netwr.
*  Para actualizar la celda.
+
 
*    CALL METHOD rr_data->modify_cell
+
*  Para actualizar la celda.
*      EXPORTING
+
*    CALL METHOD rr_data->modify_cell
*        i_row_id    = ls_mod_cell-row_id
+
*      EXPORTING
*        i_fieldname = ls_mod_cell-fieldname
+
*        i_row_id    = ls_mod_cell-row_id
*        i_value    = ls_mod_cell-value.
+
*        i_fieldname = ls_mod_cell-fieldname
+
*        i_value    = ls_mod_cell-value.
    ENDIF.
+
 
+
    ENDIF.
  ENDLOOP.
+
 
+
  ENDLOOP.
ENDFORM.
+
 
 +
ENDFORM.</nowiki>

Latest revision as of 12:34, 6 May 2020

Ejemplo con edición de columna, eventos USER_COMMAND y DATA_CHANGED

*&---------------------------------------------------------------------*
*& Report YALV
*&---------------------------------------------------------------------*
*& ejemplo reuse_alv_grid_display con edición, eventos user_command y data_changed
*&---------------------------------------------------------------------*
REPORT yalv_grid.
TYPES: BEGIN OF ty_data,
         checkbox type checkbox,
         vbeln      TYPE vbrk-vbeln,
         fkart      TYPE vbrk-fkart,
         fkdat      TYPE vbrk-fkdat,
         netwr      TYPE vbrk-netwr,
         waerk      TYPE vbrk-waerk,
       END OF ty_data.

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

**********************************************************************
*ALV DATA
**********************************************************************
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      gt_events   TYPE slis_t_event,
      gs_layout   TYPE slis_layout_alv.
*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*
SELECT-OPTIONS s_vbeln FOR wa_data-vbeln.

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

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

  PERFORM display_alv.


*&---------------------------------------------------------------------*
*&      Form  SET_PF_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.        "#EC NEEDED

  SET PF-STATUS 'STAT_ALV'. "copiar de SALV_DEMO_TABLE_LAYOUT    SALV_STANDARD

ENDFORM.                    "SET_PF_STATUS

FORM user_command USING r_ucomm LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN 'PRINT'. "ICON_PRINT
     loop at gt_data into wa_data where checkbox = 'X'.
* do something
       MESSAGE i000(0k) WITH 'Valor neto:' wa_data-netwr.
     endloop.
  ENDCASE.
*rs_selfield-refresh = 'X'.
ENDFORM.

FORM top_of_page.

ENDFORM.

FORM build_fieldcat.
  data ls_fieldcat like LINE OF gt_fieldcat.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'VBRK'
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.

  DELETE gt_fieldcat WHERE fieldname <> 'VBELN'
                       AND fieldname <> 'FKART'
                       AND fieldname <> 'FKDAT'
                       AND fieldname <> 'NETWR'
                       AND fieldname <> 'WAERK'.

  READ TABLE gt_fieldcat ASSIGNING FIELD-SYMBOL(<wa>) WITH KEY fieldname = 'NETWR'.
  <wa>-edit = 'X'.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  BUILD_EVENTS
*&---------------------------------------------------------------------*
*       Build events table
*----------------------------------------------------------------------*
FORM build_events.
  DATA: ls_event TYPE slis_alv_event.

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = gt_events[].

  MOVE 'DATA_CHANGED' TO ls_event-form.
  MOVE 'DATA_CHANGED' TO ls_event-name.
  APPEND ls_event TO gt_events.

ENDFORM.                    " BUILD_EVENTS

*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-header_text       = 'Facturas'.
  gs_layout-box_fieldname     = 'CHECKBOX'.

ENDFORM.                    " BUILD_LAYOUT

*----------------------------------------------------------------------*
*       FORM .......
*----------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv.

  DATA : ls_grid_setting TYPE lvc_s_glay.

  ls_grid_setting-edt_cll_cb = 'X'.
  DATA(l_repid) = sy-repid.

  PERFORM build_fieldcat.
  PERFORM build_events.
  PERFORM build_layout.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = l_repid
*     i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_callback_user_command  = 'USER_COMMAND'
      i_grid_settings          = ls_grid_setting
      is_layout                = gs_layout
      it_fieldcat              = gt_fieldcat
*     is_variant               = gv_variante
      it_events                = gt_events
*     is_print                 = gd_prntparams
      i_save                   = 'X'
    TABLES
      t_outtab                 = gt_data
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  data_changed
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RR_DATA    text
*----------------------------------------------------------------------*
FORM data_changed USING rr_data TYPE REF TO cl_alv_changed_data_protocol.
  DATA : ls_mod_cell TYPE lvc_s_modi.
  DATA: l_netwr TYPE vbrk-netwr.

  SORT rr_data->mt_mod_cells BY row_id .
  LOOP AT rr_data->mt_mod_cells INTO ls_mod_cell.
    IF ls_mod_cell-fieldname = 'NETWR'.
      CALL METHOD rr_data->get_cell_value
        EXPORTING
          i_row_id    = ls_mod_cell-row_id
          i_fieldname = 'NETWR'
        IMPORTING
          e_value     = l_netwr.

      IF l_netwr > 10000. "valida monto no mayor a 1.000.000
        CALL METHOD rr_data->add_protocol_entry
          EXPORTING
            i_msgid     = '0K'
            i_msgno     = '000'
            i_msgty     = 'E'
            i_msgv1     = 'Monto mayor a 1.000.000'
            i_msgv2     = ' '
            i_msgv3     = ' '
            i_fieldname = ls_mod_cell-fieldname
            i_row_id    = ls_mod_cell-row_id.
        CONTINUE.
      ENDIF.

      ls_mod_cell-value = l_netwr.

*   Para actualizar la celda.
*    CALL METHOD rr_data->modify_cell
*      EXPORTING
*        i_row_id    = ls_mod_cell-row_id
*        i_fieldname = ls_mod_cell-fieldname
*        i_value     = ls_mod_cell-value.

    ENDIF.

  ENDLOOP.

ENDFORM.