SAP BLOG--Jack Wu

人生最要紧的是充实自己,让自己的视野更广阔,让自己的脑筋更灵活,让自己看事情更准确,让自己与别人相处更和谐,让自己对群体有越来越多的贡献..........

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

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( ).

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CompassButton/article/details/1816907
个人分类: 13 SAP ABAP UI
上一篇ABAP--如何在abap中使用日志管理
下一篇ABAP--REUSE_ALV_GRID_DISPLAY的i_callback_user_command的调用跟踪摘抄
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭