ALV的栏位属性是SAP提供的ALV功能的比较重要的一个,详细的功能包括异常状态图标、图标、符号、复选框、按钮、热点、超链接、单元格类型、单元格颜色、某一栏位颜色、某一行颜色,栏位隐藏等等。ALV的栏位功能主要是通过CL_SALV_COLUMN_TABLE和CL_SALV_COLUMNS_TABLE这两个类的实现的。
1. 首先我们要取得类CL_SALV_COLUMNS_TABLE的实例。
DATA lr_columns TYPE REF TO cl_salv_columns_table.
lr_columns = gr_table->get_columns( ).
2然后通过类CL_SALV_COLUMNS_TABLE的方法get_column(value)取得指定的栏位的CL_SALV_COLUMN_TABLE的实例,取指定的栏位的实例之后,你就可以对这个栏位的相关属性进行设置。
DATA lr_column TYPE REF TO cl_salv_column_table
lr_column ?= lr_columns->get_column( 'MANDT' ).
2. 取得指定的栏位的实例之后,在这个实例的基础上进行相关的属性设置。
a) 异常状态图标
lr_columns->set_exception_column( 'EXCEPTION' ).
lr_column ?= lr_columns->get_column( 'EXCEPTION' ).
lr_column->set_short_text( 'except' ).
b) 图标
lr_column ?= lr_columns->get_column( 'ICON' ).
lr_column->set_icon( 'X' ).
lr_column->set_long_text( 'ICON' ).
c) 符号
lr_column ?= lr_columns->get_column( 'SYMBOL' ).
lr_column->set_symbol( 'X' ).
lr_column->set_long_text( 'SYMBOL' ).
d) 复选框
lr_column ?= lr_columns->get_column( 'CHECKBOX' ).
lr_column->set_cell_type( if_salv_c_cell_type=>checkbox ).
lr_column->set_long_text( 'CHECKBOX' ).
e) 热点
lr_column ?= lr_columns->get_column( 'HOTSPOT' ).
lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
lr_column->set_long_text( 'HOTSPOT' ).
f) 栏位不可见
lr_column ?= lr_columns->get_column( 'MANDT' ).
lr_column->set_visible( '' ).
g) 超链接
lr_columns->set_hyperlink_entry_column( 'T_HYPERLINK' ).
lr_column ?= lr_columns->get_column( 'HYPERLINK' ).
lr_column->set_cell_type( if_salv_c_cell_type=>link ).
lr_column->set_long_text( 'HYPERLINK' ).
lr_hyperlinks = lr_functional_settings->get_hyperlinks( ).
LOOP AT gt_hyperlink INTO wa_hyperlink.
lr_hyperlinks->add_hyperlink(
handle = wa_hyperlink-handle
hyperlink = wa_hyperlink-hyperlink ).
ENDLOOP.
h) 栏位颜色
lr_column ?= lr_columns->get_column( 'FLTIME' ).
ls_color-col = 6.
ls_color-int = 0.
ls_color-inv = 0.
lr_column->set_color( ls_color ).
i) 行颜色和单元格颜色
lr_columns->set_color_column( 'T_COLOR' ).
j) 设置栏位显示长度最优化
lr_columns->set_optimize( 'X' ).
k) 设置标题栏是否可见
lr_columns->set_headers_visible( 'X' )
注:上面的所有栏位属性设置都要需要在相应的内表字段设置相关的值,设置是否可见除外。
下面是效果图
下面是程序的所有代码:
REPORT y_xin_002.
*变量定义
INCLUDE <icon>.
INCLUDE <symbol>.
TYPES: BEGIN OF gs_spfli,
exception TYPE char1,
mandt TYPE spfli-mandt,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
countryto TYPE spfli-countryto,
cityto TYPE spfli-cityto,
airpto TYPE spfli-airpto,
fltime TYPE spfli-fltime,
deptime TYPE spfli-deptime,
arrtime TYPE spfli-arrtime,
distance TYPE spfli-distance,
distid TYPE spfli-distid,
fltype TYPE spfli-fltype,
period TYPE spfli-period,
icon TYPE icon_d,
symbol TYPE icon_d,
checkbox TYPE char1,
hotspot TYPE icon_d,
hyperlink TYPE char255,
t_hyperlink TYPE salv_t_int4_column,
t_color TYPE lvc_t_scol,
t_celltype TYPE salv_t_int4_column,
END OF gs_spfli.
TYPES ty_spfli TYPE gs_spfli OCCURS 0.
DATA: gr_table TYPE REF TO cl_salv_table.
*----------------------------------------------------------------------*
* CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
* ALV操作类(定义)
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
PUBLIC SECTION.
METHODS: getdata "取得要显示的数据
RETURNING value(lt_tab) TYPE ty_spfli,
alv_full "全屏Grid列表处理方法
IMPORTING value(lt_tab) TYPE ty_spfli,
column_setting, "设置字段属性
main. "主方法
PRIVATE SECTION.
DATA: BEGIN OF wa_hyperlink,
handle TYPE salv_de_hyperlink_handle,
hyperlink TYPE service_rl,
END OF wa_hyperlink.
DATA gt_hyperlink LIKE TABLE OF wa_hyperlink.
ENDCLASS. "lcl_alv DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
* ALV操作类(实现)
*----------------------------------------------------------------------*
CLASS lcl_alv IMPLEMENTATION.
*取得要显示的数据
METHOD getdata.
FIELD-SYMBOLS <fs_spfli> TYPE gs_spfli.
DATA: lwa_hyperlink TYPE LINE OF salv_t_int4_column,
lt_hyperlink LIKE TABLE OF lwa_hyperlink,
l_tabix TYPE sy-tabix,
ls_color TYPE lvc_s_scol,
lt_color TYPE lvc_t_scol,
ls_celltype TYPE salv_s_int4_column,
lt_celltype TYPE salv_t_int4_column.
DEFINE add_color.
clear ls_color.
ls_color-fname = &1.
ls_color-color-col = &2.
ls_color-color-int = 1.
ls_color-color-inv = 0.
ls_color-nokeycol = 'X'.
append ls_color to lt_color.
END-OF-DEFINITION.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_tab FROM spfli.
"循环,为EXCEPTION等栏位赋值
LOOP AT lt_tab ASSIGNING <fs_spfli>.
l_tabix = sy-tabix.
<fs_spfli>-exception = '3'.
<fs_spfli>-icon = icon_okay.
<fs_spfli>-symbol = sym_document.
<fs_spfli>-checkbox = 'X'.
<fs_spfli>-hotspot = icon_overview.
REFRESH lt_hyperlink.
lwa_hyperlink-value = l_tabix.
lwa_hyperlink-columnname = 'HYPERLINK'.
APPEND lwa_hyperlink TO lt_hyperlink.
<fs_spfli>-t_hyperlink = lt_hyperlink.
<fs_spfli>-hyperlink = 'www.sina.com.cn'.
wa_hyperlink-handle = l_tabix.
wa_hyperlink-hyperlink = 'www.sina.com.cn'.
APPEND wa_hyperlink TO gt_hyperlink.
REFRESH lt_color.
IF <fs_spfli>-distance >= 5000.
CLEAR ls_color.
ls_color-fname = 'DISTANCE'.
ls_color-color-col = 5.
ls_color-color-int = 1.
ls_color-color-inv = 0.
APPEND ls_color TO lt_color.
ENDIF.
CLEAR ls_color.
ls_color-fname = 'COUNTRYFR'.
ls_color-color-col = 5.
ls_color-color-int = 1.
ls_color-color-inv = 0.
APPEND ls_color TO lt_color.
IF <fs_spfli>-countryfr IS NOT INITIAL.
add_color 'EXCEPTION' 4.
add_color 'MANDT' 4.
add_color 'CARRID' 4.
add_color 'CONNID' 4.
add_color 'COUNTRYFR' 4.
add_color 'CITYFROM' 4.
add_color 'AIRPFROM' 4.
add_color 'COUNTRYTO' 4.
add_color 'CITYTO' 4.
add_color 'AIRPTO' 4.
add_color 'FLTIME' 4.
add_color 'DEPTIME' 4.
add_color 'ARRTIME' 4.
add_color 'DISTANCE' 4.
add_color 'DISTID' 4.
add_color 'FLTYPE' 4.
add_color 'PERIOD' 4.
add_color 'ICON' 4.
add_color 'SYMBOL' 4.
add_color 'CHECKBOX' 4.
add_color 'HOTSPOT' 4.
add_color 'HYPERLINK' 4.
ENDIF.
<fs_spfli>-t_color = lt_color.
ENDLOOP.
ENDMETHOD. "getdata
*输出全屏网格列表的方法
METHOD alv_full.
DATA: lr_functions TYPE REF TO cl_salv_functions.
"创建实例
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = gr_table
CHANGING
t_table = lt_tab
).
CATCH cx_salv_msg.
ENDTRY.
"显示ALV的标准功能
lr_functions = gr_table->get_functions( ).
lr_functions->set_all( 'X' ).
"设置栏位属性
me->column_setting( ).
"显示列表
gr_table->display( ).
ENDMETHOD. "alv_full
*设置字段属性
METHOD column_setting.
DATA: lr_columns TYPE REF TO cl_salv_columns_table,
lr_column TYPE REF TO cl_salv_column_table,
lr_functional_settings TYPE REF TO cl_salv_functional_settings,
lr_hyperlinks TYPE REF TO cl_salv_hyperlinks.
DATA: ls_color TYPE lvc_s_colo.
lr_columns = gr_table->get_columns( ).
lr_columns->set_optimize( 'X' ).
lr_columns->set_headers_visible( 'X' ). "设置标题栏是否可见
lr_functional_settings = gr_table->get_functional_settings( ).
TRY.
"设置栏位不可见
lr_column ?= lr_columns->get_column( 'MANDT' ).
lr_column->set_visible( '' ).
lr_column ?= lr_columns->get_column( 'PERIOD' ).
lr_column->set_visible( '' ).
lr_column ?= lr_columns->get_column( 'FLTYPE' ).
lr_column->set_visible( '' ).
lr_column ?= lr_columns->get_column( 'DISTID' ).
lr_column->set_visible( '' ).
lr_column ?= lr_columns->get_column( 'DEPTIME' ).
lr_column->set_visible( '' ).
lr_column ?= lr_columns->get_column( 'ARRTIME' ).
lr_column->set_visible( '' ).
"设置异常栏位
lr_columns->set_exception_column( 'EXCEPTION' ).
lr_column ?= lr_columns->get_column( 'EXCEPTION' ).
lr_column->set_short_text( 'except' ).
lr_column->set_medium_text( 'exception' ).
lr_column->set_long_text( 'my exception' ).
"设置图标栏位
lr_column ?= lr_columns->get_column( 'ICON' ).
lr_column->set_icon( 'X' ).
lr_column->set_long_text( 'ICON' ).
"设置符号栏位
lr_column ?= lr_columns->get_column( 'SYMBOL' ).
lr_column->set_symbol( 'X' ).
lr_column->set_long_text( 'SYMBOL' ).
"设置复选框栏位
lr_column ?= lr_columns->get_column( 'CHECKBOX' ).
lr_column->set_cell_type( if_salv_c_cell_type=>checkbox ).
lr_column->set_long_text( 'CHECKBOX' ).
"设置热点
lr_column ?= lr_columns->get_column( 'HOTSPOT' ).
lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
lr_column->set_long_text( 'HOTSPOT' ).
"设置超链接
lr_columns->set_hyperlink_entry_column( 'T_HYPERLINK' ).
lr_column ?= lr_columns->get_column( 'HYPERLINK' ).
lr_column->set_cell_type( if_salv_c_cell_type=>link ).
lr_column->set_long_text( 'HYPERLINK' ).
lr_hyperlinks = lr_functional_settings->get_hyperlinks( ).
LOOP AT gt_hyperlink INTO wa_hyperlink.
lr_hyperlinks->add_hyperlink(
handle = wa_hyperlink-handle
hyperlink = wa_hyperlink-hyperlink ).
ENDLOOP.
"设置栏位颜色
lr_column ?= lr_columns->get_column( 'FLTIME' ).
ls_color-col = 6.
ls_color-int = 0.
ls_color-inv = 0.
lr_column->set_color( ls_color ).
"设置行颜色
lr_columns->set_color_column( 'T_COLOR' ).
CATCH cx_salv_not_found.
CATCH cx_salv_data_error.
CATCH cx_salv_existing.
ENDTRY.
ENDMETHOD. "column_setting
*主方法,整合数据
METHOD main.
DATA: lt_tab TYPE ty_spfli.
"取得要显示的数据
lt_tab = me->getdata( ).
me->alv_full( lt_tab ).
ENDMETHOD. "main
ENDCLASS. "lcl_alv IMPLEMENTATION
*报表执行
START-OF-SELECTION.
DATA lr_alv TYPE REF TO lcl_alv.
CREATE OBJECT lr_alv.
lr_alv->main( ).