ABAP 之SET_TITLEBAR的使用方式
序
HELLO,这里是百里,一个学习中的ABAPER,在工作中我们肯定会遇到同一个程序有高度重合的情况,不同的TCODE只是展示区别很少区别的内容,这个时候我们就会使用到今天所讲的东西.set_titlebar
.
什么是set_titlebar ?
在ALV选择屏幕的界面及数据展示界面都会有对应的内容展示标题,这个显示的内容我们叫做TITLE ,跟英文字面翻译意思一样,就是title(标题) .而abap 中的set_titlebar 就是 设置标题. 我们通过这是标题显示的内容不同,从而给用以表现出不同的报表.
常见用处如,数据库龄,金额,分类,分工厂等都会出现这种情况,就是给A,B两个人看数据,他们百分之90的数据一致,我们只有10% 内容不一致,我们就可以通过SET_TITLEBAR 结合其他代码改动实现功能.
语法解析
CASE sy-tcode.
WHEN 'TCODE1'.
SET TITLEBAR '变量1' WITH '标题名1'.
WHEN 'TCODE2'.
SET TITLEBAR '变量2' WITH '标题名2'.
ENDCASE.
我们一般将这段语法分装到对应的子例程中,注意我们的变量需要通过双击的方式,进行替换.
还有需要注意就是需要在
INITIALIZATION 的地方调用一遍, 之后再 START-OF-SELECTION. 的地方需要再次调用一遍.
实例讲解
本内容案例是我们通过不同的TCODE设定从而实现展示不同的抬头及给两个TCODE展示不同的字段.
数据准备
TABLES : ZWDEMOTABLE1 .
" 数据准备
TYPES:BEGIN OF ty_Data ,
doccode TYPE char20 ,
zrowid TYPE char20 ,
matcode TYPE char20 ,
matname TYPE char20 ,
uom TYPE char20 ,
memo TYPE char20 ,
light TYPE iconname, "指示灯
msg TYPE string,"消息
END OF ty_Data .
data : gs_data TYPE ty_Data .
data : gt_DAta TYPE TABLE of ty_Data .
" alv 通用变量
DATA: wa_layout TYPE lvc_s_layo.
DATA:wa_fieldcat TYPE lvc_s_fcat,
gt_fieldcat TYPE lvc_t_fcat.
DATA gs_grid TYPE lvc_s_glay. "新
创建ALV变量及子例程
INITIALIZATION .
PERFORM frm_set_titlebar.
at SELECTION-SCREEN.
at SELECTION-SCREEN OUTPUT .
START-OF-SELECTION .
PERFORM frm_set_titlebar.
PERFORM get_data .
END-OF-SELECTION .
PERFORM diaplay_data .
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM get_data .
"select * from ZWDEMOTABLE1 as a into CORRESPONDING FIELDS OF TABLE gt_DAta .
select doccode ,zrowid ,matcode ,matname ,uom ,MEmo from ZWDEMOTABLE1 as a into TABLE @gt_DAta .
"select doccode zrowid matcode matname uom MEmo from ZWDEMOTABLE1 as a into TABLE gt_DAta .
LOOP AT gt_DAta into gs_data.
IF gs_data-uom = '双'.
gs_data-msg = '这个单位是' && gs_data-uom && '是红灯' .
gs_data-light = icon_led_red.
ELSE.
gs_data-msg = '这个单位是' && gs_data-uom && '是绿灯' .
gs_data-light = icon_led_green.
ENDIF.
MODIFY gt_DAta FROM gs_data .
CLEAR gs_data .
ENDLOOP.
ENDFORM.
FORM diaplay_data .
PERFORM set_alv_layout.
PERFORM bulid_fieldcat.
PERFORM call_alv_func.
ENDFORM.
FORM set_alv_layout .
CLEAR: wa_layout.
wa_layout-zebra = 'X'. "斑马线
"wa_layout-cwidth_opt = 'X'. "自动列宽
ENDFORM.
FORM bulid_fieldcat .
DEFINE add_col.
* ADD 1 TO pos.
* lw_fieldcat-col_pos = pos.
wa_fieldcat-fieldname = &1.
wa_fieldcat-ref_field = &2.
wa_fieldcat-ref_table = &3.
wa_fieldcat-scrtext_l = &4.
wa_fieldcat-outputlen = &5.
wa_fieldcat-no_zero = &6.
wa_fieldcat-edit = &7.
wa_fieldcat-edit_mask = &8.
wa_fieldcat-key = &9.
CASE wa_fieldcat-fieldname.
WHEN 'MSG' .
wa_fieldcat-outputlen = '40' .
" WHEN 'slbox'.
" wa_fieldcat-checkbox = 'X' .
" wa_fieldcat-edit = 'X' .
" wa_fieldcat-edit_mask = 'X'.
" WHEN 'VBELN' .
" wa_fieldcat-HOTSPOT = 'X' .
" WHEN 'KBETR'.
" wa_fieldcat-decimals_o = 2 .
" WHEN 'FKIMG' .
" wa_fieldcat-decimals_o = 0 .
WHEN OTHERS.
ENDCASE .
APPEND wa_fieldcat TO gt_fieldcat.
CLEAR : wa_fieldcat.
END-OF-DEFINITION.
REFRESH: gt_fieldcat.
CASE SY-tcode .
WHEN 'ZDE001'.
add_col 'LIGHT' space space '状态灯' space space space space space.
add_col 'MSG' space space '消息' space space space space space.
add_col 'DOCCODE' space space '单号' space space space space space.
add_col 'ZROWID' space space 'rowid' space space space space space.
"add_col 'MATCODE' space space '料号' space space space space space.
"add_col 'MATNAME' space space '料名' space space space space space.
"add_col 'UOM' space space '单位' space space space space space.
"add_col 'MEMO' space space '备注' space space space space space.
WHEN 'ZSD001A'.
add_col 'MATCODE' space space '料号' space space space space space.
add_col 'MATNAME' space space '料名' space space space space space.
add_col 'UOM' space space '单位' space space space space space.
add_col 'MEMO' space space '备注' space space space space space.
WHEN OTHERS.
ENDCASE.
ENDFORM.
FORM call_alv_func .
DATA :gt_event TYPE slis_t_event,
gs_event TYPE slis_alv_event.
" MOVE 'DATA_CHANGED' TO gs_event-name.
" MOVE 'ALV_DATA_CHANGED' TO gs_event-form.
APPEND gs_event TO gt_event.
gs_grid-edt_cll_cb = abap_true. " 选中复选款,立刻触发data changed 事件
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid "回调程序
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
i_grid_settings = gs_grid
is_layout_lvc = wa_layout
it_fieldcat_lvc = gt_fieldcat "需要显示的内表的列
i_save = 'A'
it_events = gt_event
TABLES
t_outtab = gt_DAta "需要显示的数据
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
ENDFORM.
FORM frm_set_titlebar .
CASE sy-tcode.
WHEN 'ZDE001'.
SET TITLEBAR 'ZTB1' WITH 'demo报表1'.
WHEN 'ZDE001A'.
SET TITLEBAR 'ZTB1' WITH 'demo报表1A'.
ENDCASE.
ENDFORM.
双击ZTB1 创建对应的字段对象,注意需要激活
结果
如图 通过不同TCODE 控制展示出不同的ALV数据及抬头内容 .
技术总计
今天讲述的内容是如何通过SET_TITILEBAR 设置同一程序实现不同的TOCDE, 满足用的的需求. 通过使用该功能及其他逻辑联合使用,我们只需要一遍代码+少量修改就可以实现两个或者多个TOCODE 的内容
百里鸡汤
学练并举,成竹在胸,敢问逐鹿群雄今何在?师生同志,协力攻关,笑看燕赵魁首谁人得.
这里是百里,一个努力的学习者. 努力学习好好记录,点滴进步,就是成功.
- 点赞
- 收藏
- 关注作者
评论(0)