ABAP Help Document(21):10.2 Selection Screens
10.2 Selection screens
Selection screens为特殊dynpros,可以在executable programs, function groups,module pools中定义。
设置Selection screens的GUI Status,使用RS_SET_SELSCREEN_STATUS,特殊情况下,使用RS_EXTERNAL_SELSCREEN_STATUS。
1.creat selection screens
语法:
1. SELECTION-SCREEN BEGIN OF SCREEN dynnr [TITLE title]
[AS WINDOW].
SELECTION-SCREEN
END OF SCREEN dynnr.
创建普通屏幕。
语法:
2. SELECTION-SCREEN
BEGIN OF SCREEN dynnr AS SUBSCREEN
[NO
INTERVALS]
[NESTING
LEVEL n].
SELECTION-SCREEN
END OF SCREEN dynnr.
创建子屏幕。
SELECTION-SCREEN中可以包含PARAMETER,SELECTION-OPTIONS,SELECTION-SCREEN,但是screen不能嵌套。
定义的SELECTION-SCREEN可以在Dynpro,PBO,PAI调用使用call screen,
call subscreen嵌入子屏幕或Tabstrip。
示例1:
"创建selection screens "TITLE:指定title "AS WINDOW:显示为弹窗 SELECTION-SCREEN BEGIN OF SCREEN 500 TITLE title AS WINDOW. PARAMETERS name TYPE sy-uname. SELECTION-SCREEN END OF SCREEN 500. title = 'Input name'. "调用屏幕 CALL SELECTION-SCREEN '0500' STARTING AT 10 10.
示例2:
"创建子屏幕 SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN. PARAMETERS: p1 TYPE c LENGTH 10, p2 TYPE c LENGTH 10, p3 TYPE c LENGTH 10. SELECTION-SCREEN END OF SCREEN 100. SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN. PARAMETERS: q1 TYPE c LENGTH 10, q2 TYPE c LENGTH 10, q3 TYPE c LENGTH 10. SELECTION-SCREEN END OF SCREEN 200. "屏幕500 SELECTION-SCREEN BEGIN OF SCREEN 500 AS WINDOW. "定义tabstrip SELECTION-SCREEN:BEGIN OF TABBED BLOCK mytab FOR 10 LINES, TAB (20) button1 USER-COMMAND push1 DEFAULT SCREEN 100, TAB (20) button2 USER-COMMAND push2 DEFAULT SCREEN 200, END OF BLOCK mytab. SELECTION-SCREEN END OF SCREEN 500. INITIALIZATION. button1 = 'Selection Screen 1'. button2 = 'Selection Screen 2'. START-OF-SELECTION. "CALL SCREEN 500. "当在dynpro的pbo,pai中call screen, "如果是selection screen,必须使用下面方式 CALL SELECTION-SCREEN 500.
2.selection screens elements
selection screen中有多种组件;selection screen最大宽度83;
SKIP
语法:SELECTION-SCREEN SKIP [n] [ldb_additions].
创建空行,默认1行。
ULINE
语法:
SELECTION-SCREEN ULINE [[/][pos|POS_LOW|POS_HIGH](len)]
[MODIF ID modid]
[ldb_additions].
创建一条线,在线必须有内容。
示例:
"划线,在线必须有内容,不能是空白 SELECTION-SCREEN ULINE /1(60).
COMMENT
语法:
SELECTION-SCREEN COMMENT [/][pos|POS_LOW|POS_HIGH](len)
{text|{[text] FOR FIELD sel}}
[VISIBLE LENGTH vlen]
[MODIF ID modid]
[ldb_additions].
示例:
"文本 "/可省略,指定文本起始位置,长度 "SELECTION-SCREEN COMMENT /5(10) test. "for field,文本作为element标签,只在line中生效 "visible length,可见长度 "element必须在comment后 SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN COMMENT 5(10) test FOR FIELD P1 VISIBLE LENGTH 5. PARAMETERS:p1 TYPE sy-datum. SELECTION-SCREEN END OF LINE.
PUSHBUTTON
语法:
SELECTION-SCREEN PUSHBUTTON [/][pos|POS_LOW|POS_HIGH ](len) button_text
USER-COMMAND fcode
[VISIBLE LENGTH vlen]
[MODIF ID modid]
[ldb_additions].
创建按钮,使用ICON_CREATE创建图标,或者使用text-xxx,text描述。
示例:
"按钮 SELECTION-SCREEN BEGIN OF LINE. "可以后续赋值描述 SELECTION-SCREEN PUSHBUTTON 5(30) btn1 USER-COMMAND clk1. "使用text-xxx描述 SELECTION-SCREEN PUSHBUTTON 40(10) text-001 USER-COMMAND clk2. SELECTION-SCREEN END OF LINE.
创建图标
FORM f_creat_icon. "按钮长度必须很长,才能显示完全 CALL FUNCTION 'ICON_CREATE' EXPORTING name = 'ICON_CHECKED' text = 'Button1' info = 'quick info' add_stdinf = '' IMPORTING RESULT = btn1 EXCEPTIONS OTHERS = 0. ENDFORM.
LINE
语法:
SELECTION-SCREEN BEGIN OF LINE.
[SELECTION-SCREEN POSITION
{pos|POS_LOW|POS_HIGH} [ldb_additions]].
SELECTION-SCREEN END OF LINE.
定义一行,position指定空白位置
line中不能嵌套定义line;
line中elements的text使用comment指定;
line中不能定义block,tabstrips;
BLOCK
语法:
SELECTION-SCREEN
BEGIN OF BLOCK block
[WITH
FRAME [TITLE title]]
[NO
INTERVALS].
SELECTION-SCREEN END OF BLOCK
block.
定义块,block名称最大长度20字符;
块最多嵌套5层;
title最大长度8个字符,text-xxx最大长度70个字符;
示例:
"块 SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001. PARAMETERS bp_1 TYPE sy-datum. SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME. PARAMETERS bp_2 TYPE sy-datum. SELECTION-SCREEN BEGIN OF BLOCK blk3 WITH FRAME. PARAMETERS bp_3 TYPE sy-datum. SELECTION-SCREEN END OF BLOCK blk3. SELECTION-SCREEN END OF BLOCK blk2. SELECTION-SCREEN END OF BLOCK blk1.
TABBED BLOCK
语法:
SELECTION-SCREEN BEGIN OF TABBED BLOCK tblock FOR n LINES [NO INTERVALS]. [SELECTION-SCREEN TAB (len) tab USER-COMMAND fcode
[DEFAULT [PROGRAM prog] SCREEN dynnr]
[MODIF ID modid]
[ldb_additions].]
SELECTION-SCREEN END OF BLOCK tblock.
定义Tabstrip,tblock最大字符16。n指定行数,最大197。
(len)最大长度79,tab最大长度8字符。
FUNCTION KEY
语法:
SELECTION-SCREEN FUNCTION KEY n [ldb_additions].
定义application toolbar,包含5个pushbutton,funciton code:FC01~FC05;
定义接口工作区,SSCRFIELDS;
使用SMP_DYNTXT,显示icon;
示例:
定义接口
"function key 接口 TABLES sscrfields.
定义function key
"function key SELECTION-SCREEN FUNCTION KEY 1. SELECTION-SCREEN FUNCTION KEY 2.
设置function key
"设置function key icon FORM f_func_key_icon. "使用这个或icon 编码4位 "TYPE-POOLS icon. "SMP_DYNTXT(菜单制作器:动态文本的程序接口) DATA:func_icon TYPE smp_dyntxt. "func_icon-text = 'func1'. "显示文本,没用? func_icon-icon_id = '@0N@'. "显示icon func_icon-icon_text = 'func1'."图标文本 func_icon-quickinfo = 'quick info'."提示tip "function key text显示 sscrfields-functxt_01 = func_icon. sscrfields-functxt_02 = 'func2'. ENDFORM.
用户操作响应,在AT SELECTION-SCREEN,处理用户操作。
"function key comm FORM f_function_key. "响应function key CASE sscrfields-ucomm. WHEN 'FC01'. MESSAGE 'function 1' TYPE 'I'. CALL SELECTION-SCREEN 501 STARTING AT 50 50. WHEN 'FC02'. MESSAGE 'function 2' TYPE 'I'. CALL SELECTION-SCREEN 502 STARTING AT 50 50. WHEN OTHERS. ENDCASE. ENDFORM.
MODIF ID
语法:
MODIF ID modid
可以定义Group,根据id对屏幕元素进行MODIFY SCREEN,SCREEN-group1进行判断。
以下Elements可以定义modify id:
SELECTION-SCREEN - COMMENT
SELECTION-SCREEN - PUSHBUTTON
SELECTION-SCREEN - TAB
SELECTION-SCREEN - ULINE
PARAMETERS
SELECT-OPTIONS
INCLUDE
语法:
SELECTION-SCREEN INCLUDE
可以包含同program其他selection-screen定义的elements。
1. SELECTION-SCREEN INCLUDE PARAMETERS para
[OBLIGATORY
[OFF]]
[MODIF
ID modid]
[ID
id].
包含parameter,obligatory重新定义是否必输。
2. SELECTION-SCREEN
INCLUDE SELECT-OPTIONS selcrit
[OBLIGATORY
[OFF]]
[NO
INTERVALS [OFF]]
[MODIF
ID modid]
[ID
id].
包含select options,更改obligatory,no intervals等。
3. SELECTION-SCREEN
INCLUDE COMMENT [/][pos](len) text
[FOR FIELD sel]
[MODIF ID modid]
[ID id].
包含comment,可以指定新的for field。
4. SELECTION-SCREEN
INCLUDE PUSHBUTTON [/][pos](len) button_text
[USER-COMMAND
ucom]
[MODIF
ID modid]
[ID
id].
包含pushbutton,可以指定新的user-command。
5. SELECTION-SCREEN INCLUDE BLOCKS block [ID id].
包含block
示例:
"使用include SELECTION-SCREEN BEGIN OF SCREEN 501 AS WINDOW. "如果p1已经obligatory,使用off关闭必输 SELECTION-SCREEN INCLUDE PARAMETERS p1 OBLIGATORY OFF. "可以定义新的user command??有问题 "SELECTION-SCREEN INCLUDE PUSHBUTTON 5(20) btn1 USER-COMMAND blk4. "包含block SELECTION-SCREEN INCLUDE BLOCKS blk1. SELECTION-SCREEN END OF SCREEN 501.
PARAMETERS
语法:
PARAMETERS {para[(len)]}|{para [LENGTH len]}
[type_options]
[screen_options]
[value_options]
[ldb_options].
[type_options]:类型定义
{ TYPE type [DECIMALS dec] }
| { LIKE dobj }
| { LIKE (name) }
[screen_options]:屏幕选项
{ {[OBLIGATORY|NO-DISPLAY] [VISIBLE LENGTH vlen]}
| {AS CHECKBOX [USER-COMMAND fcode]}
| {RADIOBUTTON GROUP group [USER-COMMAND fcode]}
| {AS LISTBOX VISIBLE LENGTH vlen [USER-COMMAND fcode] [OBLIGATORY]}
}
[MODIF ID modid] ... .
[value_options]:值选项
[DEFAULT val]
[LOWER CASE]
[MATCHCODE OBJECT search_help]
[MEMORY ID pid]
[VALUE CHECK]
创建parameter,单个值输入框。
para名字最长8个字符;
type不能是b, s, f, xstring类型;
group名字最长4个字符;
fcode名字最长20个字符;
示例:
"parameters "动态名,大写才能带出帮助等dict信息 DATA:name TYPE C LENGTH 132 VALUE 'SPFLI-CARRID'. SELECTION-SCREEN BEGIN OF SCREEN 502 AS WINDOW. "para最长8个字符 "使用(len)长度,length n指定长度,数据类型为C PARAMETERS:p01(4). PARAMETERS:p02 LENGTH 4. "使用type PARAMETERS:p03 TYPE C LENGTH 10. "reference PARAMETERS:p04 TYPE spfli-carrid. "like PARAMETERS:p05 LIKE p04. "like (name),name最长132 PARAMETERS:p06 LIKE (name). "是否必输 PARAMETERS:p07 TYPE C LENGTH 10 OBLIGATORY. "是否显示 PARAMETERS:p08 TYPE C LENGTH 10 NO-DISPLAY. "显示长度,可输入长度10,可见长度5 PARAMETERS:p09 TYPE C LENGTH 10 VISIBLE LENGTH 5. "checkbox,sscrfields-ucomm可以处理 PARAMETERS:p_check AS CHECKBOX USER-COMMAND chk1. "radiobutton,group名最长4位 PARAMETERS:p_rad1 RADIOBUTTON GROUP gp1. PARAMETERS:p_rad2 RADIOBUTTON GROUP gp1. "listbox PARAMETERS:p_listb TYPE spfli-carrid VISIBLE LENGTH 10 AS LISTBOX. "设置默认值,"设置允许小写,不设置自动转换大写 PARAMETERS:p_10 TYPE C LENGTH 10 DEFAULT 'hello' LOWER CASE. "设置帮助f4 "MATCHCODE OBJECT S_CARRIER_ID 和 check value不能同时 PARAMETERS:p_11 TYPE C LENGTH 2 MATCHCODE OBJECT S_CARRIER_ID. "设置值检查,建议必输,值检查同时设置,避免检查空值 PARAMETERS:p_12 TYPE spfli-carrid OBLIGATORY VALUE CHECK. "memory id PARAMETERS:p_13 TYPE sy-repid MEMORY ID rid. SELECTION-SCREEN END OF SCREEN 502.
屏幕输出前,memory id设置初始值:
AT SELECTION-SCREEN OUTPUT. "设置parameter值 SET PARAMETER ID 'RID' FIELD 'TEST ID'.
SELECT OPTIONS
语法:
SELECT-OPTIONS selcrit FOR {dobj|(name)}
[screen_options]
[value_options]
[NO DATABASE SELECTION]
[ldb_options].
[screen_options]:屏幕选项
[OBLIGATORY|NO-DISPLAY]
[VISIBLE LENGTH vlen]
[NO-EXTENSION]
[NO INTERVALS]
[MODIF ID modid]
[value_options]:值选项
[DEFAULT val1 [TO val2] [OPTION opt] [SIGN sgn]]
[LOWER CASE]
[MATCHCODE OBJECT search_help]
[MEMORY ID pid]
创建select-options,多值范围输入。
selcrit名字最大8个字符;
示例:
"select options TABLES:spfli. SELECTION-SCREEN BEGIN OF SCREEN 503 AS WINDOW. "必须声明table:spfli SELECT-OPTIONS s_01 FOR spfli-carrid. "动态定义 SELECT-OPTIONS s_02 FOR (name). "没有第二个输入框 SELECT-OPTIONS s_03 FOR spfli-carrid NO INTERVALS. "没有multiple selection SELECT-OPTIONS s_04 FOR spfli-carrid NO-EXTENSION. "不显示 SELECT-OPTIONS s_05 FOR spfli-carrid NO-DISPLAY. "默认值 "option:非范围:eq, ne, ge, gt, le, lt, cp, or np; "当指定TO范围:bt,nb; "sign:i or e;大小写都可 SELECT-OPTIONS s_06 FOR spfli-carrid DEFAULT 'AA' TO 'AZ' OPTION bt SIGN i. SELECTION-SCREEN END OF SCREEN 503.
示例:整体示例
"function key 接口 TABLES sscrfields. "select seceen elements SELECTION-SCREEN BEGIN OF SCREEN 500 TITLE text-001. "两行空行 SELECTION-SCREEN SKIP 2. "文本 "/可省略,指定文本起始位置,长度 "SELECTION-SCREEN COMMENT /5(10) test. "for field,文本作为element标签,只在line中生效 "visible length,可见长度 "element必须在comment后 SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN COMMENT 5(10) test FOR FIELD P1 VISIBLE LENGTH 5. PARAMETERS:p1 TYPE sy-datum OBLIGATORY. SELECTION-SCREEN END OF LINE. "划线,在线必须有内容,不能是空白 SELECTION-SCREEN ULINE /1(60). "按钮 SELECTION-SCREEN BEGIN OF LINE. "可以后续赋值描述 SELECTION-SCREEN PUSHBUTTON 5(30) btn1 USER-COMMAND clk1. "使用text-xxx描述 SELECTION-SCREEN PUSHBUTTON 40(10) text-001 USER-COMMAND clk2. SELECTION-SCREEN END OF LINE. "块 SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001. PARAMETERS bp_1 TYPE sy-datum. SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME. PARAMETERS bp_2 TYPE sy-datum. SELECTION-SCREEN BEGIN OF BLOCK blk3 WITH FRAME. PARAMETERS bp_3 TYPE sy-datum. SELECTION-SCREEN END OF BLOCK blk3. SELECTION-SCREEN END OF BLOCK blk2. SELECTION-SCREEN END OF BLOCK blk1. "function key SELECTION-SCREEN FUNCTION KEY 1. SELECTION-SCREEN FUNCTION KEY 2. SELECTION-SCREEN FUNCTION KEY 3. SELECTION-SCREEN END OF SCREEN 500. "使用include SELECTION-SCREEN BEGIN OF SCREEN 501 AS WINDOW. "如果p1已经obligatory,使用off关闭必输 SELECTION-SCREEN INCLUDE PARAMETERS p1 OBLIGATORY OFF. "可以定义新的user command??有问题 "SELECTION-SCREEN INCLUDE PUSHBUTTON 5(20) btn1 USER-COMMAND blk4. "包含block SELECTION-SCREEN INCLUDE BLOCKS blk1. SELECTION-SCREEN END OF SCREEN 501. "parameters "动态名,大写才能带出帮助等dict信息 DATA:name TYPE C LENGTH 132 VALUE 'SPFLI-CARRID'. SELECTION-SCREEN BEGIN OF SCREEN 502 AS WINDOW. "para最长8个字符 "使用(len)长度,length n指定长度,数据类型为C PARAMETERS:p01(4). PARAMETERS:p02 LENGTH 4. "使用type PARAMETERS:p03 TYPE C LENGTH 10. "reference PARAMETERS:p04 TYPE spfli-carrid. "like PARAMETERS:p05 LIKE p04. "like (name),name最长132 PARAMETERS:p06 LIKE (name). "是否必输 PARAMETERS:p07 TYPE C LENGTH 10 OBLIGATORY. "是否显示 PARAMETERS:p08 TYPE C LENGTH 10 NO-DISPLAY. "显示长度,可输入长度10,可见长度5 PARAMETERS:p09 TYPE C LENGTH 10 VISIBLE LENGTH 5. "checkbox,sscrfields-ucomm可以处理 PARAMETERS:p_check AS CHECKBOX USER-COMMAND chk1. "radiobutton,group名最长4位 PARAMETERS:p_rad1 RADIOBUTTON GROUP gp1. PARAMETERS:p_rad2 RADIOBUTTON GROUP gp1. "listbox PARAMETERS:p_listb TYPE spfli-carrid VISIBLE LENGTH 10 AS LISTBOX. "设置默认值,"设置允许小写,不设置自动转换大写 PARAMETERS:p_10 TYPE C LENGTH 10 DEFAULT 'hello' LOWER CASE. "设置帮助f4 "MATCHCODE OBJECT S_CARRIER_ID 和 check value不能同时 PARAMETERS:p_11 TYPE C LENGTH 2 MATCHCODE OBJECT S_CARRIER_ID. "设置值检查,建议必输,值检查同时设置,避免检查空值 PARAMETERS:p_12 TYPE spfli-carrid OBLIGATORY VALUE CHECK. "memory id PARAMETERS:p_13 TYPE sy-repid MEMORY ID rid. SELECTION-SCREEN END OF SCREEN 502. "select options TABLES:spfli. SELECTION-SCREEN BEGIN OF SCREEN 503 AS WINDOW. "必须声明table:spfli SELECT-OPTIONS s_01 FOR spfli-carrid. "动态定义 SELECT-OPTIONS s_02 FOR (name). "没有第二个输入框 SELECT-OPTIONS s_03 FOR spfli-carrid NO INTERVALS. "没有multiple selection SELECT-OPTIONS s_04 FOR spfli-carrid NO-EXTENSION. "不显示 SELECT-OPTIONS s_05 FOR spfli-carrid NO-DISPLAY. "默认值 "option:非范围:eq, ne, ge, gt, le, lt, cp, or np; "当指定TO范围:bt,nb; "sign:i or e;大小写都可 SELECT-OPTIONS s_06 FOR spfli-carrid DEFAULT 'AA' TO 'AZ' OPTION bt SIGN i. SELECTION-SCREEN END OF SCREEN 503. INITIALIZATION. test = 'test'. PERFORM f_creat_icon. PERFORM f_func_key_icon. AT SELECTION-SCREEN OUTPUT. "设置parameter值 SET PARAMETER ID 'RID' FIELD 'TEST ID'. AT SELECTION-SCREEN. PERFORM f_function_key. START-OF-SELECTION. CALL SCREEN 500. "创建icon FORM f_creat_icon. "按钮长度必须很长,才能显示完全 CALL FUNCTION 'ICON_CREATE' EXPORTING name = 'ICON_CHECKED' text = 'Button1' info = 'quick info' add_stdinf = '' IMPORTING RESULT = btn1 EXCEPTIONS OTHERS = 0. ENDFORM. "f_creat_icon "设置function key icon FORM f_func_key_icon. "使用这个或icon 编码4位 "TYPE-POOLS icon. "SMP_DYNTXT(菜单制作器:动态文本的程序接口) DATA:func_icon TYPE smp_dyntxt. "func_icon-text = 'func1'. "显示文本,没用? func_icon-icon_id = '@0N@'. "显示icon func_icon-icon_text = 'func1'."图标文本 func_icon-quickinfo = 'quick info'."提示tip "function key text显示 sscrfields-functxt_01 = func_icon. sscrfields-functxt_02 = 'func2'. sscrfields-functxt_03 = 'func3'. ENDFORM. "f_func_key_icon "function key comm FORM f_function_key. "响应function key CASE sscrfields-ucomm. WHEN 'FC01'. MESSAGE 'function 1' TYPE 'I'. CALL SELECTION-SCREEN 501 STARTING AT 50 50. WHEN 'FC02'. MESSAGE 'function 2' TYPE 'I'. CALL SELECTION-SCREEN 502 STARTING AT 50 50. WHEN 'FC03'. MESSAGE 'function 3' TYPE 'I'. CALL SELECTION-SCREEN 503 STARTING AT 50 50. WHEN OTHERS. ENDCASE. ENDFORM.
3.calling selection screen
语法:
CALL SELECTION-SCREEN dynnr
[STARTING
AT col1 lin1
[ENDING
AT col2 lin2]]
[USING
SELECTION-SET variant].
注意:selection screen最好不要用call screen调用。
4.selection screen processing
selection screen有自己的屏幕处理逻辑流。
AT SELECTION-SCREEN OUTPUT:相当于screen PBO;
AT SELECTION-SCREEN ...:相当于screen PAI;
语法:
AT SELECTION-SCREEN selscreen_event.
selscreen_event:
{ OUTPUT }
| { ON {para|selcrit} }
| { ON END OF selcrit }
| { ON BLOCK block }
| { ON RADIOBUTTON GROUP group }
| { }
| { ON {HELP-REQUEST|VALUE-REQUEST} FOR {para|selcrit-low|selcrit-high} }
| { ON EXIT-COMMAND }.
Selection screen屏幕事件
OUTPUT:PBO事件;
LOAD-OF-PROGRAM,INITIALIZATION只在程序加载,初始化执行一次。
ON {para|selcrit}:用户输入检查,可以在此Event,当抛出警告or错误信息,会卡住,可以再次输入。
ON END OF selcrit:select options整体检查。
ON BLOCK block:整个block检查,当抛出警告or错误信息,整个block中元素可以再次输入。
ON RADIOBUTTON GROUP group:整体radiobutton group检查,radio button不会触发on para事件。
{}: AT SELECTION-SCREEN,在整个screen输入检查完毕后触发,可以在此事件一次检查所有元素。
ON {HELP-REQUEST|VALUE-REQUEST}:相当于POH,POV事件,F1,F4帮助。帮助不会传输数据到program,使用function: RS_SELECTIONSCREEN_READ, RS_SELECTIONSCREEN_UPDATE传输。
ON EXIT-COMMAND:cancel,exit,back触发
5. Dynamic Selections
动态selection,使用CL_ABAP_DYN_PRG或者escape检查安全风险。
FREE_SELECTIONS_INIT,FREE_SELECTIONS_DIALOG创建动态selection。
示例:
"动态selection screen CLASS c_dyn_selscreen DEFINITION. PUBLIC SECTION. METHODS:constructor IMPORTING iv_tabname TYPE tabname. METHODS:get_data_show. PRIVATE SECTION. DATA:lv_selid TYPE rsdynsel-selid. DATA:lt_field_tab TYPE TABLE OF rsdsfields. DATA:lt_table_tab TYPE TABLE OF rsdstabs. DATA:ls_table_tab LIKE LINE OF lt_table_tab. DATA:lt_cond_tab TYPE rsds_twhere. DATA:lo_dref TYPE REF TO data. "alv DATA:lo_alv TYPE REF TO cl_salv_table. "tabname DATA:lv_tabname TYPE tabname. METHODS:check_table_exist IMPORTING iv_tabname TYPE tabname RETURNING VALUE(check_tabname) TYPE tabname. ENDCLASS. CLASS c_dyn_selscreen IMPLEMENTATION. "构造 METHOD constructor. "检查table name me->check_table_exist( iv_tabname ). "table ls_table_tab-prim_tab = lv_tabname. APPEND ls_table_tab TO lt_table_tab. CALL FUNCTION 'FREE_SELECTIONS_INIT' EXPORTING kind = 'T' IMPORTING selection_id = lv_selid TABLES tables_tab = lt_table_tab EXCEPTIONS OTHERS = 4. IF sy-subrc <> 0. MESSAGE 'Error in initialization' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDIF. CALL FUNCTION 'FREE_SELECTIONS_DIALOG' EXPORTING selection_id = lv_selid title = 'Free Selection' as_window = ' ' IMPORTING where_clauses = lt_cond_tab TABLES fields_tab = lt_field_tab EXCEPTIONS OTHERS = 4. IF sy-subrc <> 0. MESSAGE 'No free selection created' TYPE 'I'. LEAVE PROGRAM. ENDIF. ENDMETHOD. "获取数据 METHOD get_data_show. FIELD-SYMBOLS <table> TYPE STANDARD TABLE. FIELD-SYMBOLS <cond> LIKE LINE OF lt_cond_tab. READ TABLE lt_cond_tab WITH KEY tablename = lv_tabname ASSIGNING <cond>. IF sy-subrc <> 0. MESSAGE 'Error in condition' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDIF. CREATE DATA lo_dref TYPE TABLE OF (lv_tabname). ASSIGN lo_dref->* TO <table>. TRY. SELECT * FROM (lv_tabname) INTO TABLE <table> WHERE (<cond>-where_tab). CATCH cx_sy_dynamic_osql_error. MESSAGE 'Error in dynamic Open SQL' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDTRY. "show alv TRY. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = <table> ). lo_alv->display( ). CATCH cx_salv_msg. MESSAGE 'Error in ALV display' TYPE 'I' DISPLAY LIKE 'E'. ENDTRY. ENDMETHOD. "检查table是否存在 METHOD check_table_exist. TRY . lv_tabname = cl_abap_dyn_prg=>check_table_name_str( val = iv_tabname packages = 'SAPBC_DATAMODEL' ). CATCH cx_abap_not_a_table. MESSAGE 'Database table not found' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. CATCH cx_abap_not_in_package. MESSAGE 'Only tables from the flight data model are allowed' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDTRY. ENDMETHOD. ENDCLASS.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/14696533.html