ALV 回车事件

ALV回车事件

  • 前言
  • 1、创建报表
    • 1.1 事务码SE38
    • 1.2 点击创建
  • 2、定义内表、变量、工作区
  • 3、自定义监听事件类
  • 4、监听类的实现
  • 5、监听方法的实现
  • 6、注册回车事件
  • 7、简单报表实现
    • 7.1 定义屏幕选择条件
    • 7.2 获取数据
    • 7.3 ALV展示
    • 7.4 ALV字段设置
    • 7.5 GUI状态设置
  • 8、最终效果
    • 8.1 查询界面
    • 8.2 查询结果界面

前言

ALV报表中,可编辑列输入数据后,回车,后台做相应的数据处理后重新刷新报表显示。这时候需要用到监听事件及回车方法来是实现业务

1、创建报表

1.1 事务码SE38

ALV 回车事件_第1张图片

1.2 点击创建

ALV 回车事件_第2张图片

2、定义内表、变量、工作区

*---------------------------------------------------------------------*
* 定义内表、工作区、变量
*---------------------------------------------------------------------*

DATA gt_mseg LIKE TABLE OF mseg. "物料凭证明细
DATA lt_mseg LIKE LINE OF gt_mseg.
DATA gt_mkpf LIKE TABLE OF mkpf. "物料凭证抬头
DATA lt_mkpf LIKE LINE OF gt_mkpf.

"ALV格式
DATA: i_grid_settings TYPE lvc_s_glay.
DATA: fieldcat  TYPE lvc_t_fcat.            " 存放字段目录的内表
DATA: field TYPE lvc_s_fcat.
DATA: layout TYPE lvc_s_layo.               "布局结构
DATA: l_grid TYPE REF TO cl_gui_alv_grid.

DATA: stylelin TYPE lvc_s_styl.             "单元格样式
DATA: lt_events TYPE slis_t_event,
      ls_events TYPE slis_alv_event.

TYPES:BEGIN OF dtab,
        checkbox TYPE char01,
        mblnr    LIKE mseg-mblnr,                                   "送检单号(物料凭证号)
        mjahr    LIKE mseg-mjahr,                                   "凭证年度
        zeile    LIKE mseg-zeile,                                   "物料凭证行号
        werks    LIKE mseg-werks,                                   "工厂
        ebeln    LIKE mseg-ebeln,                                   "采购订单
        ebelp    LIKE mseg-ebelp,                                   "采购订单行号
        matnr    LIKE ekpo-matnr,                                   "物料号
        txz01    LIKE ekpo-txz01,                                   "物料描述
        maktx    LIKE makt-maktx,                                   "物料描述
        lifnr    LIKE mseg-lifnr,                                   "供应商编码
        meins    LIKE mseg-erfme,                                   "单位
        ktmng    LIKE mseg-erfmg,                                   "送检数量
        name1    LIKE lfa1-name1,                                   "供应商名称
        budat    LIKE mseg-budat_mkpf,                              "过账日期
        ekorg    LIKE ekko-ekorg,                                   "采购组织
        ihrez    LIKE ekko-ihrez,                                   "您的参考
        vbeln    LIKE zscaninspect-vbeln,                           "发货单号
        posnr    LIKE zscaninspect-posnr,                           "发货单行号
        charg    LIKE zscaninspect-charg,                           "南都批次号
        hsdat    LIKE zscaninspect-hsdat,                           "生产日期
        zscpc    LIKE zscaninspect-zscpc,                           "生产批次号
        zjjfx    LIKE ekpo-menge,                                   "紧急放行数量
        zrbjs    LIKE zscaninspect-zrbjs,                           "让步接受数量
        zfxfg    LIKE zscaninspect-zfxfg,                           "返修返工数量
        zjs      LIKE zscaninspect-zjs,                             "拒收数量
        zjjsy    LIKE zscaninspect-zjjsy,                           "降级使用数量
        zhg      LIKE zscaninspect-zhg,                             "合格数量
        zbhg     LIKE zscaninspect-zbhg,                            "不合格数量
        zoaplbhg LIKE zscaninspect-zoaplbhg,                        "批量不合格单
        zsfhg    LIKE zscaninspect-zsfhg,                           "是否合格
        zclyj    LIKE zscaninspect-zclyj,                           "处理意见
        zreason  LIKE zscaninspect-zreason,                         "检验结果原因记录
        zshzt    LIKE zscaninspect-zshzt,                           "审核状态
        flag     LIKE zscaninspect-flag,                            "审核标识
        f_style  TYPE lvc_t_styl,                                   "单元格样式
      END OF dtab.



DATA gt_dtab TYPE STANDARD TABLE OF dtab WITH HEADER LINE.
DATA gs_dtab TYPE dtab.

3、自定义监听事件类

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*       类定义-自定义监听事件类
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.

  PUBLIC SECTION.
    DATA: ucomm TYPE sy-ucomm.

    METHODS handle_data_changed
                  FOR EVENT data_changed OF cl_gui_alv_grid
      IMPORTING er_data_changed.
  PRIVATE SECTION.
ENDCLASS.                    "lcl_event_receiver DEFINITION

4、监听类的实现

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*       类实现
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.

  METHOD handle_data_changed.
    PERFORM handle_data_changed USING er_data_changed.

  ENDMETHOD.                    "handle_data_changed

ENDCLASS.

5、监听方法的实现

"监听方法
FORM handle_data_changed USING p_er_data_changed TYPE REF TO   cl_alv_changed_data_protocol.

  DATA : x_change TYPE lvc_s_modi .

  LOOP AT p_er_data_changed->mt_good_cells INTO x_change.
    IF x_change-fieldname = 'ZCLYJ'.                          "处理意见
      READ TABLE gt_dtab INTO gs_dtab INDEX x_change-row_id.
      IF sy-subrc = 0.
        IF x_change-value = '01'.                             "紧急放行
          CLEAR stylelin.
          stylelin-fieldname = 'ZJJFX'.                       " 需要编辑的列名:紧急放行数量
          stylelin-style = cl_gui_alv_grid=>mc_style_enabled. " 设置为可编辑状态
          INSERT stylelin INTO TABLE gs_dtab-f_style.

          MODIFY gt_dtab FROM gs_dtab INDEX x_change-row_id.

        ELSEIF x_change-value = '02'.
          CLEAR stylelin.
          stylelin-fieldname = 'ZRBJS'.                       " 需要编辑的列名:让步接收数量
          stylelin-style = cl_gui_alv_grid=>mc_style_enabled. " 设置为可编辑状态
          INSERT stylelin INTO TABLE gs_dtab-f_style.

          MODIFY gt_dtab FROM gs_dtab INDEX x_change-row_id.

        ENDIF.
        CLEAR gs_dtab.

      ENDIF.

    ENDIF.

    CALL METHOD l_grid->refresh_table_display
      EXCEPTIONS
        finished = 1
        OTHERS   = 2.

  ENDLOOP.

ENDFORM.

6、注册回车事件

*&---------------------------------------------------------------------*
*&      Form  register_events
*&---------------------------------------------------------------------*
*       注册回车事件
*----------------------------------------------------------------------
FORM frm_register_events USING e_grid TYPE slis_data_caller_exit.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = l_grid.

  DATA: gr_event_handler TYPE REF TO lcl_event_receiver.
  CREATE OBJECT gr_event_handler.

  CALL METHOD l_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.

  SET HANDLER gr_event_handler->handle_data_changed FOR l_grid.

  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. "register_events

7、简单报表实现

7.1 定义屏幕选择条件

*---------------------------------------------------------------------*
* 定义输入屏幕变量
*---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME TITLE text-001.

SELECT-OPTIONS s_mblnr FOR mseg-mblnr.
"采购订单号
PARAMETERS s_ebeln TYPE ekpo-ebeln .

SELECTION-SCREEN END OF BLOCK blk01.

7.2 获取数据

  PERFORM get_data.

  PERFORM display_alv.

*1、获取数据
FORM get_data.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE gt_dtab
    FROM mseg
    WHERE mblnr = s_mblnr
    and ebeln = s_ebeln
    ORDER BY mblnr ASCENDING
             ebeln ASCENDING..

ENDFORM.

7.3 ALV展示

*2、ALV显示
FORM display_alv.

  PERFORM create_fieldcat USING:
    fieldcat 'CHECKBOX' '选择' '4' 'X' '' 'X' '' 'X' '' '' '' '' '' '' '' '',
    fieldcat 'MBLNR' '送检单号' '10' '' '' 'X' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'ZEILE' '送检单行号' '4' '' '' 'X' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'VBELN' '送货单号' '20' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'POSNR' '送货单行号' '10' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'EBELN' '采购订单号' '10' '' '' '' '' '' '' 'X' '' '' '' '' '' '',
    fieldcat 'EBELP' '采购行项目' '5' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'BUDAT' '过账日期' '8' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'WERKS' '工厂' '4' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'MATNR' '物料号' '20' '' 'MATN1' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'MAKTX' '物料描述' '40' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'LIFNR' '供应商' '16' '' 'ALPHA' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'NAME1' '供应商名称' '16' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'KTMNG' '送检数量' '10' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'MEINS' '单位' '4' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'CHARG' '南都批次' '10' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'HSDAT' '生产日期' '10' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'ZSCPC' '生产批次' '10' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'ZJJFX' '紧急放行数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZRBJS' '让步接受数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZJS'   '拒收数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZFXFG' '返修返工数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZJJSY' '降级使用数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZOAPLBHG' '批量不合格单号' '13' '' '' '' '' '' '' '' '' '' '' '' '' '',
    fieldcat 'ZBHG'  '不合格合格数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZHG'   '合格数量' '13' '' '' '' '' '' '' '' '' '3' '' '' '' '',
    fieldcat 'ZSFHG' '是否合格' '13' 'X' '' '' '' '' '' '' '' '' '' '' 'ZSCANINSPECT' 'ZSFHG',
    fieldcat 'ZCLYJ' '处理意见' '13' 'X' '' '' '' '' '' '' '' '' '' '' 'ZSCANINSPECT' 'ZCLYJ',
    fieldcat 'ZREASON' '检验结果原因记录' '10' 'X' '' '' '' '' '' '' '' '' '' '' '' ''.

  i_grid_settings-edt_cll_cb = 'X'.

* 布局控制
  layout-cwidth_opt = 'X'.      "自动优化列宽
  layout-zebra      = 'X'.      "使ALV界面呈现颜色交替
*  layout-edit       = 'X'.     "整个ALV设置为可编辑
  layout-stylefname = 'F_STYLE'."设置单元格样式

**增加ALV事件(这里很重要,如果不添加,回车事件不会生效)
  ls_events-name = 'CALLER_EXIT'.
  ls_events-form = 'FRM_REGISTER_EVENTS'.
  APPEND ls_events TO lt_events.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      it_fieldcat_lvc          = fieldcat
      is_layout_lvc            = layout
      i_grid_settings          = i_grid_settings     "传参数到函数
      i_save                   = 'A'
      i_callback_user_command  = 'USER_COMMAND'
      i_callback_pf_status_set = 'PF_STATUS01'
      it_events                = lt_events
    TABLES
      t_outtab                 = gt_dtab
    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.

7.4 ALV字段设置

*3、ALV字段设置
FORM create_fieldcat  USING p0 TYPE lvc_t_fcat p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16.
  DATA: l_alv_filed  TYPE lvc_s_fcat.
  l_alv_filed-fieldname     =  p1.
  l_alv_filed-reptext       =  p2.
  l_alv_filed-outputlen     =  p3.
  l_alv_filed-edit          =  p4.
  l_alv_filed-convexit      =  p5.
  l_alv_filed-key           =  p6.
  l_alv_filed-do_sum        =  p7.
  l_alv_filed-checkbox      =  p8.
  l_alv_filed-no_zero       =  p9.
  l_alv_filed-hotspot       =  p10.
  l_alv_filed-qfieldname    =  p11.
  l_alv_filed-decimals      =  p12.
  l_alv_filed-inttype       =  p13.
  l_alv_filed-intlen        =  p14.
  l_alv_filed-ref_table     =  p15.
  l_alv_filed-ref_field     =  p16.
  APPEND l_alv_filed TO p0.
  CLEAR l_alv_filed.
ENDFORM.                    " CREATE_FIELDCAT

7.5 GUI状态设置

*4、GUI状态
FORM pf_status01 USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'PFSATUS01'.
  SET TITLEBAR 'TITLE03'.
ENDFORM.

8、最终效果

8.1 查询界面

ALV 回车事件_第3张图片

8.2 查询结果界面

ALV 回车事件_第4张图片
ALV 回车事件_第5张图片
ALV 回车事件_第6张图片
ALV 回车事件_第7张图片
ALV 回车事件_第8张图片

你可能感兴趣的:(ABAP,sap,abap)