实现功能: 复选框\设置复选框是否可编辑\实现changed_finished事件.
一、ALV增加复选框:
1.1、在输出内表里增加一个SEL的字段:
sel TYPE c,
1.2、在build_fieldcat FORM里设置checkbox属性和edit属性,并输出SEL字段:
IF ls_fieldcat-fieldname = 'SEL'.
ls_fieldcat-checkbox = 'X'.
ENDIF.
1.3、刷新内表数据,捕获到用户选择的数据
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
CALL METHOD g_grid->check_changed_data.
二、控制ALV里的复选框是否可选择:
2.1、在输出内表里增加一个Style字段,控制复选框的样式:
stytle TYPE lvc_t_styl, "2.1
2.2、设置ALV的Layout:
gs_layout-stylefname = 'STYTLE'. "2.2
2.3、在输出内表里设置STYTLE样式,控制复选框是否可行:
LOOP AT gt_data ASSIGNING .
AT NEW ebeln.
l_new_flag = 'X'.
ENDAT.
IF l_new_flag = 'X'.
CLEAR -stytle.
ls_stytle-fieldname = 'SEL'.
ls_stytle-style = cl_gui_alv_grid=>mc_style_enabled.
INSERT ls_stytle INTO TABLE -stytle.
CLEAR: l_new_flag.
ELSE.
CLEAR -stytle.
ls_stytle-fieldname = 'SEL'.
ls_stytle-style = cl_gui_alv_grid=>mc_style_disabled.
INSERT ls_stytle INTO TABLE -stytle.
ENDIF.
ENDLOOP.
三、增加CHANGED_FINISHED 事件:
3.1、定义事件接收者:
*&**ALV 事件
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
DATA: gt_event TYPE slis_t_event WITH HEADER LINE.
CLASS: cl_event_receiver DEFINITION DEFERRED.
DATA: event_receiver TYPE REF TO cl_event_receiver .
3.2、定义与实现事件:
*&**========================================================================
*&* 事件
*&**========================================================================
CLASS cl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_top_of_page FOR EVENT top_of_page
OF cl_gui_alv_grid IMPORTING e_dyndoc_id,
*Hotspot click control
handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id es_row_no,
handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column es_row_no sender,
handle_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive sender,
handle_before_user_command
FOR EVENT before_user_command OF cl_gui_alv_grid
IMPORTING e_ucomm sender,
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm sender,
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed sender,
handle_data_changed_finished
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells sender.
PRIVATE SECTION.
* This flag is set if any error occured in one of the
* following methods:
DATA: error_in_data TYPE c.
ENDCLASS. "CL_event_receiver DEFINITION
**------实现事件---------------------------------------------------
CLASS cl_event_receiver IMPLEMENTATION.
METHOD handle_top_of_page.
* perform frm_top_of_page using e_dyndoc_id.
ENDMETHOD. "handle_top_of_page
METHOD handle_hotspot_click .
* perform frm_handle_hotspot_click using e_row_id e_column_id es_row_no.
ENDMETHOD . "handle_hotspot_click
METHOD handle_double_click.
* PERFORM frm_handle_double_click USING e_row e_column es_row_no sender.
ENDMETHOD. "HANDLE_DOUBLE_CLICK
METHOD handle_toolbar.
ENDMETHOD. "handle_toolbar
METHOD handle_before_user_command.
ENDMETHOD. "handle_before_user_command
METHOD handle_user_command.
ENDMETHOD. "handle_user_command
METHOD handle_data_changed.
ENDMETHOD. "handle_data_changed
METHOD handle_data_changed_finished.
DATA: ls_data LIKE ty_data.
DATA: ls_stable TYPE lvc_s_stbl.
DATA: ls_modi TYPE lvc_s_modi.
BREAK-POINT.
error_in_data = space.
READ TABLE et_good_cells INTO ls_modi INDEX 1.
IF ls_modi-fieldname = 'SEL'.
READ TABLE gt_data INTO ls_data INDEX ls_modi-row_id.
MODIFY gt_data FROM ls_data TRANSPORTING sel WHERE ebeln = ls_data-ebeln.
"刷新内表
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD g_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
ENDIF.
ENDMETHOD. "HANDLE_DATA_CHANGED_FINISHED
ENDCLASS. "CL_event_receiver IMPLEMENTATION
3.3、追加事件并设置事件参数:
"3.3
gt_event-name = 'CALLER_EXIT'. "CALLER_EXIT 不能改变
gt_event-form = 'FRM_CHANGED_FINISHED'. " FORM
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'ALV_GUI_STATUS'
i_callback_user_command = 'ALV_USER_COMMAND'
it_events = gt_event[]
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
3.4、注册事件:
FORM FRM_CHANGED_FINISHED USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
* 设置enter事件
CALL METHOD g_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
CALL METHOD g_grid->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_finished FOR g_grid.
ENDFORM. "FRM_CHANGED_FINISHED
程序完事代码(新建一个GUI_1000可直接运行):
*&---------------------------------------------------------------------*
*& Report ZFIRP004
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zfirp005_test.
TABLES: ekpo.
TYPE-POOLS:slis.
*&*ALV
DATA:gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo.
*&**ALV 事件
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
DATA: gt_event TYPE slis_t_event WITH HEADER LINE.
CLASS: cl_event_receiver DEFINITION DEFERRED.
DATA: event_receiver TYPE REF TO cl_event_receiver .
DATA: BEGIN OF ty_data,
ebeln LIKE ekpo-ebeln,
ebelp LIKE ekpo-ebelp,
loekz LIKE ekpo-loekz,
matnr LIKE ekpo-matnr,
bukrs LIKE ekpo-bukrs,
werks LIKE ekpo-werks,
lgort LIKE ekpo-lgort,
menge LIKE ekpo-menge,
meins LIKE ekpo-meins,
netwr LIKE ekpo-netwr,
sel TYPE c, "1.1
stytle TYPE lvc_t_styl, "2.1
message(100) TYPE c,
END OF ty_data,
gt_data LIKE TABLE OF ty_data.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: s_bukrs FOR ekpo-bukrs DEFAULT '8000',
s_matnr FOR ekpo-matnr,
s_werks FOR ekpo-werks.
PARAMETERS: p_mtart LIKE mara-mtart DEFAULT 'Z400'.
SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN OUTPUT.
AT SELECTION-SCREEN.
INITIALIZATION.
START-OF-SELECTION.
PERFORM frm_get_data.
END-OF-SELECTION.
PERFORM alv_display.
*&**========================================================================
*&* 事件
*&**========================================================================
CLASS cl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_top_of_page FOR EVENT top_of_page
OF cl_gui_alv_grid IMPORTING e_dyndoc_id,
*Hotspot click control
handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id es_row_no,
handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column es_row_no sender,
handle_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive sender,
handle_before_user_command
FOR EVENT before_user_command OF cl_gui_alv_grid
IMPORTING e_ucomm sender,
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm sender,
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed sender,
handle_data_changed_finished
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells sender.
PRIVATE SECTION.
* This flag is set if any error occured in one of the
* following methods:
DATA: error_in_data TYPE c.
ENDCLASS. "CL_event_receiver DEFINITION
**------实现事件---------------------------------------------------
CLASS cl_event_receiver IMPLEMENTATION.
METHOD handle_top_of_page.
* perform frm_top_of_page using e_dyndoc_id.
ENDMETHOD. "handle_top_of_page
METHOD handle_hotspot_click .
* perform frm_handle_hotspot_click using e_row_id e_column_id es_row_no.
ENDMETHOD . "handle_hotspot_click
METHOD handle_double_click.
* PERFORM frm_handle_double_click USING e_row e_column es_row_no sender.
ENDMETHOD. "HANDLE_DOUBLE_CLICK
METHOD handle_toolbar.
ENDMETHOD. "handle_toolbar
METHOD handle_before_user_command.
ENDMETHOD. "handle_before_user_command
METHOD handle_user_command.
ENDMETHOD. "handle_user_command
METHOD handle_data_changed.
ENDMETHOD. "handle_data_changed
METHOD handle_data_changed_finished.
DATA: ls_data LIKE ty_data.
DATA: ls_stable TYPE lvc_s_stbl.
DATA: ls_modi TYPE lvc_s_modi.
BREAK-POINT.
error_in_data = space.
READ TABLE et_good_cells INTO ls_modi INDEX 1.
IF ls_modi-fieldname = 'SEL'.
READ TABLE gt_data INTO ls_data INDEX ls_modi-row_id.
MODIFY gt_data FROM ls_data TRANSPORTING sel WHERE ebeln = ls_data-ebeln.
"刷新内表
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD g_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
ENDIF.
ENDMETHOD. "HANDLE_DATA_CHANGED_FINISHED
ENDCLASS. "CL_event_receiver IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* 获取 主数据
*----------------------------------------------------------------------*
FORM frm_get_data .
FIELD-SYMBOLS <fs> LIKE ty_data.
DATA: l_new_flag TYPE c,
ls_stytle TYPE lvc_s_styl.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM ekpo
WHERE bukrs IN s_bukrs
AND matnr IN s_matnr
AND werks IN s_werks.
SORT gt_data BY ebeln ebelp .
"2.3
LOOP AT gt_data ASSIGNING <fs>.
AT NEW ebeln.
l_new_flag = 'X'.
ENDAT.
IF l_new_flag = 'X'.
CLEAR <fs>-stytle.
ls_stytle-fieldname = 'SEL'.
ls_stytle-style = cl_gui_alv_grid=>mc_style_enabled.
INSERT ls_stytle INTO TABLE <fs>-stytle.
CLEAR: l_new_flag.
ELSE.
CLEAR <fs>-stytle.
ls_stytle-fieldname = 'SEL'.
ls_stytle-style = cl_gui_alv_grid=>mc_style_disabled.
INSERT ls_stytle INTO TABLE <fs>-stytle.
ENDIF.
ENDLOOP.
ENDFORM.
FORM alv_display .
PERFORM alv_build_fieldcat .
gs_layout-cwidth_opt = abap_true. "优化列宽设置
gs_layout-no_toolbar = 'X'.
gs_layout-stylefname = 'STYTLE'. "2.2
"3.3
gt_event-name = 'CALLER_EXIT'. "CALLER_EXIT 不能改变
gt_event-form = 'FRM_CHANGED_FINISHED'.
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'ALV_GUI_STATUS'
i_callback_user_command = 'ALV_USER_COMMAND'
it_events = gt_event[]
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH '系统错误' space space space.
ENDIF.
ENDFORM.
FORM alv_gui_status USING fu_extab TYPE slis_t_extab.
SET PF-STATUS 'GUI_1000'.
ENDFORM.
FORM alv_user_command USING fu_ucomm LIKE sy-ucomm
fu_selfield TYPE slis_selfield.
DATA:
ls_stable LIKE lvc_s_stbl.
* 1.3 刷新內表
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
CALL METHOD g_grid->check_changed_data.
CASE fu_ucomm.
WHEN 'POST'.
CLEAR:fu_ucomm.
WHEN 'CHECK'.
CLEAR:fu_ucomm.
ENDCASE.
*&**重新设置ALV显示布局
CALL METHOD g_grid->set_frontend_layout
EXPORTING
is_layout = gs_layout.
*&**刷新显示数据 row col 为X刷新后保持位置不变
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD g_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
fu_selfield-refresh = 'X'.
fu_selfield-row_stable = 'X'.
fu_selfield-col_stable = 'X'.
ENDFORM.
FORM alv_build_fieldcat .
DATA: ls_fieldcat TYPE lvc_s_fcat.
DEFINE set_fieldcatalog.
CLEAR ls_fieldcat.
ls_fieldcat-col_pos = &1. "列位置
ls_fieldcat-edit = &2. "可编辑
ls_fieldcat-just = &3. "对齐方式
ls_fieldcat-ref_table = &4. "引用表
ls_fieldcat-ref_field = &5. "引用字段
ls_fieldcat-fieldname = &6. "要显示的字段
ls_fieldcat-coltext = &7. "字段名
IF ls_fieldcat-fieldname = 'SEL'. "1.2
ls_fieldcat-checkbox = 'X'.
ENDIF.
APPEND ls_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.
set_fieldcatalog:
1 'X ' 'L' ' ' ' ' 'SEL' '选择',
1 ' ' 'L' ' ' ' ' 'EBELN' '采购订单',
1 ' ' 'L' ' ' ' ' 'EBELP' '采购订单项目',
1 ' ' 'L' ' ' ' ' 'BUKRS' '公司代码',
1 ' ' 'L' ' ' ' ' 'WERKS' '工厂',
1 ' ' 'L' ' ' ' ' 'LGORT' '库位',
1 ' ' 'L' ' ' ' ' 'MATNR' '物料编码',
1 ' ' 'L' ' ' ' ' 'LOEKZ' '删除标识',
1 ' ' 'L' ' ' ' ' 'MENGE' '数量',
1 ' ' 'L' ' ' ' ' 'MEINS' '单位',
1 ' ' 'L' ' ' ' ' 'NETWR' '净值'.
ENDFORM.
FORM FRM_CHANGED_FINISHED USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
* 设置enter事件
CALL METHOD g_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
CALL METHOD g_grid->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_finished FOR g_grid.
ENDFORM. "FRM_CHANGED_FINISHED