通过CL_SALV_TABLE ALV的显示调用
*&---------------------------------------------------------------------*
*& REPORT ZDEMO172525
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
*& REPORT
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& 修改日志:
*& 日志号 修改人 修改时间 传输号码 修改描述
*& ----- ----- -------- -------- --------
*& 001 XXX
*&---------------------------------------------------------------------*
report zdemo172525 .
************************************************************************
*标准表声明
************************************************************************
tables:marc.
class l_cl_handle_events definition.
public section.
methods: on_double_click for event double_click of cl_salv_events_table
importing
row "事件触发所在的行号
column."事件触发所在的列名
endclass.
class l_cl_handle_events implementation.
method on_double_click.
perform show_cell_info using row column 'is selected'.
endmethod.
endclass.
************************************************************************
*类型定义
************************************************************************
types:begin of ty_list,
matnr type marc-matnr,
werks type marc-werks,
bstrf type marc-bstrf,
basmg type marc-basmg,
color type lvc_t_scol,
end of ty_list.
************************************************************************
*变量声明
************************************************************************
data: gt_list type standard table of ty_list, "ALV显示的内表
gs_list type ty_list. "工作区
data: gt_fieldcat type standard table of lvc_s_fcat, "ALV所展示字段的存储结构
gs_fieldcat type lvc_s_fcat,
gs_layout type lvc_s_layo. "GS_LAYOUT是ALV属性的存储结构
************************************************************************
* 选择屏幕
************************************************************************
selection-screen:begin of block b1 with frame title text-100.
select-options: s_werks for marc-werks.
parameters:p_but10 radiobutton group g1 .
parameters:p_but20 radiobutton group g1 default 'X' .
parameters:p_but21 radiobutton group g1 .
parameters:p_but22 radiobutton group g1 .
parameters:p_but23 radiobutton group g1 .
parameters:p_but30 radiobutton group g1 .
selection-screen:end of block b1.
*SELECTION-SCREEN:FUNCTION KEY 1.
************************************************************************
* 初始化数据赋值
************************************************************************
initialization.
************************************************************************
* 选择屏幕数据检查
************************************************************************
at selection-screen.
**************************************************************************
*选择屏幕事件
**************************************************************************
at selection-screen output.
**************************************************************************
*数据获取
**************************************************************************
start-of-selection.
perform frm_get_data.
**************************************************************************
*ALV展示
**************************************************************************
end-of-selection.
case 'X'.
when p_but10.perform frm_show_alv.
when p_but20.perform frm_salv20.
when p_but21.perform frm_salv21.
when p_but22.perform frm_salv22.
when p_but23.perform frm_salv23.
when p_but30.
when others.
endcase.
*&---------------------------------------------------------------------*
*& FORM FRM_SHOW_ALV
*&---------------------------------------------------------------------*
*& ALV展示
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_show_alv .
perform frm_set_fieldcat. "ALV界面展示字段添加
perform frm_set_layout. "ALV全局属性定义
perform frm_display_alv. "调用ALV函数显示数据
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_FIELDCAT
*&---------------------------------------------------------------------*
*& 展示字段
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_set_fieldcat .
refresh:gt_fieldcat.
define fieldcatset.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = &1. "字段名称
GS_FIELDCAT-REF_FIELD = &2. "参考字段
GS_FIELDCAT-REF_TABLE = &3. "参考表
GS_FIELDCAT-EDIT = &4. "是否可编辑
GS_FIELDCAT-ICON = &5. "图标
GS_FIELDCAT-EMPHASIZE = &6. "带有颜色的高亮列
GS_FIELDCAT-HOTSPOT = &7. "单击敏感
GS_FIELDCAT-COLTEXT = &8. "字段显示名称
GS_FIELDCAT-SCRTEXT_L = &8. "列长文本
GS_FIELDCAT-SCRTEXT_M = &8. "列中文本
GS_FIELDCAT-SCRTEXT_S = &8. "列短文本
APPEND GS_FIELDCAT TO GT_FIELDCAT.
end-of-definition.
fieldcatset 'MATNR' '' '' '' '' '' '' '物料'.
fieldcatset 'WERKS' '' '' '' '' '' '' '工厂 '.
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_LAYOUT
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_set_layout .
clear gs_layout.
gs_layout-zebra ='X'. "使ALV界面呈现颜色交替
gs_layout-cwidth_opt ='X'. "自动优化列宽
* GS_LAYOUT-BOX_FNAME = 'CHECK'. "选择行控制
gs_layout-sel_mode ='A'. "选择模式,"A"在最左端有选择按钮
* GS_LAYOUT-INFO_FNAME ='CLR'. "设置颜色属性
* GS_LAYOUT-STYLEFNAME = 'STYLE'."内部表字段的字段名称
endform.
*&---------------------------------------------------------------------* 选择框
*& FORM FRM_OUTPUT
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_display_alv .
data: g_repid like sy-repid.
g_repid = sy-repid.
"显示ALV调用函数
call function 'REUSE_ALV_GRID_DISPLAY_LVC'
exporting
i_callback_program = g_repid
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
i_save = 'A'
i_callback_pf_status_set = 'FRM_SET_STATUS' "设置ALV工具栏
i_callback_user_command = 'FRM_USER_COMMAND' "设置自定义按钮事件
tables
t_outtab = gt_list "要展示数据的内表
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_STATUS_SET
*&---------------------------------------------------------------------*
*& 新建ALV展示界面
*&---------------------------------------------------------------------*
*& --> SLIS_T_EXTAB
*&---------------------------------------------------------------------*
form frm_set_status using rt_extab type slis_t_extab.
set pf-status 'STANDARD' excluding rt_extab. "自定义状态名称
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_FIELDCAT
*&---------------------------------------------------------------------*
*& 用户操作
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
"GUI状态的保存刷新
form frm_user_command using r_ucomm like sy-ucomm
rs_selfield type slis_selfield.
data: g_grid type ref to cl_gui_alv_grid.
call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
importing
e_grid = g_grid.
call method g_grid->check_changed_data.
rs_selfield-refresh = 'X'.
rs_selfield-col_stable = 'X'.
rs_selfield-row_stable = 'X'.
case r_ucomm.
when ''. "当点击按钮时调用下面的程序
endcase.
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_GET_DATA
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_get_data .
select
matnr
werks
basmg
bstrf
into corresponding fields of table gt_list
from marc up to 10 rows where werks in s_werks .
do 5 times.
append lines of gt_list to gt_list.
enddo.
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_SALV
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_salv20 .
data: gr_table type ref to cl_salv_table.
data: gr_display type ref to cl_salv_display_settings.
data: gr_layout type ref to cl_salv_layout.
data: gs_program type salv_s_layout_key."该结构包含了布局变式所属程序名
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.
"START——全屏模式
cl_salv_table=>factory(
importing r_salv_table = gr_table"用来接收工厂产生的实例
changing t_table = gt_list ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header('SALV TEST').
"布局样式
gr_layout = gr_table->get_layout( ).
gs_program-report = sy-repid.
gr_layout->set_key( gs_program )."设置布局保存为变式时,这些变式应属性此KEY(本程序名)
gr_layout->set_save_restriction( cl_salv_layout=>restrict_none )."允许保存布局为变式
"feildcat布局样式
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'MATNR' )."需处理的列
gr_column->set_long_text( 'KEY1' ).
gr_column->set_medium_text( 'KEY1' ).
gr_column->set_short_text( 'KEY1' ).
* GR_COLUMN->SET_VISIBLE( CL_SALV_COLUMN_TABLE=>FALSE )."隐藏列,注:这里虽然是隐藏了,但在布局设置里还是可以看到此列的,如果使布局里也看不到,则需要GR_COLUMN->SET_TECHNICAL( 'X' ).
"——修改列名
gr_column ?= gr_columns->get_column('WERKS').
gr_column->set_long_text( 'KEY2' ).
gr_column->set_medium_text( 'KEY2' ).
gr_column->set_short_text( 'KEY2' ).
"——列颜色
data: g_color type lvc_s_colo.
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'MATNR' )."需处理的列
g_color-col = '7'.
g_color-int = '1'.
g_color-inv = '0'.
gr_column->set_color( g_color ).
"——优化列宽
gr_columns = gr_table->get_columns( ).
gr_columns->set_optimize( 'X' ).
"——行列选择模式
data: gr_selection type ref to cl_salv_selections.
gr_selection = gr_table->get_selections( ).
gr_selection->set_selection_mode( if_salv_c_selection_mode=>row_column )."可以以行、列的方式进行选择
"——弹窗显示
* gr_table->set_screen_popup(
* start_column = 1
* end_column = 50
* start_line = 1
* end_line = 300 ).
"——分类小计
data: gr_sorts type ref to cl_salv_sorts.
data: gr_agg type ref to cl_salv_aggregations.
gr_sorts = gr_table->get_sorts( ).
gr_sorts->add_sort(
columnname = 'MATNR'
position = 1 "排序的顺序,如果根据多个字段来排时,决定哪个先排
sequence = if_salv_c_sort=>sort_up "升序
"是否需要以此字段进行分类小计
subtotal = if_salv_c_bool_sap=>true ).
gr_sorts->add_sort(
columnname = 'WERKS'
position = 2
sequence = if_salv_c_sort=>sort_down "降序
subtotal = if_salv_c_bool_sap=>true ).
gr_agg = gr_table->get_aggregations( ).
"需要进行分类小计及汇总的字段:INT1、INT2
gr_agg->add_aggregation( 'BSTRF' ). "决定此列是否进行分类汇总。注:如果先不进行排序,则只有汇总,不会进行分类小计
gr_agg->add_aggregation( 'BASMG' ).
"——初始化设置过滤条件
data: gr_filter type ref to cl_salv_filters.
gr_filter = gr_table->get_filters( ).
"只有KEY1为 A,STR以AB开头的数据才显示出来
gr_filter->add_filter(
columnname = 'WERKS'
sign = 'I'
option = 'EQ'
low = '0009').
"——自定义GUi
"设置工具条:只适用于全屏模式与全屏幕经典列表模式的SALV
gr_table->set_screen_status(
pfstatus = 'STANDARD'"系统提示的标准Status为SAPLSALV_METADATA_STATUS程序中的SALV_TABLE_STANDARD
report = sy-repid
"注:此参数只对SALV标准的预设保留按钮起作用,也就是说,当 T001 GUI Status是从
"系统中提供的标准Gui Staus拷贝时才起作用,即通用此参数来屏蔽或显示某些预置按钮
"对自己完全新创建的GUI Status是的按钮(实质上是根据 FunCode来判断的)不起作用
set_functions = gr_table->c_functions_all )."显示所有通用的预设按钮
"set_functions = gr_table->c_functions_default )."显示基本默认选择性的预设按钮
"set_functions = gr_table->c_functions_none )."所有预设按钮都将不会显示
"——双击事件
"=====获取事件对象
data: lr_event type ref to cl_salv_events_table.
lr_event = gr_table->get_event( ).
"=====事件注册
data: lr_handle_event type ref to l_cl_handle_events.
create object lr_handle_event.
set handler lr_handle_event->on_double_click for lr_event.
"——热点点击
"设置热点列
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'MATNR').
gr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
"——设置单元格颜色
data: lt_color type lvc_t_scol,
ls_color type lvc_s_scol.
loop at gt_list assigning field-symbol(<ls_outtab>).
clear: ls_color,lt_color.
ls_color-fname = 'BSTRF'."为哪列设置颜色
ls_color-color-col = '2'."颜色值
ls_color-color-int = '1'."加深
ls_color-color-inv = '0'."前景背景颜色反转
append ls_color to lt_color.
<ls_outtab>-color = lt_color.
endloop.
"——捕获异常
data:myref type ref to cx_salv_data_error.
data err_text type string.
try.
gr_columns->set_color_column( 'COLOR' ).
catch cx_salv_data_error into myref..
err_text = myref->get_text( ).
endtry.
gr_table->display( )."调用实例方法显示表报
"END——全屏模式
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_SALV21
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_salv21 .
"START——全屏经典列表模式
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions_list.
cl_salv_table=>factory(
exporting list_display = abap_true"以列表形式显示
importing r_salv_table = gr_table"用来接收工厂产生的实例
changing t_table = gt_list ).
"——
gr_functions = gr_table->get_functions( ).
gr_functions->set_default( )."ACTIVATE STANDARD SELECTION OF GENERIC ALV FUNCTIONS。激活工具栏中通用的选择性按钮。根据当前显示是LIST模式、还是GRID模式,来设置工具条上最基本的默认按钮,比GR_FUNCTIONS->SET_ALL( ABAP_TRUE)设置的按钮要少很多
gr_table->display( ).
"END——
endform.
*&---------------------------------------------------------------------*
*& FORM FRM_SALV21
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions_list.
data:gr_container type ref to cl_gui_custom_container.
data: ok_code type syucomm.
form frm_salv22 .
call screen 9000.
endform.
module 9000_pbo output.
"判断是否已分配了一个有效引用
if gr_container is not bound and p_but22 = 'X'.
"创建容器
create object gr_container
exporting
container_name = 'CONTAINER_1'. "屏幕上用户自定义控件名
"创建ALV
cl_salv_table=>factory(
exporting
r_container = gr_container
container_name = 'CONTAINER_1'
importing
r_salv_table = gr_table
changing
t_table = gt_list ).
"设置工具栏
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ). "ACTIVATE ALL GENERIC ALV FUNCTIONS,将激活所有的ALV内置通用按钮
"显示
gr_table->display( ).
endif.
endmodule.
*&---------------------------------------------------------------------*
*& FORM FRM_SALV21
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
form frm_salv23.
endform.
*&---------------------------------------------------------------------*
*& Form SHOW_CELL_INFO
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> ROW
*& --> COLUMN
*& --> P_
*&---------------------------------------------------------------------*
form show_cell_info using p_row
p_column
value(p_str) type string.
data: l_row type char10.
write p_row to l_row left-justified.
concatenate l_row 'Line,' p_column 'Column' p_str into p_str separated by space.
message i001(00) with p_str.
endform.