ABAP--如何使用CL_SALV_TABLE 的代码样例(2004以后版本)

本文详细介绍如何通过ABAP中的CL_SALV_TABLE类及其相关类来优化ALV报表展示,包括工具栏添加、列表样式调整、列设置、排序、过滤、汇总及布局保存等功能。

1、简单的CL_SALV_TABLE 的使用
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_table->display( ).
运行结果

2、使用CL_SALV_FUNCTIONS添加工具栏(注意添加的红色代码)
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).

gr_table->display( ).
运行结果

3、使用CL_SALV_DISPLAY_SETTINGS(注意添加的红色代码)
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_table->display( ).

4、使用CL_SALV_COLUMNS_TABLE和CL_SALV_COLUMN_TABLE(注意添加的红色代码)
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.

data: color type lvc_s_colo.
start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'This is long text' ).
gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ).
color-col = '6'.
color-int = '1'.
color-inv = '0'.
gr_column->set_color( color ).

gr_table->display( ).

5、使用CL_SALV_SORTS(注意添加的红色代码),增加排序
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.
data: gr_sorts type ref to cl_salv_sorts.

data: color type lvc_s_colo.
start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'This is long text' ).
gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ).
color-col = '6'.
color-int = '1'.
color-inv = '0'.
gr_column->set_color( color ).
gr_sorts = gr_table->get_sorts( ).
gr_sorts->ADD_SORT( 'CITYTO' ).

gr_table->display( ).

6、使用CL_SALV_AGGREGATIONS(注意添加的红色代码),增加汇总
将DISTANCE字段按CITYTO字段进行汇总
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.
data: gr_sorts type ref to cl_salv_sorts.
data: gr_agg type ref to cl_salv_aggregations.

data: color type lvc_s_colo.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'This is long text' ).
gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ).
color-col = '6'.
color-int = '1'.
color-inv = '0'.
gr_column->set_color( color ).
gr_sorts = gr_table->get_sorts( ).
* gr_sorts->ADD_SORT( 'CITYTO' ).
gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ).
gr_agg = gr_table->get_aggregations( ).
gr_agg->add_aggregation( 'DISTANCE' ).
gr_table->display( ).

7、使用CL_SALV_FILTERS(注意添加的红色代码),增加过滤
只显示CARRID等于'LH'
REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.
data: gr_sorts type ref to cl_salv_sorts.
data: gr_agg type ref to cl_salv_aggregations.
data: gr_filter type ref to cl_salv_filters.

data: color type lvc_s_colo.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'This is long text' ).
gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ).
color-col = '6'.
color-int = '1'.
color-inv = '0'.
gr_column->set_color( color ).
gr_sorts = gr_table->get_sorts( ).
* gr_sorts->ADD_SORT( 'CITYTO' ).
gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ).
gr_agg = gr_table->get_aggregations( ).
gr_agg->add_aggregation( 'DISTANCE' ).
gr_filter = gr_table->get_filters( ).
gr_filter->add_filter( columnname = 'CARRID' low = 'LH' ).

gr_table->display( ).

8、使用CL_SALV_LAYOUT(注意添加的红色代码),增加变式保存
*&---------------------------------------------------------------------*
*& Report ZTEST4
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZALVOM_DEMO1.
data: ispfli type table of spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: gr_display type ref to cl_salv_display_settings.
data: gr_columns type ref to cl_salv_columns_table.
data: gr_column type ref to cl_salv_column_table.
data: gr_sorts type ref to cl_salv_sorts.
data: gr_agg type ref to cl_salv_aggregations.
data: gr_filter type ref to cl_salv_filters.
data: gr_layout type ref to cl_salv_layout.

data: key type salv_s_layout_key.
data: color type lvc_s_colo.

start-of-selection.
select * into table ispfli from spfli.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_striped_pattern( cl_salv_display_settings=>true ).
gr_display->set_list_header( 'This is the heading' ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'This is long text' ).
gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ).
color-col = '6'.
color-int = '1'.
color-inv = '0'.
gr_column->set_color( color ).
gr_sorts = gr_table->get_sorts( ).
* gr_sorts->ADD_SORT( 'CITYTO' ).
gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ).
gr_agg = gr_table->get_aggregations( ).
gr_agg->add_aggregation( 'DISTANCE' ).
gr_filter = gr_table->get_filters( ).
gr_filter->add_filter( columnname = 'CARRID' low = 'LH' ).
gr_layout = gr_table->get_layout( ).
key-report = sy-repid.
gr_layout->set_key( key ).
gr_layout->set_save_restriction( cl_salv_layout=>restrict_none ).

gr_table->display( ).

ABAP 中,`CL_SALV_TABLE` 是一个强大的类,用于展示和配置 ALV(ABAP List Viewer)表格。它提供了丰富的功能,如显示数据、设置功能按钮、列优化、布局配置以及异常处理等。 ### 显示数据 要使用 `CL_SALV_TABLE` 显示数据,可以通过调用 `CL_SALV_TABLE=>FACTORY` 方法来创建一个实,并传递数据表作为参数。以下是一个简单的示: ```abap DATA: lo_salv TYPE REF TO cl_salv_table, gt_data TYPE TABLE OF sflight. CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = lo_salv CHANGING t_table = gt_data. lo_salv->get_functions_base( )->set_all( ). lo_salv->display( ). ``` 在这个子中,`gt_data` 是包含数据的内部表,`lo_salv` 是创建的 `CL_SALV_TABLE` 实。通过调用 `get_functions_base()->set_all()` 可以启用所有默认功能按钮,最后调用 `display()` 方法来显示 ALV 表格 [^3]。 ### 设置函数 `CL_SALV_TABLE` 提供了多种方法来设置功能按钮。如,可以禁用或启用特定的功能,或者添加自定义功能。以下是如何设置功能按钮的示: ```abap DATA: lo_functions TYPE REF TO cl_salv_functions_list. lo_functions = lo_salv->get_functions_list( ). lo_functions->set_function_visible( if_salv_c_function=>export ). lo_functions->set_function_enabled( if_salv_c_function=>print, abap_false ). ``` 在这个子中,`get_functionsList()` 方法获取功能列表,`set_function_visible()` 方法设置特定功能为可见,而 `set_function_enabled()` 方法则可以启用或禁用特定功能 [^2]。 ### 列优化 列优化包括调整列宽、设置列标题、隐藏列等操作。可以通过 `CL_SALV_COLUMNS_TABLE` 类的方法来实现这些设置: ```abap DATA: lo_columns TYPE REF TO cl_salv_columns_table. lo_columns = lo_salv->get_columns( ). " 设置列宽 lo_columns->set_optimize( if_salv_c_bool=>true ). " 设置列标题 lo_columns->get_column( 'CARRID' )->set_short_text( 'Airline' ). " 隐藏列 lo_columns->get_column( 'CONNID' )->set_visible( if_salv_c_bool=>false ). ``` 上述代码展示了如何优化列宽、更改列标题以及隐藏特定列 [^1]。 ### 布局配置 布局配置涉及保存和恢复用户的视图设置,比如列顺序、宽度和排序等。这可以通过 `CL_SALV_LAYOUT` 类来完成: ```abap DATA: lo_layout TYPE REF TO cl_salv_layout. lo_layout = lo_salv->get_layout( ). lo_layout->set_key( 'MY_LAYOUT_KEY' ). lo_layout->set_default( if_salv_c_bool=>true ). lo_layout->save( ). ``` 在这个子中,`set_key()` 方法设置了布局的标识符,`set_default()` 方法将当前布局设为默认布局,而 `save()` 方法则保存了当前的布局设置 [^1]。 ### 异常处理 在使用 `CL_SALV_TABLE` 时,可能会遇到一些异常情况,比如数据不一致或无效的操作。为了处理这些异常,可以使用 TRY-CATCH 块来捕获并处理异常: ```abap TRY. CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = lo_salv CHANGING t_table = gt_data. CATCH cx_salv_msg INTO DATA(lx_msg). " 处理异常 WRITE: / 'Error creating SALV table:', lx_msg->get_text( ). ENDTRY. ``` 在这个 TRY-CATCH 块中,如果创建 `CL_SALV_TABLE` 实时发生错误,则会捕获到 `cx_salv_msg` 类型的异常,并输出错误信息 [^1]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值