ALV Data_Changed事件应用一例

在alv中若需要用户交互输入数据,则数据的输入后可能需要做检查或者其他联动设备,则需要alv中进行回车操作,或者在被修改的单元格失去焦点的时候,能够触发事件,以便程序可以对变化的结果信息做详细的分析,或者做处理。

如果需要达到这个效果,则需要为alv绑定data_changed事件。

绑定事件的方法是,

首先创建一个事件类,
在类中定义一个专门用户捕获和处理alv  的 data_changed事件的方法,并编写相应的代码。

在事件回调代码中进行绑定,绑定时需要alv对象,如果当前的alv不是oo alv,则需要先获得alv对象。

绑定之后,该alv就可以相应data_changed事件
提高了交货的效果。

REPORT z_test_t05.
TYPE-POOLS:slis.
TABLES:
zfico210_lg_fc,
zfico210_dept.

TYPES: BEGIN OF ty_fc.
        INCLUDE STRUCTURE zfico210_lg_fc.
TYPES: box,
END OF ty_fc.

DATA: gt_fc TYPE STANDARD TABLE OF ty_fc WITH HEADER LINE ,

gt_dept_code TYPE STANDARD TABLE OF zfico210_dept,
gs_dept_code LIKE LINE OF gt_dept_code .

DATA:
      gs_fc LIKE LINE OF gt_fc,
      ws_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      ws_fieldgroups_tab TYPE slis_t_sp_group_alv,
      ws_layout TYPE slis_layout_alv,
      ws_sortfields_tab TYPE slis_t_sortinfo_alv WITH HEADER LINE,
      ws_events TYPE slis_t_event WITH HEADER LINE,
      v_repid LIKE sy-repid,
      nn TYPE i VALUE 0.

DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.


*----------------------------------------------------------------------*
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
*......................................................................*
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
    PERFORM data_changed USING er_data_changed.

  ENDMETHOD. "handle_data_changed
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
DATA: event_receiver TYPE REF TO lcl_event_receiver.


SELECTION-SCREEN: BEGIN OF BLOCK b3 WITH FRAME TITLE text-002.
SELECT-OPTIONS: p_dept2 FOR zfico210_lg_fc-dept_code MATCHCODE OBJECT zfico210_dept_sh MODIF ID s2.

SELECTION-SCREEN: END OF BLOCK b3.




INITIALIZATION.

  PERFORM get_public_info.


START-OF-SELECTION.


  PERFORM alv_events.
  PERFORM pfm_sel_data.
  PERFORM pfm_div_data.

*&---------------------------------------------------------------------*
*& Form PFM_SEL_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM pfm_sel_data .
  CLEAR: gt_fc,gs_fc.
  SELECT * FROM zfico210_lg_fc INTO CORRESPONDING FIELDS OF TABLE gt_fc
    WHERE dept_code IN p_dept2 .

ENDFORM. " PFM_SEL_DATA
*&---------------------------------------------------------------------*
*& Form pfm_div_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM pfm_div_data .

  DEFINE hout.
    nn = nn + 1.
    ws_fieldcat-fieldname = '&1'.
    ws_fieldcat-seltext_m = &2.
    ws_fieldcat-col_pos = nn.
    ws_fieldcat-outputlen = &3.
    ws_fieldcat-datatype = '&4'.
    ws_fieldcat-edit = &5.

    if ws_fieldcat-fieldname = 'DEPT_CODE' .
      ws_fieldcat-ref_fieldname = 'DEPT_CODE' .
      ws_fieldcat-ref_tabname = 'ZFICO210_DEPT'.
    endif.
    append ws_fieldcat.
    clear ws_fieldcat.
  END-OF-DEFINITION.

  hout dept_code '部门编号' 15 char 'X'.
  hout dept_name '部门描述' 15 char ''.
  hout lgort '库存地点' 15 char ''.
* hout dept_code2 '单位编号(业务部)' 15 char ''.
  hout create_user '部门录入人' 10 char ''.
  hout createdate '部门录入日期' 12 date ''.
  hout submit_flag '是否提交' 10 char ''.
  hout check_flag '部门审核' 10 char ''.
  hout check_user '部门审核人' 10 char ''.
  hout check_date '部门审核日期' 15 date ''.
  hout confirm_user '确认人' 10 char ''.
  hout confirm_date '确认日期' 10 date ''.
  hout confirm_flag '确认标识' 10 char ''.
  hout aenam '最后修改人' 15 char '' .
  hout aedat '最后修改时间' 15 dats '' .

  ws_layout-zebra = 'X'.
  ws_layout-box_fieldname = 'BOX'.
  v_repid = sy-repid.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = v_repid
      is_layout = ws_layout
      it_fieldcat = ws_fieldcat[]
      i_default = 'X'
      i_save = 'A'
      i_callback_pf_status_set = 'MAIN_STATUS'
      i_callback_user_command = 'USER_CLICKED'
      it_special_groups = ws_fieldgroups_tab[]
      it_sort = ws_sortfields_tab[]
      it_events = ws_events[]
    TABLES
      t_outtab = gt_fc
    EXCEPTIONS
      program_error = 1
      OTHERS = 2.

ENDFORM. " pfm_div_data
*&---------------------------------------------------------------------*
*& Form main_status
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM main_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD'.
ENDFORM. "MAIN_STATUS
*&---------------------------------------------------------------------*
*& Form user_clicked
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->UCOMM text
* -->SELFIELD text
*----------------------------------------------------------------------*
FORM user_clicked USING ucomm LIKE sy-ucomm
                        selfield TYPE slis_selfield.
  DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.
  CALL METHOD lr_grid->check_changed_data.
  selfield-refresh = 'X'.
  CHECK sy-subrc = 0.
  CASE ucomm.
    WHEN 'AGW'.
      LEAVE TO SCREEN 0.
    WHEN 'AWG'.
      LEAVE PROGRAM.

    WHEN '&TIJIAO'.
      DATA: lc_ok VALUE 'X'.

      READ TABLE gt_fc INTO gs_fc WITH KEY box = 'X'.

      IF sy-subrc = 0.
        LOOP AT gt_fc INTO gs_fc WHERE box = 'X'.
*
          CLEAR gs_dept_code.
          READ TABLE gt_dept_code INTO gs_dept_code
                 WITH KEY dept_code = gs_fc-dept_code BINARY SEARCH.
          IF sy-subrc <> 0.
            CLEAR lc_ok .
            EXIT .
            MESSAGE '事务不规范!' TYPE 'I'.
          ENDIF.

        ENDLOOP.

        IF lc_ok = 'X'.
          LOOP AT gt_fc INTO gs_fc WHERE box = 'X'.


            gs_fc-aenam = sy-uname.
            gs_fc-aedat = sy-datum.

            UPDATE zfico210_lg_fc SET dept_code = gs_fc-dept_code
                                         aenam = gs_fc-aenam
                                         aedat = gs_fc-aedat
            WHERE lgort = gs_fc-lgort.

          ENDLOOP.

          IF sy-subrc = 0.


            COMMIT WORK.
            IF sy-subrc = 0.
              MESSAGE '事务已提交!' TYPE 'S'.

            ENDIF.
          ELSE.
            ROLLBACK WORK.
            MESSAGE '保存失败,错误未知!' TYPE 'I'.
          ENDIF.
        ELSE.
          MESSAGE '保存失败,部门不规范!' TYPE 'I'.

        ENDIF.

      ELSE.

        MESSAGE '未选中信息,请选择更改的条目!' TYPE 'I'.
      ENDIF.
  ENDCASE.

ENDFORM. "user_clicked



*&---------------------------------------------------------------------*
*& Form GET_PUBLIC_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_public_info .
  SELECT * FROM zfico210_dept INTO TABLE gt_dept_code .
  SORT gt_dept_code ASCENDING BY dept_code.
ENDFORM. " GET_PUBLIC_INFO



*&---------------------------------------------------------------------*
*& Form alv_envents
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM alv_events .
  ws_events-name = 'CALLER_EXIT'.
  ws_events-form = 'CALLER_EXIT'.
  APPEND ws_events .
ENDFORM. " alv_envents



*&---------------------------------------------------------------------*
*& Form alv_envents
*&---------------------------------------------------------------------*
*设置下拉列表,使Grid和内表能链接上
*---------------------------------------------------------------------*
FORM caller_exit USING ls_data TYPE slis_data_caller_exit.

* BREAK-POINT.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = l_ref_alv.

*
* CALL METHOD l_ref_alv->set_drop_down_table
* EXPORTING
* it_drop_down = g_flag.
*
* CALL METHOD l_ref_alv->set_ready_for_input
* EXPORTING
* i_ready_for_input = 1.

  CALL METHOD l_ref_alv->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_modified.

  CREATE OBJECT event_receiver.
  SET HANDLER event_receiver->handle_data_changed FOR l_ref_alv.

ENDFORM. "CALLER_EXIT




************************************************************************************
*****************捕捉alv改变值后处理的perform data_changed**************************
************************************************************************************
FORM data_changed USING p_er_data_changed TYPE REF TO cl_alv_changed_data_protocol.

  IF p_er_data_changed->mt_mod_cells IS NOT INITIAL.


    FIELD-SYMBOLS: <f_data> LIKE LINE OF gt_fc.
    DATA : ls_modi TYPE lvc_s_modi .
    DATA: ls_mod_cell TYPE lvc_s_modi.

* DATA: lt_cell TYPE lvc_t_cell.
* CALL METHOD l_ref_alv->get_selected_cells
* IMPORTING
* et_cell = lt_cell.
*

    DATA: li_row TYPE i,
     lc_value TYPE c,
     li_col TYPE i,
     ls_row_id TYPE lvc_s_row,
     ls_col_id TYPE lvc_s_col,
     ls_row_no TYPE lvc_s_roid.



    CALL METHOD l_ref_alv->get_current_cell
      IMPORTING
        e_row = li_row
        e_value = lc_value
        e_col = li_col
        es_row_id = ls_row_id
        es_col_id = ls_col_id
        es_row_no = ls_row_no.


    LOOP AT p_er_data_changed->mt_mod_cells INTO ls_mod_cell.
      CASE ls_mod_cell-fieldname .
        WHEN 'DEPT_CODE'. " 根据原因分析类型的变化提取对应的描述
          READ TABLE gt_fc ASSIGNING <f_data> INDEX ls_mod_cell-row_id .
          IF sy-subrc = 0.

            IF ls_mod_cell-value EQ ''.
              <f_data>-dept_name = ''.
            ELSE.
              READ TABLE gt_dept_code INTO gs_dept_code WITH KEY dept_code = ls_mod_cell-value.
              IF sy-subrc = 0.
                <f_data>-dept_name = gs_dept_code-dept_name.
              ENDIF.
            ENDIF.
          ENDIF.
      ENDCASE.
    ENDLOOP.

    CALL METHOD l_ref_alv->refresh_table_display
      EXPORTING
        i_soft_refresh = 'X'.

* CALL METHOD l_ref_alv->set_selected_cells
* EXPORTING
* it_cells = lt_cell.

    CALL METHOD l_ref_alv->set_current_cell_via_id
      EXPORTING
        is_row_id = ls_row_id
        is_column_id = ls_col_id
        is_row_no = ls_row_no.

  ENDIF.
ENDFORM. " data_changed
发布了334 篇原创文章 · 获赞 16 · 访问量 98万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 撸撸猫 设计师: 设计师小姐姐

分享到微信朋友圈

×

扫一扫,手机浏览