OO ALV常用功能完整简例.docx
《OO ALV常用功能完整简例.docx》由会员分享,可在线阅读,更多相关《OO ALV常用功能完整简例.docx(20页珍藏版)》请在冰豆网上搜索。
![OO ALV常用功能完整简例.docx](https://file1.bdocx.com/fileroot1/2023-2/4/52b3a520-a5e5-40d6-9441-dc78a445f93c/52b3a520-a5e5-40d6-9441-dc78a445f93c1.gif)
OOALV常用功能完整简例
OO ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)
(2013-07-2314:
18:
51)
转载▼
标签:
alv
oo
热键
跳转
ooalv
分类:
ALV
OOALV 常用功能简介
目录
OOALV 常用功能简介
一、所用类:
二、效果图:
单击
双击
F4帮助
编辑某单元格并校验
三、实现过程:
1、 创建一个屏幕9001,不需要画任何控件
2、 定义数据:
3、 获取显示数据:
4、 alv 显示数据:
一、所用类:
cl_gui_alv_grid
cl_gui_docking_container
二、效果图:
执行:
单击
单击航线单元格:
双击
双击连接号码单元格:
F4帮助
航班单元格上F4或点击 后面的小按钮
(本功能在设置字段样式时实现DEFINE mac_fieldlog.
"设置参考表和参考字段,为了提供搜索帮助
wa_fieldcatalog-ref_table = &4. "参考表
wa_fieldcatalog-ref_field = &5. "参考字段
编辑某单元格并校验
修改价格后随便点击某个按钮或者双击某个字段,触发:
handle_data_changed
三、实现过程:
1、 创建一个屏幕9001,不需要画
2、 定义数据:
*&---------------------------------------------------------------
*& ALV 全局变量
*&---------------------------------------------------------------
DATA:
g_alv_grid TYPE REF TO cl_gui_alv_grid,"ALV控件
alv_ctner TYPE REF TO cl_gui_docking_container.
DATA:
wa_fieldcatalog TYPE lvc_s_fcat, "显示数据列内表工作区域
it_fieldcatalog TYPE lvc_t_fcat. "显示数据列内表
DATA:
g_event TYPE REF TO alv_event_receiver.
*&---------------------------------------------------------------
*& 全局变量
*&---------------------------------------------------------------
DATA:
BEGIN OF str_spfli,
checkbox TYPE char1, "复选框
carrid TYPE sflight-carrid , "航线承运人ID
connid TYPE sflight-connid , "航班连接 Id
fldate TYPE sflight-fldate , "航班日期
price TYPE sflight-price , "航空运费
END OF str_spfli.
DATA:
g_it_show LIKE TABLE OF str_spfli WITH KEY checkbox carrid.
g_wa_show LIKE str_spfli.
3、 获取显示数据:
"获取数据
SELECT carrid
connid
fldate
price
FROM sflight INTO CORRESPONDING FIELDS OF TABLE g_it_show.
4、 alv 显示数据:
PROCESS BEFORE OUTPUT.
MODULE STATUS_9001.
"alv 显示数据
MODULE MDL_ALV_SHOW.
*&---------------------------------------------------------------
module MDL_ALV_SHOW output.
PERFORM zform_alv_show.
endmodule.
*&---------------------------------------------------------------
FORM zform_alv_show .
DATA:
l_style TYPE i,
ls_layout TYPE lvc_s_layo,
is_stable TYPE lvc_s_stbl,
l_disvariant TYPE disvariant.
* 屏幕初始化
IF alv_ctner IS INITIAL.
CREATE OBJECT alv_ctner
EXPORTING
repid = sy-repid
dynnr = '9001'
* side = cl_gui_docking_container=>dock_at_right ”ALV贴屏幕右边,从屏幕右边开始算宽度
side = cl_gui_docking_container=>dock_at_left "ALV贴屏幕左边,从左边算屏幕宽度,
extension = 900 "屏幕宽度
style = cl_gui_control=>ws_child "可选参数,设置ALV是否可用手动拖动大小
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE s001(00) WITH '屏幕初始化失败'.
LEAVE LIST-PROCESSING.
ENDIF.
*添加自定义工具条
PERFORM frm_set_alv_toolbar.
*
CREATE OBJECT g_alv_grid
EXPORTING
i_parent = alv_ctner.
*构建显示字段样式
PERFORM frm_build_fieldcat.
"Set/Reset Ready for Input Status
CALL METHOD g_alv_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
* layout 设定样式
CLEAR ls_layout.
ls_layout-cwidth_opt = 'X'. "优化列宽
CLEAR l_disvariant.
l_disvariant-report = sy-repid. "当前程序
* event 绑定事件
CREATE OBJECT g_event.
"自定义工具条
SET HANDLER g_event->toolbar FOR g_alv_grid.
"数据修改事件
SET HANDLER g_event->handle_data_changed FOR g_alv_grid.
"单击某一单元格事件
SET HANDLER g_event->handle_cell_click FOR g_alv_grid.
"用户点击工具条上按钮触发的事件
SET HANDLER g_event->handle_user_command FOR g_alv_grid.
"双击某一单元格事件
SET HANDLER g_event->handle_double_click FOR g_alv_grid.
"以下两个方法达到的效果是:
光标离开正在编辑的单元格时,
就触发了handle_data_changed
"如果不注释掉以下两个方法,
则只有执行下一操作时才会触发handle_data_changed,比如点保存、删除
CALL METHOD g_alv_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
CALL METHOD g_alv_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
* "显示内表数据
CALL METHOD g_alv_grid->set_table_for_first_display
EXPORTING
is_layout = ls_layout
is_variant = l_disvariant
it_toolbar_excluding = gt_tools
CHANGING
it_fieldcatalog = it_fieldcatalog[]
it_outtab = g_it_show[].
ELSE.
"冲销设置tcode 为 RELOAD,防止因为原来的tcode影响程序执行
CALL METHOD cl_gui_cfw=>set_new_ok_code
EXPORTING
new_code = 'RELOAD'.
"ALV 控制:
刷新稳定性 按行刷新,可选参数
CLEAR is_stable.
is_stable-col = 'X'.
"刷新
CALL METHOD g_alv_grid->refresh_table_display
EXPORTING
is_stable = is_stable
i_soft_refresh = 'X'.
"提交
CALL METHOD cl_gui_cfw=>flush.
ENDIF.
ENDFORM. " ZFORM_ALV_SHOW
其中:
添加工具条上常用按钮
FORM frm_set_alv_toolbar .
REFRESH gt_tools[].
"你也可以删除标准的功能按钮
DELETEe_object->mt_toolbarWHEREFUNCTION='&DETAIL'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&REFRESH'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&LOCAL&UNDO'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&SORT_ASC'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&SORT_DSC'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&FIND'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&FIND_MORE'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&MB_FILTER'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&MB_SUM'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&MB_SUBTOT'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&MB_EXPORT'.
DELETEe_object->mt_toolbarWHEREFUNCTION='&MB_VARIANT'.
DELETEe_object->mt_toolbarWHEREquickinfo='插入行'.
DELETEe_object->mt_toolbarWHEREquickinfo='删除行'.
DELETEe_object->mt_toolbarWHEREquickinfo='剪切'.
DELETEe_object->mt_toolbarWHEREquickinfo='复制文本'.
DELETEe_object->mt_toolbarWHEREquickinfo='插入总览'.
DELETEe_object->mt_toolbarWHEREquickinfo='附加行'.
DELETEe_object->mt_toolbarWHEREquickinfo='复制行'.
DELETEe_object->mt_toolbarWHEREquickinfo='视图'.
DELETEe_object->mt_toolbarWHEREquickinfo='显示图形'.
DELETEe_object->mt_toolbarWHEREquickinfo='撤销'.
DELETEe_object->mt_toolbarWHEREquickinfo='明细'.
DELETEe_object->mt_toolbarWHEREquickinfo='打印'.
DELETEe_object->mt_toolbarWHEREquickinfo='最终用户文档'.
APPEND cl_gui_alv_grid=>mc_fc_check TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_cut TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_copy TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_copy_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_delete_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_move_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_paste TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_paste_new_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_sum TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_info TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_refresh TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_graph TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_append_row TO gt_tools.
APPEND cl_gui_alv_grid=>mc_fc_loc_undo TO gt_tools.
ENDFORM. " FRM_SET_ALV_TOOLBAR
设置显示字段和其参考字段
DEFINE mac_fieldlog.
* g_pos = g_pos + 1. "第几列
* wa_fieldcatalog-col_pos = g_pos. "列
wa_fieldcatalog-fieldname = &1. "字段名称
* wa_fieldcatalog-coltext = &2. "显示名称,跟reptext效果基本相同
wa_fieldcatalog-reptext = &2.
wa_fieldcatalog-outputlen = &3. "显示长度
"设置参考表和参考字段,为了提供搜索帮助
wa_fieldcatalog-ref_table = &4. "参考表
wa_fieldcatalog-ref_field = &5. "参考字段
"设置复选框可编辑
if &1 eq 'CHECKBOX'.
wa_fieldcatalog-checkbox = 'X'.
wa_fieldcatalog-edit = 'X'.
endif.
"设置 价格PRICE字段 可修改
if &1 eq 'PRICE'.
wa_fieldcatalog-edit = 'X'.
endif.
"设置 航线承运人字段 CARRID 为热键
if &1 eq 'CARRID'.
wa_fieldcatalog-hotspot = 'X'.
endif.
append wa_fieldcatalog to it_fieldcatalog.
clear wa_fieldcatalog.
END-OF-DEFINITION.
FORM frm_build_fieldcat .
mac_fieldlog 'CHECKBOX' '选择' '' ' ' ' ' .
mac_fieldlog 'CARRID' '航线承运人' '' 'SFLIGHT' 'CARRID'.
mac_fieldlog 'CONNID' '航班数量' '' 'SFLIGHT' 'CONNID' .
mac_fieldlog 'FLDATE' '国家' '' 'SFLIGHT' 'FLDATE '. "最后两个字段 参考表内字段,可以作为帮助
mac_fieldlog 'PRICE' '起飞城市' '' 'SFLIGHT' 'PRICE' .
ENDFORM. " FRM_BUILD_FIELDCAT
事件的定义
*----------------------------------------------------------------
* CLASS ALV_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------
CLASS alv_event_receiver DEFINITION.
PUBLIC SECTION.
"添加工具条
METHODS toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING
e_object
e_interactive.
"子都修改时触发
METHODS handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING
er_data_changed.
"某字段设置热键后,单击此单元格时触发
METHODS handle_cell_click FOR EVENT