REUSE_ALV_GRID_DISPLAY_LVC-EVENT事件使用

如上图,选择报废同一个抬头发票对应的任意一行,其他行的报废也自动勾选,使用event事件来实现,代码如下:

*&---------------------------------------------------------------------*

*& Report  ZCNFI_GT03
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report zcnfi_gt03.
tables:ztavim_comb.

types:
begin of tp_out,
  checkbox,
  field_style type lvc_t_styl" 为内表添加设置编辑状态所需的字段
  vbeln   type vbrp-vbeln"发票抬头
  vbelnc  type vbrp-vbeln,
  zreject type ztavim_comb-zreject"报废标记
  end of tp_out.

data:
      gt_comb type table of ztavim_comb,
      gt_out type table of tp_out.
"ALV
data:
  o_grid type ref to cl_gui_alv_grid,
  gt_events type slis_t_event,
  gs_events like line of gt_events,
  wa_layo type lvc_s_layo,
  wa_fcat type lvc_s_fcat,
  gt_fcat type lvc_t_fcat.
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_receiver definition"定义类 捕捉各种事件
  public section.
    methods handle_modify                   "数据改变
    for event data_changed of cl_gui_alv_grid
    importing er_data_changed.
endclass.                    "LCL_EVENT_RECEIVER DEFINITION
data gt_event_receiver type ref to lcl_event_receiver .
*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_receiver implementation"实现类 处理事件
* 界面修改事件
  method handle_modify .
    perform frm_data_changed using er_data_changed.

  endmethod.                    "HANDLE_MODIFY
endclass.                    "LCL_EVENT_RECEIVER IMPLEMENTATION


select-options:s_vbeln for ztavim_comb-vbeln.

start-of-selection.
  perform frm_data_edit.
  perform frm_data_out.
*&---------------------------------------------------------------------*
*&      Form  FRM_DATA_EDIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form frm_data_edit .
  data:
        lw_comb type ztavim_comb,
        lw_out type tp_out.
  select *
    from ztavim_comb
    into table gt_comb
    where vbeln in s_vbeln.
  select *
    from ztavim_comb
    appending table gt_comb
    where vbelnc in s_vbeln.

  sort gt_comb .
  delete adjacent duplicates from gt_comb comparing all fields.

  loop at gt_comb into lw_comb.
    "本条数据显示
    clear:lw_out.
    lw_out-vbeln lw_comb-vbeln.
    lw_out-vbelnc lw_comb-vbelnc.
    lw_out-zreject lw_comb-zreject.
    append lw_out to gt_out.
  endloop.

  sort:gt_out.
  delete adjacent duplicates from gt_out comparing all fields.
  sort:gt_out by vbeln vbelnc.
endform.                    " FRM_DATA_EDIT
*&---------------------------------------------------------------------*
*&      Form  FRM_DATA_OUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form frm_data_out .
  data:lw_out type tp_out.

  datal_grid_settings type lvc_s_glay  .

  "Set fieldcatalog
  clear:wa_fcat.
  wa_fcat-fieldname 'CHECKBOX'.
  wa_fcat-scrtext_l '复选框'.
  wa_fcat-checkbox 'X'.
  wa_fcat-edit 'X'.
  append wa_fcat to gt_fcat.

  clear:wa_fcat.
  wa_fcat-fieldname 'VBELN'.
  wa_fcat-scrtext_l '抬头发票'.
  append wa_fcat to gt_fcat.
  clear:wa_fcat.
  wa_fcat-fieldname 'VBELNC'.
  wa_fcat-scrtext_l '发票号码'.
  append wa_fcat to gt_fcat.

  clear:wa_fcat.
  wa_fcat-fieldname 'ZREJECT'.
  wa_fcat-scrtext_l '报废标记'.
  wa_fcat-checkbox 'X'.
  wa_fcat-edit 'X'.
  append wa_fcat to gt_fcat.
*  "已经报废的改为不可编辑状态
*  DATA stylelin TYPE lvc_s_styl.
*  LOOP AT gt_out INTO lw_out WHERE zreject = 'X'.
*    stylelin-fieldname = 'ZREJECT'. " 需要编辑的列名
*    stylelin-style = cl_gui_alv_grid=>mc_style_disabled. " 设置为不可编辑状态
*    APPEND stylelin TO lw_out-field_style.
*    CLEAR stylelin.
*    MODIFY gt_out FROM lw_out.
*  ENDLOOP.

  "Set layout
  wa_layo-cwidth_opt 'X'.
  wa_layo-stylefname 'FIELD_STYLE'" 将内表中的字段名存入显示格式
**************定义事件FORM名称*********************
  gs_events-name 'CALLER_EXIT'.
  gs_events-form 'CALLER_EXIT'.
  append gs_events to gt_events.

  l_grid_settings-edt_cll_cb 'X'  .
  call function 'REUSE_ALV_GRID_DISPLAY_LVC'
    exporting
      i_callback_program       sy-cprog
      i_callback_pf_status_set 'SET_STATUS'
      i_callback_user_command  'FRM_USER_COMMAND'
      i_grid_settings          l_grid_settings
      is_layout_lvc            wa_layo
      it_fieldcat_lvc          gt_fcat
      i_save                   'A'
      it_events                gt_events
    tables
      t_outtab                 gt_out
    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.                    " FRM_DATA_OUT
*&---------------------------------------------------------------------*
*&      Form  SET_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
form set_status using rt_extab type slis_t_extab.

  set pf-status '9000'.
  set titlebar 'TBAR'.

endform.                    " SET_STATUS
*&---------------------------------------------------------------------*
*&      Form  caller_exit
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->E_GRID     text
*----------------------------------------------------------------------*
form caller_exit using e_grid type slis_data_caller_exit.

  call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    importing
      e_grid o_grid.

  call method o_grid->register_edit_event     "注册GRID事件
    exporting
      i_event_id cl_gui_alv_grid=>mc_evt_enter"事件:回车
    exceptions
      error      1
      others     2.

  create object gt_event_receiver.
  set handler gt_event_receiver->handle_modify for o_grid.

endform.                    "CALLER_EXIT
*&---------------------------------------------------------------------*
*&      Form  FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
form frm_user_command using r_ucomm like sy-ucomm
                            rs_selfield type slis_selfield.
  data:lw_out type tp_out.

* ALV GridControl full screen.
  call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    importing
      e_grid o_grid.
* Verification of Changes.
  call method o_grid->check_changed_data.
  rs_selfield-refresh 'X'.     "自动刷新
  rs_selfield-col_stable 'X'.  "防止列跑位
  rs_selfield-row_stable 'X'.  "防止行跑位

  case r_ucomm.
    when 'SAVE'.  "
      perform frm_update_data.
    when 'BACK1' or 'BACK2' or 'BACK3'.
      leave to screen .
*    WHEN 'SAL'.  "取消全选
*      CLEAR:lw_out.
*      lw_out-checkbox = ''.
*      MODIFY gt_out FROM lw_out TRANSPORTING checkbox WHERE checkbox = 'X'.
*    WHEN 'ALL'.  "全选
*      CLEAR:lw_out.
*      lw_out-checkbox = 'X'.
*      MODIFY gt_out FROM lw_out TRANSPORTING checkbox WHERE checkbox = ''.
  endcase.
endform.                    "FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  FRM_UPDATE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form frm_update_data .
  data:
      lw_out type tp_out,
      lw_comb type ztavim_comb.
  loop at gt_out into lw_out where checkbox 'X'.
    lw_comb-zreject lw_out-zreject.
    modify gt_comb from lw_comb transporting zreject
     where vbeln lw_out-vbeln
     and vbelnc lw_out-vbelnc.
  endloop.

  "更新
  update ztavim_comb from table gt_comb.
  if sy-subrc 0.
    commit work.
    message '更新成功' type 'S'.
  else.
    rollback work.
  endif.
endform.                    " FRM_UPDATE_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_DATA_CHANGED
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ER_DATA_CHANGED  text
*----------------------------------------------------------------------*
form frm_data_changed  using    ir_data_changed
      type ref to cl_alv_changed_data_protocol.
  data ls_mod_cell type lvc_s_modi,
         lv_value    type lvc_value.

  data:
        lv_id type lvc_s_modi-row_id,
        lt_out type table of tp_out,
        lw_out type tp_out.

  sort ir_data_changed->mt_mod_cells by row_id .
  loop at ir_data_changed->mt_mod_cells into ls_mod_cell .
    if ls_mod_cell-fieldname 'ZREJECT'.
      read table gt_out into lw_out index ls_mod_cell-row_id.
      lw_out-zreject ls_mod_cell-value.
      lw_out-checkbox 'X'.
      modify gt_out from lw_out transporting zreject checkbox where vbeln lw_out-vbeln.
    endif.
  endloop .

  data stbl type lvc_s_stbl.
  stbl-row 'X'." 基于行的稳定刷新
  stbl-col 'X'." 基于列稳定刷新
  call method o_grid->refresh_table_display
    exporting
      is_stable stbl.
endform.                    " FRM_DATA_CHANGED

相关文章:

  • 2021-07-17
  • 2022-12-23
  • 2021-09-27
  • 2022-01-14
猜你喜欢
  • 2022-12-23
  • 2021-10-10
  • 2022-01-13
  • 2022-02-28
  • 2022-03-07
相关资源
相似解决方案