标签:
ALV:SAPListViewer
ALV显示格式分为GRID及LIST两种模式,一个以网格显示,另一个以表格显示,两者都有工具栏稍有不同
Layout主要用于设置ALV整体输出格式,为ALV可选项参数。属于类型池 TYPE-POOLS:SLIS
TYPE-POOLS:slis.
DATA: layout TYPE slis_layout_alv.
Layout结构所定义的所有字段如下(各字段详细使用说明,可参考REUSE_ALV_GRID_DISPLAY的 is_layout参数文档说明):
no_colhead(1) type c, " no headings不显示列标题行
no_hotspot(1) type c, " headings not as hotspot
zebra(1) type c, " striped pattern斑马线显示,颜色隔行交替显示
no_vline(1) type c, " columns separated by space不显示垂直网格线
no_hline(1) type c, "rows separated by space B20K8A0N5D不显示水平网格线
cell_merge(1) type c, " not suppress field replication ALV单元格中的内容能够复制到剪贴板
edit(1) type c, " for grid onlyALV是否可编辑,注意只对Grid模式有效,对List模式无效
edit_mode(1) type c, " for grid only
numc_sum(1) type c, " totals for NUMC-Fields possib.
no_input(1) type c, " only display fields
f2code like sy-ucomm, "gs_layout-f2code = ‘&ETA‘.设置触发弹出详细信息窗口的功能码,这里是双击(&ETA)就可以弹出详细窗口,而不需要通过右键菜单
reprep(1) type c, " report report interface active
no_keyfix(1) type c, " do not fix keycolumns
expand_all(1) type c, " Expand all positions
no_author(1) type c, " No standard authority check
* PF-status
def_status(1) type c, " default status space or ‘A‘
item_text(20) type c, " Text for item button
countfname type lvc_fname,
* Display options
colwidth_optimize(1) type c, ALV网格(单元格)宽度设置为自动最优化,按输出内容宽度自动调整
no_min_linesize(1) type c, " line size = width of the list
min_linesize like sy-linsz, " if initial min_linesize = 80
max_linesize like sy-linsz, " Default 250
window_titlebar like sy-title,
no_uline_hs(1) type c,
* Exceptions
lights_fieldname type slis_fieldname," fieldname for exception列显示为红绿灯
lights_tabname type slis_tabname, " fieldname for exception
lights_rollname like dfies-rollname," rollname f. exceptiondocu
lights_condense(1) type c, " fieldname for exception
* Sums
no_sumchoice(1) type c, " no choice for summing up
no_totalline(1) type c, " no total line
no_subchoice(1) type c, " no choice for subtotals
no_subtotals(1) type c, " no subtotals possible
no_unit_splitting type c, " no sep. tot.lines by inh.units
totals_before_items type c, " diplay totals before the items统计行会在数据行前面显示
totals_only(1) type c, " show only totals只显示合计
totals_text(60) type c, " text for 1st col. in total line合计第一列文本
subtotals_text(60) type c, " text for 1st col. in subtotals
* Interaction交互
box_fieldname type slis_fieldname, " fieldname for checkbox指定数据内表中哪列以选择按钮形式显示(首列前可按下或弹上来的按钮),ALV最左上角会出现全选按钮
box_tabname type slis_tabname," tabname for checkbox
box_rollname like dd03p-rollname," rollname for checkbox
expand_fieldname type slis_fieldname, " fieldname flag ‘expand‘
hotspot_fieldname type slis_fieldname, " fieldname flag hotspot
confirmation_prompt, " confirm. prompt when leaving当退出ALV报表展示界面时,是否需要提示用户
key_hotspot(1) type c, " keys as hotspot " K_KEYHOT设置关键字段是否是热点,可单击
flexible_key(1) type c, " key columns movable,...
group_buttons(1) type c, " buttons for COL1 - COL5
get_selinfos(1) type c, " read selection screen
group_change_edit(1) type c, " Settings by user for new group
no_scrolling(1) type c, " no scrolling
* Detailed screen详细屏幕
detail_popup(1) type c, " show detail in popup 右键中有 Detail 菜单,是否弹出详细信息窗口
detail_initial_lines(1) type c, " show also initial lines为空行时单元格内容也会显示在右键弹出的详细框中
detail_titlebar like sy-title," Titlebar for detail设置详细弹出窗口的标题栏
* Display variants
header_text(20) type c, " Text for header button
default_item(1) type c, " Items as default
* colour颜色
info_fieldname type slis_fieldname, " infofield for listoutput指定数据输出内表中哪列存储的是颜色,用来设置ALV每行数据的颜色。注:使用属性需要同时在数据内表中定义一个与该参数所定义字段名相同的栏位,如:LAYOUT-INFO_FIELDNAME=’COLOR’,假设数据内表名为LT_OUT,则需要在该内表增加一个栏们“COLOR”,颜色范围 C000~C999
coltab_fieldname type slis_fieldname, " colors
* others
list_append(1) type c, " no call screen
xifunckey type aqs_xikey, " eXtended interaction(SAPQuery)
xidirect type flag, " eXtended INTeraction(SAPQuery)
dtc_layout type dtc_s_layo, "Layout for configure the Tabstip
allow_switch_to_list(1) type c, "ACC: Switch from FullGrid to List
Fieldcat主要用于ALV数据显示结构的定义,包括具体的栏位名称、类型、各字段的输出格式(与Layout不一样的是输出格式只针对某个字段),为ALV必选参数
TYPE-POOLS:slis.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
Fieldcat结构所定义的所有字段如下(各字段详细使用说明,可参考REUSE_ALV_GRID_DISPLAY的 it_fieldcat参数文档说明):
row_pos like sy-curow, " output in row
col_pos like sy-cucol, " position of the column列的输出位置字段在表中第几列
fieldname type slis_fieldname,"针对输出内表哪列进行设置,只有设置了的列才会显示,如果没有设置,则不会显示在ALV中。如果此字段是CURR(currency field) ,QUAN(Quantity field) 需要指定相应的CUKY, UNIT字段.需设置Cfieldname Ctabname 和Qfieldname Qtabname
tabname type slis_tabname,
currency(5) type c, TCURX表中的货币名称
cfieldname type slis_fieldname, " field with currency unit金额字段所参照的货币单位字段名称(货币单位字段为数据内表中的某列?)
ctabname type slis_tabname, " and table
ifieldname type slis_fieldname, " initial column
quantity(3) type c,计量单位
qfieldname type slis_fieldname, " field with quantity unit
qtabname type slis_tabname, " and table
roundtypei, " round in write statement四舍五入至小数点后多少位
exponent(3) type c, " exponent for floats浮点数的幂指数为多少位
key(1) type c, " column with key-color指定字段是否是关键字段,如果是则单元格显示的颜色会不同,并会靠前显示
icon(1) type c, " as icon字段将以图标形式显示,INCLUDE <list> ,图标字段内容来自list Inc.文件
symbol(1) type c, " as symbol字段内容作为符号输出,与上面的ICON属性相似
checkbox(1) type c, " as checkbox字段将以复选框形式显示
just(1) type c, " (R)ight (L)eft (C)ent.单元格中内容显示时对齐方式。不设置时按钮数据类型默认对齐方式来对齐
lzero(1) type c, " leading zero 为X时输出前导零
no_sign(1) type c, " write no-sign 不显示数字符号
no_zero(1) type c, " write no-zero 只输出有意义的值,空值不输出。为X时全为零(如:00000)时不输出,所以不输出零时应该最好同时设置lzero = sapce与no_zero = X,相反如果要输出,则应同时设置lzero = X 与no_zero = space
no_convext(1) type c,
edit_mask type slis_edit_mask, "对字段内容进行掩码格式化输出,具体请参照《ABAP BC Programming.docx》中的WRITE … TO章节,如:gt_fieldcat-edit_mask = ‘____/__/__‘可对日期字段都日期格式化显示
emphasize(4) type c, " emphasize设置列的颜色
fix_column(1) type c, " Spalte fixieren列固定不滚动,与Key属性相似,但颜色不会发生变化
do_sum(1) type c, " sum up该列是否进行小计,需与gt_sort-subtot一起使用(即需要参考排序),否则只对整列进行一个合计
no_out(1) type c, " (O)blig.(X)no out但注意用户可以change layout(布局设置)来显示出此列
tech(1) type c, " technical field该字段为技术字段:设置为技术列的列将不会再显示出来,即使是在布局设置里也是看不到的
outputlen like dd03p-outputlen,列的字符输出宽度
offset type dd03p-outputlen, " offset
调整输出列抬头的文本:文本可以随着用户调整列的宽度而变化,其文本即是在对应data element中定义的,如果需要定制,则可以指定以下字段
seltext_l like dd03p-scrtext_l, " long key word标题字段显示的名称(长)
seltext_m like dd03p-scrtext_m, " middle key word标题字段显示的名称(中)
seltext_s like dd03p-scrtext_s, " short key word标题字段显示的名称(短)
ddictxt(1) type c, " (S)hort (M)iddle (L)ong设置以长、中还是短名称来显示,取值分别为 S、M、L。直接指定文本显示为长文本、中、还是短文本, 指定这个字段后则会固定下来,不会随着用户的宽度调整变化.
reptext_ddic like dd03p-reptext, " heading (ddic),(header) Analogous(相似) to the Data element main header
rollname like dd03p-rollname,F1帮助需参照的DataElement,在ALV的显示界面将鼠标放到该字段的位置后按F1会弹出该字段的说明。另外,可以不指明字段的描述(如seltext_l、seltext_m、seltext_s),函数会自动将字段的描述显示为该参照的数据元素
下面datatype两个属性在字段如果是金额或P小数(数量)类型时,需要对datatype此属性进行设置,否则,如果不设置时,在修改对应单元格内容时,会自动将你所输入的数除100,即小数点提前两位;并且如果是数量类型,除了设置datatype外,inttype也需要进行设置,且为 C,具体实现请参考这里
datatype like dd03p-datatype,
inttype like dd03p-inttype,
intlen like dd03p-intlen,
lowercase like dd03p-lowercase,
ref_fieldname like dd03p-fieldname,"如需单元格显示F4输入帮助,则需要指定字段所参照的表名
ref_tabname like dd03p-tabname,"如需单元格显示F4输入帮助,则需要指定字段所参照的表中的字段名
roundfieldname type slis_fieldname,
roundtabname type slis_tabname,
decimalsfieldname type slis_fieldname,
decimalstabname type slis_tabname,
decimals_out(6) type c, " decimals in write statement控制小数点的位数
text_fieldname type slis_fieldname,
ddic_outputlen like dd03p-outputlen,
key_sel(1) type c, " field not obligatory
no_sum(1) type c, " do not sum up
sp_group(4) type c, " group specification
reprep(1) type c, " selection for rep/rep
input(1) type c, " input
edit(1) type c, " internal use only是否可编辑
hotspot(1) type c, " hotspot设置字段内容下面是否有热点(有下划线,可点击,单击即可触发相应事件)
REUSE_ALV_GRID_DISPLAY:Grid 格式的ALV
REUSE_ALV_LIST_DISPLAY: LIST格式的 ALV
REUSE_ALV_FIELDCATALOG_MERGE:根据程序中的数据内表结构,来自动生成FIELDCAT[]内表,会根据内表结构所参照的词典类型来自动完成如表标题字段名的生成,得到大概的FIELDCAT[]后,可以再根据特殊情况再修改FIELDCAT[]内表,这样可以大大减少FIELDCAT[]内表的创建工作。注:程序中用来输出的内表结构中的每个字段都要参考了数据字典中的Data element,否则无法获取字段的相关信息,此时在自动构建FIELDCAT后再手动对无参照字典类型的字段进行手工设置
注:还有一种以 LVC结尾的ALV函数REUSE_ALV_GRID_DISPLAY_LVC,此函数中引用到的类型大部分都不再从类型池slis 中来引用(TYPE-POOLS:slis.),而是直接引用字典中已定义好的表或结构类型,这种函数属于新性函数,与面向对象的CL_GUI_ALV_GRID生成的ALV参数类型上基本相同,所以以后一般如果使用函数方式来产生ALV,推荐使用REUSE_ALV_GRID_DISPLAY_LVC函数,而非REUSE_ALV_GRID_DISPLAY函数因为这样方便修改面向对象方式的ALV
REUSE_ALV_GRID_DISPLAY与REUSE_ALV_LIST_DISPLAY函数的参数基本相同:
CALLFUNCTION‘REUSE_ALV_GRID_DISPLAY‘ | CALLFUNCTION‘REUSE_ALV_LIST_DISPLAY‘ |
TYPE-POOLS:slis.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA:BEGIN OF lt_stock OCCURS 0,
sel,"用来做选择按钮列
checkbox,"用作复选框列
werks LIKE mard-werks,"工厂
matnr LIKE mara-matnr,"物料
labst LIKE mard-labst,"库存
int TYPE i,"用来测试统计的自定义字段
color(4),"用来存储颜色
END OF lt_stock.
INITIALIZATION.
PERFORM inital.
START-OF-SELECTION.
DEFINE fill_fdcat.
clear fieldcat.
fieldcat-fieldname = &1. "需要显示的内表行结构中的哪个字段
fieldcat-seltext_l = &2. "ALV显示时标题栏中的文本
fieldcat-key = &3. "是否是关键字段,如果是则单元格显示的颜色会不同,并会靠前显示
append fieldcat.
END-OF-DEFINITION.
fill_fdcat ‘WERKS‘ ‘工厂‘ ‘X‘.
fill_fdcat ‘MATNR‘ ‘物料‘ ‘X‘.
fill_fdcat ‘LABST‘ ‘库存‘ ‘‘.
fill_fdcat ‘INT‘ ‘自定义字段‘ ‘‘.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
it_fieldcat = fieldcat[]
TABLES
t_outtab = lt_stock.
FORM inital .
lt_stock-sel = ‘X‘.
lt_stock-checkbox = ‘X‘.
lt_stock-werks = ‘1001‘.
lt_stock-matnr = ‘1001M1‘.
lt_stock-labst = 4.
lt_stock-int = 2.
lt_stock-color = ‘C100‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-sel = ‘X‘.
lt_stock-werks = ‘1001‘.
lt_stock-matnr = ‘1001M2‘.
lt_stock-labst = 3.
lt_stock-int = 1.
lt_stock-color = ‘C200‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-checkbox = ‘X‘.
lt_stock-werks = ‘1002‘.
lt_stock-matnr = ‘1001M1‘.
lt_stock-labst = 2.
lt_stock-int = 3.
lt_stock-color = ‘C300‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-werks = ‘1002‘.
lt_stock-matnr = ‘1001M2‘..
lt_stock-labst = 1.
lt_stock-int = 1.
lt_stock-color = ‘C300‘.
APPEND lt_stock .
ENDFORM.
注:通过此种方式实现时,显示内表中的所有列都会输出,只是那些没有参照字典中的类型字段没有字段标题名而已,这需要在FieldCat生产后,通过代码修改即可。
REPORT zalv_test.
TYPE-POOLS:slis.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: layout TYPE slis_layout_alv.
DATA:BEGIN OF lt_stock OCCURS 0,
sel,"用来做选择按钮列
checkbox,"用作复选框列
werks LIKE mard-werks,"工厂
matnr LIKE mara-matnr,"物料
labst LIKE mard-labst,"库存
int TYPE i,"用来测试统计的自定义字段
color(5),"用来存储颜色
END OF lt_stock.
INITIALIZATION.
PERFORM inital.
START-OF-SELECTION.
PERFORM build_fieldcat.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
it_fieldcat = fieldcat[]
TABLES
t_outtab = lt_stock.
FORM build_fieldcat .
DATA: descr_ref TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <comp_output> TYPE abap_compdescr.
DATA: absname TYPE string.
DATA: ref_datadesrc TYPE REF TO cl_abap_datadescr.
DATA: BEGIN OF title ,
rollname TYPE dd04l-rollname,
scrtext_s TYPE dd04t-scrtext_s,
scrtext_m TYPE dd04t-scrtext_m,
scrtext_l TYPE dd04t-scrtext_l,
END OF title.
"lt_stock为带表头的内表,这里传递的是表头,在使用时修改这里即可
descr_ref ?= cl_abap_typedescr=>describe_by_data( lt_stock ).
LOOP AT descr_ref->components ASSIGNING <comp_output>.
ref_datadesrc = descr_ref->get_component_type( <comp_output>-name ).
absname = ref_datadesrc->absolute_name.
SPLIT absname AT `=` INTO absname absname.
CLEAR:title.
SELECT SINGLE dd04l~rollname dd04t~scrtext_s dd04t~scrtext_m dd04t~scrtext_l
INTO CORRESPONDING FIELDS OF title
FROM dd04l INNER JOIN dd04t ON dd04l~rollname = dd04t~rollname
WHERE ddlanguage = sy-langu AND dd04l~rollname = absname.
fieldcat-fieldname = <comp_output>-name.
* 如果fieldcat定义为:data: fieldcat type table of lvc_s_fcat(不过此时不能再使用REUSE_ALV_GRID_DISPLAY函数了,还是使用以LVC结尾的REUSE_ALV_GRID_DISPLAY_LVC函数,否则参数类型不匹).使用下面语句:
* fieldcat-reptext = title-scrtext_l.
* fieldcat-scrtext_l = title-scrtext_l.
* fieldcat-scrtext_m = title-scrtext_m.
* fieldcat-scrtext_s = title-scrtext_s.
* 如果fieldcat定义为:data: fieldcat type slis_t_fieldcat_alv with header line.使用下面语句进行设置:
fieldcat-seltext_l = title-scrtext_l.
fieldcat-seltext_m = title-scrtext_m.
fieldcat-seltext_s = title-scrtext_s.
APPEND fieldcat.
ENDLOOP.
ENDFORM. "build_fieldcatalog
FORM inital .
lt_stock-sel = ‘X‘.
lt_stock-checkbox = ‘X‘.
lt_stock-werks = ‘1001‘.
lt_stock-matnr = ‘1001M1‘.
lt_stock-labst = 4.
lt_stock-int = 2.
lt_stock-color = ‘C100‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-sel = ‘X‘.
lt_stock-werks = ‘1001‘.
lt_stock-matnr = ‘1001M2‘.
lt_stock-labst = 3.
lt_stock-int = 1.
lt_stock-color = ‘C200‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-checkbox = ‘X‘.
lt_stock-werks = ‘1002‘.
lt_stock-matnr = ‘1001M1‘.
lt_stock-labst = 2.
lt_stock-int = 3.
lt_stock-color = ‘C300‘.
APPEND lt_stock .
CLEAR:lt_stock.
lt_stock-werks = ‘1002‘.
lt_stock-matnr = ‘1001M2‘..
lt_stock-labst = 1.
lt_stock-int = 1.
lt_stock-color = ‘C300‘.
APPEND lt_stock .
ENDFORM.
TYPE-POOLS:slis.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: layout TYPE slis_layout_alv.
"注:如果是参照字典中的structure, table, view中的某个字段时,字段的定义只能使用
"LIKE操作符,否则使用TYPE时,该字段在使用 REUSE_ALV_FIELDCATALOG_MERGE
"函数时将被忽略,但不参照字典类型的除外。另外,内表本身的定义只能采用老式方式
"来定义,不能采用新方式定义,否则不能被REUSE_ALV_FIELDCATALOG_MERGE找到
DATA:BEGIN OF T_STOCK OCCURS 0,
sel,"用来做选择按钮列
checkbox,"用作复选框列
werks LIKE mard-werks,"工厂
matnr LIKE mara-matnr,"物料
labst LIKE mard-labst,"库存
int TYPE i,"用来测试统计的自定义字段
color(5),"用来存储颜色
END OF T_STOCK.
INITIALIZATION.
PERFORM inital.
START-OF-SELECTION.
*REUSE_ALV_FIELDCATALOG_MERGE函数内部实现规则:
*
* 数据类型为 clnt 字典类型字段不是显示,即 fieldcat-no_out = space ,如果
* 需要显示,则可以设置REUSE_ALV_FIELDCATALOG_MERGE参数i_client_never_display
* 来显示
* 如果参照的字典structure, table, view类型中的某字段是关键字然,则会设置
* fieldcat-key = ‘X‘
* dictionary references to unit fields are copied if the reference
* fields are in the structure.
CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE‘
EXPORTING
i_program_name = sy-repid
i_inclname = sy-repid"一般指定为本程序名,如果显示内表参照的为字典中的类型则可省略
i_internal_tabname = ‘T_STOCK‘ "显示输出内表名,要大小
"如果定义的显示输出内表是参照的字典中的structure, table, view时,才需要指定
* i_structure_name =
i_client_never_display = ‘X‘"类型为CLNT字典类型的字段将不会被设置,即不会显示
i_bypassing_buffer = ‘X‘"另外,此函数还有缓存问题,发现在修改输出内表显示结构后,此函数不会刷新,加了以下这两个参数还是一样,只能将显示输出内表的变量名修改一下,不知道如何做才好啊
i_buffer_active = ‘ ‘
CHANGING
ct_fieldcat = fieldcat[].
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
it_fieldcat = fieldcat[]
TABLES
t_outtab = T_STOCK.
FORM inital .
T_STOCK-sel = ‘X‘.
T_STOCK-checkbox = ‘X‘.
T_STOCK-werks = ‘1001‘.
T_STOCK-matnr = ‘1001M1‘.
T_STOCK-labst = 4.
T_STOCK-int = 2.
T_STOCK-color = ‘C100‘.
APPEND T_STOCK .
CLEAR:T_STOCK.
T_STOCK-sel = ‘X‘.
T_STOCK-werks = ‘1001‘.
T_STOCK-matnr = ‘1001M2‘.
T_STOCK-labst = 3.
T_STOCK-int = 1.
T_STOCK-color = ‘C200‘.
APPEND T_STOCK .
CLEAR:T_STOCK.
T_STOCK-checkbox = ‘X‘.
T_STOCK-werks = ‘1002‘.
T_STOCK-matnr = ‘1001M1‘.
T_STOCK-labst = 2.
T_STOCK-int = 3.
T_STOCK-color = ‘C300‘.
APPEND T_STOCK .
CLEAR:T_STOCK.
T_STOCK-werks = ‘1002‘.
T_STOCK-matnr = ‘1001M2‘..
T_STOCK-labst = 1.
T_STOCK-int = 1.
T_STOCK-color = ‘C300‘.
APPEND T_STOCK .
ENDFORM.
默认输出的ALV已供了很多标准的按钮了:
自定义ALV工具栏有两种方式:第一处是自已创建一个GUI Status(通常作法是从SLVC_FULLSCREEN函数组中的拷贝STANDARD_FULLSCREENGui Status,再在此基础之上新增按钮即可);第二种是调用系统已定义好的标准ALV工具栏;
系统已定义好的标准ALV GUI Status 在程序SAPLKKBL中,可以通过SET PF-STATUS … OF PROGRAM语句直接来调用,如:
SET PF-STATUS ‘STANDARD_FULLSCREEN‘ OF PROGRAM ‘SAPLKKBL‘.
SAPLKKBL程序中定义的系统标准的ALV GUI Status有以下几种:
l STDPOPUP_FULLSCREEN
l STANDARD_FULLSCREEN 与SLVC_FULLSCREEN函数组中的STANDARD_FULLSCREEN是一样的,都是标准的(函数组SALV中的STANDARDstatus也是标准的)
l STDPOPBX_FULLSCREEN
l STDPOPUP_FULLSCREEN
l STANDARD_FULLSCR_HR
l STDPOPBX_FS_LIGHT
l STDPOPUP_FS_LIGHT
l STD_LIGHT_FULLSCREEN
这些工具条中的按钮都已经设置了Function Code,且都是保留字,这些Funcode都已经在ALV Guid控件中实现,不需得新分配Funcode功能代码,按钮默认功能码:
ALV中自定义工具栏需要通过REUSE_ALV_GRID_DISPLAY函数的I_CALLBACK_PF_STATUS_SET参数,传递设置GUI Status的Form的名称,在此Form使用SET PF-STATUS语句来重新定义ALV工具栏,该Form接口如下:
FORM set_pf_status USING rt_extab TYPE slis_t_extab
rt_extab是要排除掉的按钮,根据Funcode来排除。另外,也可以通过REUSE_ALV_GRID_DISPLAY函数IT_EXCLUDING内表参数来直接排除哪些预置按钮不可用,IT_EXCLUDING内表结构只有一个fcode字段,即根据保留的FunCode即可disabled掉。
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fieldcat[]
I_CALLBACK_PF_STATUS_SET = ‘SET_PF_STATUS‘
TABLES
t_outtab = t_stock1.
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
"去激活Function code为&ETA的详情按钮
APPEND ‘&ETA‘ TO rt_extab.
SET PF-STATUS ‘STANDARD_FULLSCREEN‘ OF PROGRAM ‘SAPLKKBL‘ EXCLUDING
rt_extab.
ENDFORM.
如果没有重定义ALV工具栏,则会默认使用函数组SLVC_FULLSCREEN中定义的STANDARD_FULLSCREEN工具条
在自定义工具栏时,通常从标准GUI Status上拷贝出来,再新加按钮。
函数组 SLVC_FULLSCREEN中定义的STANDARD_FULLSCREEN GUI Status拷贝方式如下,通过 SE41可以快速拷贝:
如果不是ALV,是Write输出时,拷贝:
ALV触发的事件(如双击某行数据、单击热点、点ALV工具栏上的自定义的按钮时),会回调I_CALLBACK_USER_COMMAND参数指定的Form,接口如下:
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
Parameter R_UCOMM 为触发的function code
结构 RS_SELFIELD 包括了以下字段:
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fieldcat[]
I_CALLBACK_USER_COMMAND = ‘USER_COMMAND‘
TABLES
t_outtab = gt_data[].
注:只有自已新增的按钮(或双击数据行时、热点点击)再会调用,ALV 标准默认的Function Code是不会调用的(除非使用使用IT_EVENT_EXIT)
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
MESSAGE i001(00) WITH ‘事件触发时当前行:‘ rs_selfield-tabindex.
ENDFORM.
ALV可触发的事件可用REUSE_ALV_EVENTS_GET 函数来获得,输出一个内表,类型是slis_t_event,行项为slis_alv_event,包含两个字段:一个为事件名,另一个是事件处理的FORM名
DATA: i_events TYPE slis_t_event WITH HEADER LINE.
CALL FUNCTION ‘REUSE_ALV_EVENTS_GET‘
EXPORTING
"0:表示获取的是通过函数REUSE_ALV_LIST_DISPLAY产生的ALV所支持的事件
"4:表示获取的是通过函数REUSE_ALV_GUID_DISPLAY产生的ALV所支持的事件
i_list_type = 4
IMPORTING
et_events = i_events[].
LOOP AT i_events.
WRITE:/ i_events-name,i_events-form.
ENDLOOP.
触发USER_COMMAND事件时,会调用以下签名的Form:
触发PF_STATUS_SET事件时,会调用以下签名的Form(其中pf_status_set名是自定义的,非固定为 pf_status_set):
FORM pf_status_set USING rt_extab TYPE slis_t_extab.
事件名 | 何时触发 | 触发时所回调Form的签名要求 | 回调方式 |
USER_COMMAND | 用户点击工具栏中自定义按钮、预置按钮(需通过REUSE_ALV_GRID_DISPLAY函数的IT_EVENT_EXIT参数来特殊处理才会回调指定的Form)、数据行双击、单元格热点点击等一系列用户操作 | FORM user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield. (其中user_command名是自定义的,而不是固定为 user_command) | 1. 通过REUSE_ALV_GRID_DISPLAY函数的I_CALLBACK_USER_COMMAND参数接口回调 2. 通过REUSE_ALV_GRID_DISPLAY函数的IT_EVENTS参数接口回调 |
PF_STATUS_SET | ALV工具栏显示前,可自定义工具条 | FORM pf_status_set USING rt_extab TYPE slis_t_extab (其中 pf_status_set 名是自定义的,而不是固定为 pf_status_set ) | 1. 通过REUSE_ALV_GRID_DISPLAY函数的I_CALLBACK_PF_STATUS_SET参数接口回调 2. 通过REUSE_ALV_GRID_DISPLAY函数的IT_EVENTS参数接口回调 |
types: slis_t_event type slis_alv_event occurs 0.
*--- Structure for event handling
types: begin of slis_alv_event,
name(30),"事件名
form(30),"事件触发时需回调的Form名
end of slis_alv_event.
点击ALV默认提供的工具栏按钮时,不会回调I_CALLBACK_USER_COMMAND = ‘USER_COMMAND‘参数中指定的Form ,因为那些按钮所对应的功能代码已被设为保留Funcode了,如果要想在点击这些标准的按钮时也回调指定的Form,可通过REUSE_ALV_GRID_DISPLAY函数的IT_EVENT_EXIT 参数实现的,可以向IT_EVENT_EXIT参数内表填充需要被拦截的保留Funcode,及在是执行对应功能代码之前还是之后调用,具体应用请参考这里:
DATA: event_exit TYPE slis_t_event_exit WITH HEADER LINE.
event_exit-ucomm = ‘&OAD‘."此Funcode为点击AlV工具栏上的选择布局按钮时会被USER_COMMAND Form拦截
event_exit-after = ‘X‘.
APPEND event_exit.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fieldcat[]
i_callback_user_command = ‘USER_COMMAND‘
IT_EVENT_EXIT = event_exit[]
TABLES
t_outtab = gt_result.
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
...
ENDFORM.
注意:
颜色设置中有优先级顺序,他们是单元格-->行-->列,即若同时使用了上述3中更改颜色的方法,则列的颜色会被行的颜色覆盖掉,而行的颜色又会被单元格的颜色覆盖掉,最终只会显示出单元格的颜色。
如果这列被设置为关键列,就是LS_FCAT-KEY = ‘X‘ ,那么颜色设置就不会起作用
ALV中的每行数据颜色是通过LayOut来控制的。需要在显示输出内表结构中增加一列字段,用来存储数据行的颜色
TYPE-POOLS:slis.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: gs_layout TYPE slis_layout_alv.
DATA:BEGIN OF gt_data OCCURS 0,
val1(4),"颜色值
val2(4),"颜色值
color(4),"用来存储行颜色
END OF gt_data.
START-OF-SELECTION.
PERFORM inital.
DEFINE fill_fdcat.
clear gt_fieldcat.
gt_fieldcat-fieldname = &1.
gt_fieldcat-seltext_l = &2.
append gt_fieldcat.
END-OF-DEFINITION.
fill_fdcat ‘VAL1‘ ‘颜色值‘.
fill_fdcat ‘VAL2‘ ‘颜色值‘.
gs_layout-info_fieldname = ‘COLOR‘." 指定哪一列用来作为行颜色的列
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
it_fieldcat = gt_fieldcat[]
is_layout = gs_layout
TABLES
t_outtab = gt_data.
FORM inital .
DATA: c1,c2,c3.
DO 8 TIMES.
c1 = sy-index - 1.
DO 2 TIMES.
c2 = sy-index - 1.
DO 2 TIMES.
c3 = sy-index - 1.
CONCATENATE ‘C‘ c1 c2 c3 INTO gt_data-color.
gt_data-val1 = gt_data-color.
gt_data-val2 = gt_data-color.
APPEND gt_data.
ENDDO.
ENDDO.
ENDDO.
ENDFORM.
颜色值定义为4位字符,首位固定为字母“C”,第2位为颜色,由0~7表示,不同的数字表示不同的颜色属性,如:
0 = background color 1 = Gray-blue 2 = Light gray 3 = yellow
4 = blue-gray 5 = green 6 = red 7 = orange
第3位表示输出文字是否高亮显示,由0~1表示,为1时表示高亮显示。第4位测试了一下,基本上0~9颜色都差不多,唯一就是当取值为1时,底色又回到了灰色(且只是在第3位为0时才有此效果)。
ALV中的颜色是由4位数组成:‘C‘+COL+INT+INV
*** Structure for colors
types: begin of slis_color,
col type i,
int type i,
inv type i,
end of slis_color.
第1位:固定取值为C
第2位COL:颜色值,取值为0~7
第3位INT:高亮,即颜色是否加深,取值为0、1。1表示加深显示
第4位INV:颜色是否反转,即颜色是作用在背景上,还是作用在输出字符上,取值上为0、1。为1时表示设置的是前景色,即输出字符本身的颜色(好像只有在第3位为0时才有效?)
以下是各种颜色值测试表:
TYPE-POOLS:slis.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: gt_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE.
DATA:BEGIN OF gt_data OCCURS 0,
curr TYPE ekpo-netpr,
int TYPE i,
p TYPE p LENGTH 5 DECIMALS 2,
END OF gt_data.
START-OF-SELECTION.
PERFORM inital.
DEFINE fill_fdcat.
clear gt_fieldcat.
gt_fieldcat-fieldname = &1.
gt_fieldcat-seltext_l = &2.
if &1 = ‘CURR‘ .
"设置列颜色
gt_fieldcat-emphasize = ‘C510‘.
endif.
if &1 = ‘INT‘ .
gt_fieldcat-emphasize = ‘C610‘.
endif.
append gt_fieldcat.
END-OF-DEFINITION.
fill_fdcat ‘CURR‘ ‘CURR‘ .
fill_fdcat ‘INT‘ ‘INT‘ .
fill_fdcat ‘P‘ ‘P(3.2)‘ .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = sy-repid
it_fieldcat = gt_fieldcat[]
TABLES
t_outtab = gt_data[].
FORM inital .
gt_data-curr = 1.
gt_data-int = 1.
gt_data-p = 1.
APPEND gt_data.
gt_data-curr = 2.
gt_data-int = 2.
gt_data-p = 2.
APPEND gt_data.
ENDFORM.
除了可为整行设置行颜色外,每个单元格(不只是整列)的颜色也是可以单独进行设置。当然既然可以给单元格设置颜色,则整列相同颜色也是可以采用此方式来设置的
TYPE-POOLS:slis.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: gs_layout TYPE slis_layout_alv.
DATA:BEGIN OF gt_data OCCURS 0,
val1(40),"颜色值
val2(40),"颜色值
val3(40),
colortable TYPE slis_t_specialcol_alv, "每行中的单元格颜色内表,可对多个单元格进行不同颜色设置
END OF gt_data.
"每行中单元格颜色列表
DATA: gt_color TYPE slis_t_specialcol_alv WITH HEADER LINE.
START-OF-SELECTION.
PERFORM inital.
DEFINE fill_fdcat.
clear gt_fieldcat.
gt_fieldcat-fieldname = &1.
gt_fieldcat-seltext_l = &2.
append gt_fieldcat.
END-OF-DEFINITION.
fill_fdcat ‘VAL1‘ ‘颜色值‘.
fill_fdcat ‘VAL2‘ ‘颜色值‘.
fill_fdcat ‘VAL3‘ ‘无颜色‘.
gs_layout-coltab_fieldname = ‘COLORTABLE‘."设置输出内表中哪列为颜色列表
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
it_fieldcat = gt_fieldcat[]
is_layout = gs_layout
TABLES
t_outtab = gt_data.
FORM inital .
DATA: c1,c2.
DO 7 TIMES.
c1 = gt_color-color-col = sy-index - 1."颜色值
DO 2 TIMES.
c2 = gt_color-color-int = sy-index - 1."颜色亮度
gt_color-color-inv = ‘0‘."颜色作用于背景
CASE sy-index .
WHEN 1.
gt_color-fieldname = ‘VAL1‘."需进行颜色设置的列名(已具体到单元格)
CONCATENATE ‘col=‘ c1 ` ` ‘int=‘ c2 ` ` ‘inv=0‘ INTO gt_data-val1 .
WHEN 2.
gt_color-fieldname = ‘VAL2‘."需进行颜色设置的列名(已具体到单元格)
CONCATENATE ‘col=‘ c1 ` ` ‘int=‘ c2 ` ` ‘inv=0‘ INTO gt_data-val2 .
ENDCASE.
APPEND gt_color.
ENDDO.
"设置每行数据中单元格的颜色内表
gt_data-colortable = gt_color[].
gt_data-val3 = ‘无颜色‘.
APPEND gt_data.
CLEAR:gt_color,gt_color[].
ENDDO.
ENDFORM.
可以通过layout(slis_layout_alv)中的edit参数来设置整个网格(所有单元格)是否可编辑:
DATA: gs_layout TYPE slis_layout_alv.
gs_layout-edit = ‘X‘.
如果只想让某列(一整列)可修改,而不是整个网格,则可对fieldcat中的edit参数来设置某列是否可编辑:
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
gt_fieldcat-edit = ‘X‘.
如果想对某些单元格进行设置,在满足一定条件的单元格才能修改,此时只能使用以LVC结尾的REUSE_ALV_GRID_DISPLAY_LVC函数,而非REUSE_ALV_GRID_DISPLAY函数:
"TYPE-POOLS:slis.
DATA: gt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE."注:此时的fieldcat不再引用slis_t_fieldcat_alv类型
DATA: gs_layout TYPE lvc_s_layo."注:此时的Layout不再是引用slis_layout_alv类型
DATA:BEGIN OF gt_data OCCURS 0,
val1(40),
val2(40),
cellstab TYPE lvc_t_styl,"单元格可编辑控制
END OF gt_data.
"单元格可编辑控制内表
DATA: gt_cellstab TYPE lvc_t_styl WITH HEADER LINE.
START-OF-SELECTION.
PERFORM inital.
DEFINE fill_fdcat.
clear gt_fieldcat.
gt_fieldcat-fieldname = &1.
gt_fieldcat-scrtext_l = &2.
"设置单元格为可编辑状态:此处设置所有单元格都可编辑
gt_fieldcat-edit = ‘X‘.
append gt_fieldcat.
END-OF-DEFINITION.
fill_fdcat ‘VAL1‘ ‘列1‘.
fill_fdcat ‘VAL2‘ ‘列2‘.
gs_layout-stylefname = ‘CELLSTAB‘."设置可编辑信息
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC‘
EXPORTING
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat[]
TABLES
t_outtab = gt_data.
FORM inital .
gt_data-val1 = ‘cell1‘.
gt_data-val2 = ‘cell2‘.
gt_cellstab-fieldname = ‘VAL1‘.
"将原本可编辑的单元格切换到不可编辑样式。注:这里
"只是样式的切换,不能仅仅使用cl_gui_alv_grid=>mc_style_enabled来将单元格设置为
"可编辑状态,单元格真正是否可编辑是由fieldcat-edit或layout-edit来决定的,而仅
"设置为cl_gui_alv_grid=>mc_style_enabled是不可编辑的
gt_cellstab-style = cl_gui_alv_grid=>mc_style_disabled.
APPEND gt_cellstab.
gt_data-cellstab = gt_cellstab[].
APPEND gt_data.
ENDFORM.
标签:
原文地址:http://www.cnblogs.com/jiangzhengjun/p/4291337.html