ABAP - OOALV DEMO 杂乱无用的笔记

CLASS lcl_cls DEFINITION.
  PUBLIC SECTION.
    METHODS handle_toolbar FOR EVENT toolbar
                  OF cl_gui_alv_grid
      IMPORTING e_object.

    METHODS 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.

CLASS lcl_cls IMPLEMENTATION.
  METHOD handle_toolbar.
    "向MT_TOOLBAR中增加行,增加按钮
    DATA ls_button TYPE stb_button.

    ls_button-butn_type = '3'.
    APPEND ls_button TO e_object->mt_toolbar.

    CLEAR:ls_button.
    ls_button-function = 'SELALL'.
    ls_button-text     = '全选'.
    ls_button-icon     = icon_checkbox.
    APPEND ls_button TO e_object->mt_toolbar.

    CLEAR:ls_button.
    ls_button-function = 'CANALL'.
    ls_button-text     = '取消全选'.
    ls_button-icon     = icon_dummy.
    APPEND ls_button TO e_object->mt_toolbar.

    IF p_rd1 = 'X'.
      CLEAR:ls_button.
      ls_button-function = 'ENSTOCK'.
      ls_button-text     = '确认为淘汰品'.
      ls_button-icon     = icon_status_ok.
      APPEND ls_button TO e_object->mt_toolbar.
    ELSEIF p_rd3 = 'X'.
      CLEAR:ls_button.
      ls_button-function = 'DATASE'.
      ls_button-text     = '数据保存'.
      ls_button-icon     = icon_allow.
      APPEND ls_button TO e_object->mt_toolbar.

      CLEAR:ls_button.
      ls_button-function = 'POSTEM'.
      ls_button-text     = '邮件推送'.
      ls_button-icon     = icon_envelope_closed.
      APPEND ls_button TO e_object->mt_toolbar.
    ELSEIF p_rd4 = 'X'.

    ENDIF.
  ENDMETHOD.

  METHOD handle_user_command.
    CASE e_ucomm.
      WHEN 'SELALL'.   "全选按钮
        LOOP AT  ASSIGNING .
          ASSIGN COMPONENT 'SEL' OF STRUCTURE  TO .
          IF sy-subrc = 0.
             = abap_true.
          ENDIF.
        ENDLOOP.

      WHEN 'CANALL'.   "取消全选按钮
        LOOP AT  ASSIGNING .
          ASSIGN COMPONENT 'SEL' OF STRUCTURE  TO .
          IF sy-subrc = 0.
             = abap_false.
          ENDIF.
        ENDLOOP.

      WHEN 'ENSTOCK'.  "更新到配置表按钮
        SORT gt_ttp BY matnr.
*       DELETE ADJACENT DUPLICATES FROM gt_ttp COMPARING matnr.
        LOOP AT gt_ttp ASSIGNING FIELD-SYMBOL().
          gs_wl-matnr = -matnr.
          gs_wl-w_date = sy-datum.
          gs_wl-w_time = sy-uzeit.
          gs_wl-w_user = sy-uname.
          APPEND gs_wl TO gt_wl.
          CLEAR gs_wl.
        ENDLOOP.
        MODIFY ztlbmm_cszx_wl FROM TABLE gt_wl.
        IF sy-subrc = 0.
          MESSAGE '确认淘汰品成功' TYPE 'S'.
        ELSE.
          MESSAGE '确认淘汰品失败' TYPE 'S' DISPLAY LIKE 'E'.
        ENDIF.

      WHEN 'DATASE'.  "数据保存按钮
        PERFORM frm_data_save.

      WHEN 'POSTEM'.  "邮件推送功能
        PERFORM frm_butn_email.
    ENDCASE.
    PERFORM frm_refresh_alv.
  ENDMETHOD.


  METHOD handle_data_changed.
    LOOP AT er_data_changed->mt_mod_cells INTO DATA(ls_cell).
      "1.读到修改的行
      READ TABLE  ASSIGNING FIELD-SYMBOL() INDEX ls_cell-row_id.
      IF sy-subrc = 0 .

        "2.根据字段修改对应的值
        CASE ls_cell-fieldname.
          WHEN 'SEL'.
            ASSIGN COMPONENT 'SEL' OF STRUCTURE  TO .
            IF sy-subrc = 0.
               = ls_cell-value.
            ENDIF.

          WHEN 'ZMENGE'.
            ASSIGN COMPONENT 'ZMENGE' OF STRUCTURE  TO .
            IF sy-subrc = 0.
               = ls_cell-value.
            ENDIF.

        ENDCASE.
      ENDIF.
      CLEAR: ls_cell.
    ENDLOOP.
    PERFORM frm_refresh_alv.
  ENDMETHOD.
ENDCLASS.

 FORM frm_set_handler.
  CREATE OBJECT event_handler.
  SET HANDLER event_handler->handle_toolbar       FOR gs_alv.
  SET HANDLER event_handler->handle_user_command  FOR gs_alv.
  SET HANDLER event_handler->handle_data_changed  FOR gs_alv.

** 将焦点设置到ALV上
  CALL METHOD cl_gui_control=>set_focus
    EXPORTING
      control = gs_alv.
** data_changed事件触发时点
  CALL METHOD gs_alv->register_edit_event
    EXPORTING
**      i_event_id = cl_gui_alv_grid=>mc_evt_enter  "回车时触发
      i_event_id = cl_gui_alv_grid=>mc_evt_modified "单元格修改时触发
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
ENDFORM.

你可能感兴趣的:(ABAP)