0


SAP ABAP ALV回车事件和新增删除行

如何在alv画面新增和删除一行或多行数据

发布日期:2024/07/27

案例:ALV维护表数据,包含工厂,物料,物料描述。可以新增一行,工厂物料可编辑,回车后自动带出物料描述,可以选中删除行项目。

1.主要逻辑为,点击新增行按钮后,向ALV输出表中添加一条数据。刷新屏幕。数据物料编码后,通过注册的回车事件添加获取物料描述逻辑。

完整代码如下:

REPORT zewm_rpt_244.

TABLES: sscrfields,ztewm_psjh_scom.

TYPES:
  BEGIN OF ts_upload,
    werks TYPE ztewm_psjh_scom-werks,
    matnr TYPE ztewm_psjh_scom-matnr,
    maktx TYPE ztewm_psjh_scom-maktx,
  END OF ts_upload,
  BEGIN OF ts_output,
    box         TYPE char1,
    field_style TYPE lvc_t_styl.
    INCLUDE TYPE ztewm_psjh_scom.
TYPES: END OF ts_output.
TYPES:tt_upload TYPE STANDARD TABLE OF ts_upload,
      tt_output TYPE STANDARD TABLE OF ts_output.

DATA:
  gt_upload   TYPE tt_upload,
  gt_output   TYPE tt_output,
  gt_events   TYPE slis_t_event,
  gt_fieldcat TYPE lvc_t_fcat,
  g_grid      TYPE REF TO cl_gui_alv_grid.

DATA:gs_layout TYPE lvc_s_layo.
DATA: stbl   TYPE lvc_s_stbl.

DATA: le_row     TYPE i,
      le_value   TYPE c,
      le_col     TYPE i,
      les_row_id TYPE lvc_s_row,
      les_col_id TYPE lvc_s_col,
      les_row_no TYPE lvc_s_roid.

CLASS lcl_event_handler DEFINITION."类定义
  PUBLIC SECTION.
    METHODS:
      handle_data_changed FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING e_modified et_good_cells.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION."类实现
  METHOD handle_data_changed.
* 获取当前单元格的信息
    CALL METHOD g_grid->get_current_cell
      IMPORTING
        e_row     = le_row
        e_value   = le_value
        e_col     = le_col
        es_row_id = les_row_id
        es_col_id = les_col_id
        es_row_no = les_row_no.
*读取当前行数据
    READ TABLE gt_output INTO DATA(gs_output) INDEX les_row_id-index.

    IF sy-subrc = 0.
*获取物料描述
      SELECT SINGLE maktx INTO @gs_output-maktx FROM makt WHERE matnr = @gs_output-matnr AND spras = @sy-langu.
      MODIFY gt_output FROM gs_output  INDEX les_row_id-index.

      stbl-row = 'X'." 基于行的稳定刷新
      stbl-col = 'X'." 基于列稳定刷新

      CALL METHOD g_grid->refresh_table_display
        EXPORTING
          is_stable = stbl.
    ENDIF.
  ENDMETHOD.
ENDCLASS. "lcl_event_handler IMPLEMENTATION

START-OF-SELECTION.
  PERFORM frm_get_data.

END-OF-SELECTION.
  PERFORM frm_display_alv.

*数据获取
FORM frm_get_data.
  SELECT * FROM ztewm_psjh_scom INTO CORRESPONDING FIELDS OF TABLE @gt_output.
ENDFORM.
*ALV展示
FORM frm_display_alv .
  PERFORM frm_build_layout.
  PERFORM frm_build_fieldcat.
  PERFORM frm_set_event.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-cprog
      i_callback_pf_status_set = 'FRM_STATUS_SET'
      i_callback_user_command  = 'FRM_USER_COMMAND'
      i_bypassing_buffer       = abap_true
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      it_events                = gt_events
    TABLES
      t_outtab                 = gt_output
    EXCEPTIONS ##FM_SUBRC_OK
      program_error            = 1
      OTHERS                   = 2.
ENDFORM.

FORM frm_status_set USING it_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZSTANDARD1'.
ENDFORM.

FORM frm_build_layout.

  CLEAR gs_layout.
  gs_layout-sel_mode  = 'A'.
  gs_layout-zebra = 'X'.
  gs_layout-cwidth_opt = 'X'.
  gs_layout-stylefname = 'FIELD_STYLE'.

ENDFORM.                    " FRM_BUILD_LAYOUT

FORM frm_build_fieldcat.
  CLEAR:gt_fieldcat.
  gt_fieldcat = VALUE #( BASE gt_fieldcat ( fieldname = 'WERKS' coltext = '工厂' ) ).
  gt_fieldcat = VALUE #( BASE gt_fieldcat ( fieldname = 'MATNR' coltext = '物料编号' ref_table = 'MARA' ref_field = 'MATNR' ) ).
  gt_fieldcat = VALUE #( BASE gt_fieldcat ( fieldname = 'MAKTX' coltext = '物料描述'  ) ).

ENDFORM.
*设置事件内表,用于关联LVC
FORM frm_set_event.
  DATA: ls_events TYPE slis_alv_event.
  ls_events-name = 'CALLER_EXIT' .
  ls_events-form =  'FRM_REGISTER_EVENTS'.
  APPEND ls_events TO gt_events.

ENDFORM.
*注册回车事件
FORM frm_register_events USING e_grid TYPE slis_data_caller_exit.
  DATA: gr_event_handler TYPE REF TO lcl_event_handler.
  CREATE OBJECT gr_event_handler.

  IF g_grid IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = g_grid.
  ENDIF.

  CALL METHOD g_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 g_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

FORM frm_user_command USING pv_ucomm LIKE sy-ucomm
                            ps_selfield TYPE slis_selfield.

  DATA: ls_stbl   TYPE lvc_s_stbl,
        ls_output TYPE ts_output.

  DATA(lv_refresh) = abap_on.

*获取当前alv
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = g_grid.

*检查更新
  CALL METHOD g_grid->check_changed_data.

  CALL METHOD g_grid->set_ready_for_input
    EXPORTING
      i_ready_for_input = 1.

*稳定刷新,保持滚动条位置
  ls_stbl-row = ls_stbl-col = 'X'.

  CASE pv_ucomm.
    WHEN '&ZNEW'.
      PERFORM frm_new_line.
    WHEN '&ZDEL'.
      READ TABLE gt_output TRANSPORTING NO FIELDS WITH KEY box = 'X'.
      IF sy-subrc <> 0.
        MESSAGE '请至少选择一条数据进行操作' TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.
      PERFORM frm_del_data.
    WHEN '&DATA_SAVE'."保存
      READ TABLE gt_output TRANSPORTING NO FIELDS WITH KEY box = 'X'.
      IF sy-subrc <> 0.
        MESSAGE '请至少选择一条数据进行操作' TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.
      PERFORM frm_save_data.
  ENDCASE.
  IF lv_refresh = abap_on.
    gs_layout-no_toolbar   = 'X'.
    CALL METHOD g_grid->set_frontend_layout
      EXPORTING
        is_layout = gs_layout.
    CALL METHOD g_grid->refresh_table_display
      EXPORTING
        is_stable = ls_stbl.
  ENDIF.
ENDFORM.
*新增行
FORM frm_new_line.
  DATA:ls_stylelin TYPE lvc_s_styl.
  DATA:lt_lvc_t_styl TYPE lvc_t_styl.
  INSERT INITIAL LINE INTO gt_output INDEX 1.
  IF sy-subrc = 0.
    READ TABLE gt_output ASSIGNING FIELD-SYMBOL(<lfs_output>) INDEX 1.
    IF sy-subrc = 0.
      <lfs_output>-box = 'X'.
*打开可编辑模式
      PERFORM frm_open_edit CHANGING <lfs_output>-field_style.
    ENDIF.
  ENDIF.

ENDFORM.
*数据保存
FORM frm_save_data.
  DATA:ls_output    TYPE ts_output,
       lt_output    TYPE STANDARD TABLE OF ts_output,
       lt_psjh_scom TYPE STANDARD TABLE OF ztewm_psjh_scom.

  lt_output = gt_output.
  SORT lt_output BY matnr werks.
  DELETE ADJACENT DUPLICATES FROM lt_output COMPARING matnr werks.
  DESCRIBE TABLE lt_output LINES DATA(lv_cont).
  DESCRIBE TABLE gt_output LINES DATA(lv_cont1).
  IF lv_cont <> lv_cont1.
    MESSAGE '存在工厂,物料重复的数据,请检查!' TYPE 'I'.
    RETURN.
  ENDIF.

  DELETE lt_output WHERE box <> 'X'.
  CHECK lt_output IS NOT INITIAL.
  MOVE-CORRESPONDING lt_output TO lt_psjh_scom.
  MODIFY ztewm_psjh_scom FROM TABLE lt_psjh_scom.
  IF sy-subrc = 0.
    COMMIT WORK.
  ELSE.
    ROLLBACK WORK.
  ENDIF.

ENDFORM.

*删除行
FORM frm_del_data.
  DATA:ls_output    TYPE ts_output,
       lt_psjh_scom TYPE STANDARD TABLE OF ztewm_psjh_scom.
  DATA(lt_output) = gt_output.
  DELETE lt_output WHERE box <> 'X'.
  MOVE-CORRESPONDING lt_output TO lt_psjh_scom .

  DELETE ztewm_psjh_scom FROM TABLE lt_psjh_scom.
  IF sy-subrc = 0.
    COMMIT WORK.
  ELSE.
    ROLLBACK WORK.
  ENDIF.
ENDFORM.
*打开修改模式
FORM frm_open_edit CHANGING ct_field_style TYPE lvc_t_styl.

  DATA: ls_stylerow TYPE lvc_s_styl.
  CLEAR:ct_field_style.
  CLEAR:ls_stylerow.
  ls_stylerow-fieldname = 'WERKS'.
  ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled.
  INSERT ls_stylerow INTO TABLE ct_field_style.

  CLEAR:ls_stylerow.
  ls_stylerow-fieldname = 'MATNR'.
  ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled.
  INSERT ls_stylerow INTO TABLE ct_field_style.
ENDFORM.
*关闭修改模式
FORM frm_close_edit CHANGING ct_field_style TYPE lvc_t_styl.

  DATA: ls_stylerow TYPE lvc_s_styl.
  CLEAR:ct_field_style.
  CLEAR:ls_stylerow.
  ls_stylerow-fieldname = 'WERKS'.
  ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.
  INSERT ls_stylerow INTO TABLE ct_field_style.

  CLEAR:ls_stylerow.
  ls_stylerow-fieldname = 'MATNR'.
  ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.
  INSERT ls_stylerow INTO TABLE ct_field_style.
ENDFORM.
  1. 状态栏添加新增行,删除行按钮。在这里插入图片描述
  2. 最后来看一下效果吧 执行后点击新增行在这里插入图片描述 向新增行输入信息后回车。在这里插入图片描述 回车带出物料描述并保存。在这里插入图片描述
标签: 前端

本文转载自: https://blog.csdn.net/Wuuuf/article/details/140734608
版权归原作者 甜糕不甜 所有, 如有侵权,请联系我们删除。

“SAP ABAP ALV回车事件和新增删除行”的评论:

还没有评论