1 在GUI状态里添加自定义的按钮,保存、激活;
2 修改代码:
(1)自定义的按钮的触发在 FRM_USER_COMMAND 这个模块里面。
(2)这里注意 FRM_USER_COMMAND 的参数是两个,把自定义的按钮和触发后的操作写在CASE里面。
3 效果展示如下:
贴一下完整代码:
FORM display_data .
* alv 显示定义
DATA: i_grid_settings TYPE lvc_s_glay,
lt_fieldcat TYPE slis_t_fieldcat_alv,
lr_aggregations TYPE REF TO cl_salv_aggregations.
DATA:gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
gs_layout TYPE slis_layout_alv.
* 自定义ALV 显示方式
DEFINE fill_alv.
clear gt_fieldcat.
gt_fieldcat-fieldname = &1. "变量1 字段名
gt_fieldcat-seltext_m = &2 . "变量2 字段描述
gt_fieldcat-outputlen = &3 . "变量2 字段描述
gt_fieldcat-NO_ZERO = &4.
append gt_fieldcat.
END-OF-DEFINITION.
fill_alv 'CARRID' '航线代码' '3' 'X'.
fill_alv 'AIRPFROM' '起飞机场' '3' 'X'.
fill_alv 'CONNID' '航线连接' '4' 'X'.
fill_alv 'FLTIME' '航班时间' '10' 'X'.
fill_alv 'CITYFROM' '起飞城市' '20' 'X'.
fill_alv 'CITYTO' '目标城市' '20' 'X'.
LOOP AT gt_fieldcat.
MODIFY gt_fieldcat.
IF gt_fieldcat-fieldname = ' FLTIME'.
gt_fieldcat-edit = 'X'.
gt_fieldcat-outputlen = '50'.
ENDIF.
MODIFY gt_fieldcat.
ENDLOOP.
i_grid_settings-edt_cll_cb = 'X'. "退出可编辑单元格时回调
gs_layout-zebra = 'X'.
gs_layout-colwidth_optimize = 'X'.
gs_layout-box_fieldname = 'ZSEL'. "定义边框选中栏
gs_layout-window_titlebar = '查询表'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
i_grid_settings = i_grid_settings
is_layout = gs_layout
it_fieldcat = gt_fieldcat[]
it_sort = lt_sort
* it_events = i_events
i_save = 'A'
TABLES
t_outtab = gt_tab
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " DISPLAY_DATA
FORM frm_user_command USING ucomm TYPE sy-ucomm
p_selfld TYPE slis_selfield.
CASE ucomm.
WHEN '&BACK'.
leave to screen 0.
ENDCASE.
ENDFORM.
总结:自定义按钮体现在i_callback_user_command = 'FRM_USER_COMMAND' 中,其中FRM_USER_COMMAND是自定义的过程。