SAP ABAP ALV REUSE ALV GRID DISPLAY

From SapWiki
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.