1. 程式人生 > >ABAP--如何使用CL_SALV_TABLE 的程式碼樣例(2004以後版本)

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