以前一直比较反感用ALV做编辑,因为实在是太慢了。但是用TableControl做这种类型的开发的话,缺点也很多,比如:
1、代码量大
2、需要画屏幕,调屏幕
3、有需求变更时,变更调整的工作量也很大
4、不像ALV一样可以方便的复制、粘贴、导出、筛选等等
最近自己刚做完的项目上有这样的需求,是用OO-ALV来做的,而且最近有几个群友也在群里有类似的需求,所以今天就把这个基础的程序结构分享出来吧。
效果图
代码下载地址
链接: https://pan.baidu.com/s/1Nv1l1TsLL06wpR0Wd_fEXA
提取码: xye7
详细代码
REPORT z_alv_header_item_test.
TYPES:BEGIN OF ty_header,
box TYPE c,
key TYPE char10,
END OF ty_header,
BEGIN OF ty_item,
box TYPE c,
key TYPE char10,
itemno TYPE char5,
END OF ty_item.
*----------------------------------------------------------------------*
* CLASS lcl_alv_event_handler DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_alv_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
handle_data_changed_finished FOR EVENT data_changed_finished
OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells.
ENDCLASS. "lcl_alv_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_alv_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_alv_event_handler IMPLEMENTATION .
METHOD handle_data_changed_finished.
PERFORM handle_data_changed_finished.
ENDMETHOD. "HANDLE_USER_COMMAND
ENDCLASS. "lcl_alv_event_handler IMPLEMENTATION
DATA:
gt_fieldcat_header TYPE lvc_t_fcat,
gt_fieldcat_item TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
gs_layout TYPE lvc_s_layo.
DATA: go_docking_con TYPE REF TO cl_gui_docking_container,
go_splitter_con TYPE REF TO cl_gui_splitter_container,
go_container_1 TYPE REF TO cl_gui_container,
go_container_2 TYPE REF TO cl_gui_container,
go_alv_header TYPE REF TO cl_gui_alv_grid,
go_alv_item TYPE REF TO cl_gui_alv_grid,
go_event_header TYPE REF TO lcl_alv_event_handler.
DATA: gt_header TYPE TABLE OF ty_header WITH HEADER LINE,
gt_item TYPE TABLE OF ty_item WITH HEADER LINE,
gt_item_filter TYPE lvc_t_filt WITH HEADER LINE.
DEFINE d_build_fieldcat.
gs_fieldcat-fieldname = &2.
gs_fieldcat-checkbox = &3.
gs_fieldcat-edit = &4.
gs_fieldcat-no_out = &5.
gs_fieldcat-coltext = &6.
append gs_fieldcat to &1.
clear gs_fieldcat.
END-OF-DEFINITION.
PARAMETERS:p_test TYPE c.
INITIALIZATION.
gt_header-key = '1000000001'. APPEND gt_header.
gt_header-key = '1000000002'. APPEND gt_header.
gt_item-key = '1000000001'. gt_item-itemno = '10001'. APPEND gt_item.
gt_item-key = '1000000002'. gt_item-itemno = '10001'. APPEND gt_item.
gt_item-key = '1000000002'. gt_item-itemno = '10002'. APPEND gt_item.
AT SELECTION-SCREEN OUTPUT.
CHECK go_docking_con IS INITIAL.
"创建容器
CREATE OBJECT go_docking_con
EXPORTING
ratio = 95
side = cl_gui_docking_container=>dock_at_bottom.
"拆分为上、下
CREATE OBJECT go_splitter_con
EXPORTING
parent = go_docking_con
rows = 2
columns = 1.
go_splitter_con->set_row_height( EXPORTING id = 1 height = 50 ).
"创建Header的ALV
go_container_1 = go_splitter_con->get_container( row = 1 column = 1 ).
CREATE OBJECT go_alv_header
EXPORTING
i_parent = go_container_1.
d_build_fieldcat:
gt_fieldcat_header 'BOX' 'X' 'X' '' '选择',
gt_fieldcat_header 'KEY' ' ' ' ' '' 'KEY'.
CALL METHOD go_alv_header->set_table_for_first_display
EXPORTING
is_layout = gs_layout
CHANGING
it_outtab = gt_header[]
it_fieldcatalog = gt_fieldcat_header.
"创建Item的ALV
go_container_2 = go_splitter_con->get_container( row = 2 column = 1 ).
CREATE OBJECT go_alv_item
EXPORTING
i_parent = go_container_2.
d_build_fieldcat:
gt_fieldcat_item 'BOX' ' ' ' ' 'X' '选择',
gt_fieldcat_item 'KEY' ' ' ' ' '' 'KEY',
gt_fieldcat_item 'ITEMNO' ' ' ' ' '' 'ITEM NO.'.
gt_item_filter-fieldname = 'BOX'.
gt_item_filter-sign = 'I'.
gt_item_filter-option = 'EQ'.
gt_item_filter-low = 'X'.
APPEND gt_item_filter.
CALL METHOD go_alv_item->set_table_for_first_display
EXPORTING
is_layout = gs_layout
CHANGING
it_outtab = gt_item[]
it_filter = gt_item_filter[]
it_fieldcatalog = gt_fieldcat_item.
"事件
CREATE OBJECT go_event_header.
SET HANDLER go_event_header->handle_data_changed_finished FOR go_alv_header.
go_alv_header->register_edit_event( EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified ).
*&---------------------------------------------------------------------*
*& Form handle_data_changed_finished
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM handle_data_changed_finished.
LOOP AT gt_header.
gt_item-box = gt_header-box.
MODIFY gt_item TRANSPORTING box WHERE key = gt_header-key.
ENDLOOP.
go_alv_item->refresh_table_display( ).
ENDFORM. "handle_data_changed_finished