ALV详解:OO SALV

OOSALV

Net Weaver2004平台上集合了利用函数和GRID的功能发布的SALV包程序(SALV也称为New ALV)。

SALV可以像使用函数方式生成ALV那样,不用创建屏幕就可以调用的全屏方式显示的ALV

SALVGRID报表可以在后台运行,但以前函数方式或OO方式生成的GRID不能?

SALV与现有的方法(Function ALV)相比,为了方便以接口的方式提供更整合及细微的功能,但SALV不提供编辑功能(但可以通过SALV适配器调用CL_GUI_ALV_GRID修改成编辑模式,就可以在ALV中修改数据了

更多的实例请参考SALV程序包(Package,里面有大量示例

 

 

SALV有以下三种类型的报表:

image096

三种显示模式

l  全屏模式

l  全屏模式——Classic 报表模式(普通List列表)

l  利用控制器的模式(利用用户自定义控件区域来控制ALV显示大小)

CL_GUI_ALV_GRID相似的是CL_SALV_TABLE类,通过其factory方法,生成SALV实例:

image097

要显示ALV,只需要调用以下两个方法即可:

factory静态方法。定义要显示在屏幕上的数据;定义ALV报表样式;调用此方法就会返回CL_SALV_TABLE类型的实例对象

display实例方法。调用此方法屏幕上就显示ALV

image098

LIST_DISPLAY:参数决定了列表显示的模式:是以List普通列表方式显示,还是Grid网格方式显示

R_CONTAINER用户自定义控件区域的引用对象,类型为CL_GUI_CONTAINER

CONTAINER_NAME:屏幕上用户自定义控件区域(Custom Controlimage064的名称

上面三个参数与显示模式关系如下:

 

LIST_DISPLAY

R_CONTAINER

CONTAINER_NAME

全屏

ABAP_FALSE

初始值

初始值

Classic List

ABAP_TRUE

初始值

初始值

利用控制器

ABAP_FALSE

CL_GUI_CONTAINER对象引用

用户自定义控件区域(Custom Control)名称

全屏模式

image099

与通过REUSE_ALV_GRID_DISPLAY函数产生的ALV相当:

DATA: gr_table TYPE REF TO cl_salv_table.
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
    CHANGING t_table = gt_data[] ).

  gr_table
->display( )."
调用实例方法显示表报

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

全屏经典列表模式

image100

与通过REUSE_ALV_LIST_DISPLAY函数产生的ALV相当:

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_functions TYPE REF TO cl_salv_functions_list.
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
    
EXPORTING list_display = abap_true"以列表形式显示
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).
  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( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

可控模式

先绘制屏幕 100,然后在其上面放上用户自定义控件区域(Custom Controlimage064[1],并命名为CONTAINER_1

image101

image102

 

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.
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
CALL SCREEN 100.

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

MODULE 100_pbo OUTPUT.
 
PERFORM inital.
 
"判断是否已分配了一个有效引用
 
IF gr_container IS NOT BOUND.
   
"创建容器
   
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_data[] ).
    
"设置工具栏
   gr_functions
= gr_table->get_functions( ).
    gr_functions
->set_all( abap_true). "Activate All Generic ALV Functions,将激活所有的ALV内置通用按钮

   
"显示
    gr_table
->display( ).

 
ENDIF.
ENDMODULE.

CL_SALV_TABLE设置相关方法

SALV进行设置,先需要通过CL_SALV_TABLE对象实例获取相关设置对象,然后再对这些设置对进行操作

如对样式的设置,需要先通过CL_SALV_TABLE对象的GET_LAYOUT( )方法拿到CL_SALV_LAYOUT对象,实后通过该对象相关方法对SALV进行设置;除样式外,如排序、工具栏设置、事件等,都是先通过某个GET*方法拿到相应对象,再通过该对象相关方法对SALV进行设置,这与以前Function ALV、及OO ALV不太一样。如上面程序中对SALV工具栏进行设置是这样的:

DATA: gr_functions TYPE REF TO cl_salv_functions_list.
gr_functions = gr_table->get_functions( ).

gr_functions
->set_all( abap_true).

 

CL_SALV_TABLEGET*相关方法:

image103

样式设置

显示设置

image104

SET_VERTICAL_LINES:设置是否显示垂直线

SET_HORIZONTAL_LINES:设置是否显示水平线

SET_STRIPED_PATTERN:斑马条纹(行颜色交替)

SET_LIST_HEADER_SIZE:报表头

SET_FIT_COLUMN_TO_TABLE_SIZE:列自适应表格宽度

image105

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_display TYPE REF TO cl_salv_display_settings.
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_display
= gr_table->get_display_settings( ).
  gr_display
->set_striped_pattern( cl_salv_display_settings=>true ).
  gr_display
->set_list_header('Header Test').

  gr_table
->display( ). "调用实例方法显示表报

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bb'.
 
APPEND gt_data.
ENDFORM.

布局变式

image106

F4_LAYOUTS

SET_DEFAULT

SET_INITIAL_LAYOUT

SET_KEY

SET_SAVE_RESTRICTION

image107

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_layout TYPE REF TO cl_salv_layout.
DATA: gs_program TYPE salv_s_layout_key."该结构包含了布局变式所属程序名
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  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 )."允许保存布局为变式

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bb'.
 
APPEND gt_data.
ENDFORM.

类似的Fieldcat

Function ALVOO ALV可通过Fieldcat对列进行相关设置,SALV也有类似于这样的东西,只不过不是Fieldcat,而是通过CL_SALV_COLUMN对象来实现的。CL_SALV_COLUMN的获取过程如下:

image108

image109

image110

隐藏字段(列)

image111

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns
->get_column( 'KEY1' )."需处理的列

 
gr_column->set_visible( cl_salv_column_table=>false )."隐藏列,注:这里虽然是隐藏了,但在布局设置里还是可以看到此列的,如果使布局里也看不到,则需要gr_column->set_technical( 'X' ).

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

设置列标题

image112


DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns->get_column( 'KEY2' )."
需处理的列
  gr_column->set_long_text( 'key2' ).
  gr_column
->set_medium_text( 'key2' ).
  gr_column
->set_short_text( 'key2'
).
  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

设置列的颜色

image113
DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table
.
DATA: g_color TYPE lvc_s_colo.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns
->get_column( 'KEY2' )."需处理的列
  g_color
-col = '6'.
  g_color
-int = '1'.
  g_color
-inv = '0'
.
  gr_column->set_color( g_color ).

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data-key1 = 'b'.

  gt_data
-key2 = 'bb'.
 
APPEND gt_data.
ENDFORM.

优化列宽

相当于Function ALV所使用Layoutcolwidth_optimize属性

优化前:image114

优化后:image115

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(20),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_columns
= gr_table->get_columns
( ).
  gr_columns->set_optimize( 'X' ).

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bbbbbbbb'.
 
APPEND gt_data.
ENDFORM.

SALV行列选择模式

image116

image117

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_selection TYPE REF TO cl_salv_selections.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(20),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

  gr_selection
= gr_table
->get_selections( ).
 
gr_selection->set_selection_mode( if_salv_c_selection_mode=>row_column )."可以以行、列的方式进行选择

  gr_table
->display( ).

FORM
inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bbbbbbbb'.
 
APPEND gt_data.
ENDFORM.

以弹出框显示SALV

可控模式(利用控制器的模式)SALV是不能以弹出框来显示的

image118

DATA: gr_table TYPE REF TO cl_salv_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,"ALV标题行会参照词典类型
  key2
(20),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[]
).
  gr_table->set_screen_popup(
    start_column
= 1
    end_column
= 50
    start_line
= 1
    end_line
= 5
).
  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bbbbbbbb'.
 
APPEND gt_data.
ENDFORM.

排序、分类汇总

该示例与前面Function ALV的示例效果是一样的

image119

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_sorts TYPE REF TO cl_salv_sorts.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.
DATA: gr_agg TYPE REF TO cl_salv_aggregations.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
(1),
  key2
(2),
  str
(3),
  int1
TYPE i,
  int2
TYPE i,
  int3
TYPE i,
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.

  cl_salv_table
=>factory(
     
IMPORTING r_salv_table = gr_table
     
CHANGING t_table = gt_data[] ).


  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns
->get_column( 'KEY1' ).
  gr_column
->set_long_text( 'key1' ).
  gr_column ?= gr_columns->get_column( 'KEY2' ).
  gr_column
->set_long_text( 'key2' ).
  gr_column ?= gr_columns
->get_column( 'STR' ).
  gr_column
->set_long_text( 'str' ).
  gr_column ?= gr_columns
->get_column( 'INT1' ).
  gr_column
->set_long_text( 'int1' ).
  gr_column ?= gr_columns
->get_column( 'INT2' ).
  gr_column
->set_long_text( 'int2' ).
  gr_column ?= gr_columns
->get_column( 'INT3' ).
  gr_column
->set_long_text( 'int3' ).

  gr_sorts
= gr_table->get_sorts( ).
  gr_sorts
->add_sort(
    columnname
= 'KEY1'
   
position = 1 "排序的顺序,如果根据多个字段来排时,决定哪个先排
    sequence
= if_salv_c_sort=>sort_up "升序
   
"是否需要以此字段进行分类小计
    subtotal
= if_salv_c_bool_sap=>true
 
).


  gr_sorts
->add_sort(
    columnname
= 'KEY2'
   
position = 2
    sequence
= if_salv_c_sort=>sort_down "降序
    subtotal
= if_salv_c_bool_sap=>true
 
).


  gr_agg
= gr_table->get_aggregations( ).
 
"需要进行分类小计及汇总的字段:INT1INT2
  gr_agg
->add_aggregation( 'INT1' ). "决定此列是否进行分类汇总。注:如果先不进行排序,则只有汇总,不会进行分类小计
  gr_agg
->add_aggregation( 'INT2' ).

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aaa'.
  gt_data
-int1 = 2.
  gt_data
-int2 = 1.
  gt_data
-int3 = 8.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aab'.
  gt_data
-int1 = 3.
  gt_data
-int2 = 2.
  gt_data
-int3 = 7.
 
APPEND gt_data.

  gt_data
-key1 = 'c'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aab'.
  gt_data
-int1 = 4.
  gt_data
-int2 = 2.
  gt_data
-int3 = 6.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'ab'.
  gt_data
-str = 'aba'.
  gt_data
-int1 = 6.
  gt_data
-int2 = 1.
  gt_data
-int3 = 2.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'ab'.
  gt_data
-str = 'abb'.
  gt_data
-int1 = 3.
  gt_data
-int2 = 4.
  gt_data
-int3 = 2.
 
APPEND gt_data.

  gt_data
-key1 = 'e'.
  gt_data
-key2 = 'ca'.
  gt_data
-str = 'eca'.
  gt_data
-int1 = 2.
  gt_data
-int2 = 2.
  gt_data
-int3 = 4.
 
APPEND gt_data.

  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'ba'.
  gt_data
-str = 'bba'.
  gt_data
-int1 = 8.
  gt_data
-int2 = 5.
  gt_data
-int2 = 2.
 
APPEND gt_data.

  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bb'.
  gt_data
-str = 'bbb'.
  gt_data
-int1 = 1.
  gt_data
-int2 = 2.
  gt_data
-int3 = 4.
 
APPEND gt_data.

  gt_data
-key1 = 'c'.
  gt_data
-key2 = 'ca'.
  gt_data
-str = 'cca'.
  gt_data
-int1 = 5.
  gt_data
-int2 = 1.
  gt_data
-int2 = 3.
 
APPEND gt_data.

  gt_data
-key1 = 'c'.
  gt_data
-key2 = 'cc'.
  gt_data
-str = 'ccc'.
  gt_data
-int1 = 2.
  gt_data
-int2 = 1.
  gt_data
-int3 = 5.
 
APPEND gt_data.

  gt_data
-key1 = 'd'.
  gt_data
-key2 = 'ca'.
  gt_data
-str = 'dca'.
  gt_data
-int1 = 4.
  gt_data
-int2 = 6.
  gt_data
-int3 = 2.
 
APPEND gt_data.
ENDFORM.

设置数据过滤条件

ALV 的标准按钮中已经有过滤的功能,我们也可以在初始显示的时候就设置过滤条件

image120

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_sorts TYPE REF TO cl_salv_sorts.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table
.
DATA: gr_filter TYPE REF TO cl_salv_filters.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
(1),
  key2
(2),
  str
(3),
  int1
TYPE i,
  int2
TYPE i,
  int3
TYPE i,
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.

  cl_salv_table
=>factory(
     
IMPORTING r_salv_table = gr_table
     
CHANGING t_table = gt_data[] ).


  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns
->get_column( 'KEY1' ).
  gr_column
->set_long_text( 'key1' ).
  gr_column ?= gr_columns
->get_column( 'KEY2' ).
  gr_column
->set_long_text( 'key2' ).
  gr_column ?= gr_columns
->get_column( 'STR' ).
  gr_column
->set_long_text( 'str' ).
  gr_column ?= gr_columns
->get_column( 'INT1' ).
  gr_column
->set_long_text( 'int1' ).
  gr_column ?= gr_columns
->get_column( 'INT2' ).
  gr_column
->set_long_text( 'int2' ).
  gr_column ?= gr_columns
->get_column( 'INT3' ).
  gr_column
->set_long_text( 'int3' ).


 
gr_filter = gr_table->get_filters( ).
 
"只有Key1 astrab开头的数据才显示出来
  gr_filter
->add_filter(
    columnname
= 'KEY1'
   
sign = 'I'
    option
= 'EQ'
    low
= 'a').
  gr_filter
->add_filter(
    columnname
= 'STR'
   
sign = 'I'
    option
= 'CP'
    low
= 'ab*'
).

  gr_table->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aaa'.
  gt_data
-int1 = 2.
  gt_data
-int2 = 1.
  gt_data
-int3 = 8.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aab'.
  gt_data
-int1 = 3.
  gt_data
-int2 = 2.
  gt_data
-int3 = 7.
 
APPEND gt_data.

  gt_data
-key1 = 'c'.
  gt_data
-key2 = 'aa'.
  gt_data
-str = 'aab'.
  gt_data
-int1 = 4.
  gt_data
-int2 = 2.
  gt_data
-int3 = 6.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'ab'.
  gt_data
-str = 'aba'.
  gt_data
-int1 = 6.
  gt_data
-int2 = 1.
  gt_data
-int3 = 2.
 
APPEND gt_data.

  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'ab'.
  gt_data
-str = 'abb'.
  gt_data
-int1 = 3.
  gt_data
-int2 = 4.
  gt_data
-int3 = 2.
 
APPEND gt_data.
ENDFORM.

GUI Status

使用预置的GUI Status

注:若要设置自定义的GUI Status只适用于全屏模式全屏幕经典列表模式SALV

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory("全屏模式的SALV
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[] ).


 
"设置工具条:只适用于全屏模式全屏幕经典列表模式SALV
  gr_table
->set_screen_status(
     pfstatus
= 'T002'"系统提示的标准StatusSAPLSALV_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 )."所有预设按钮都将不会显示

  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

在预设置工具栏上附加按钮

附加按钮只适用于可控制模式(即用户对话屏幕)的SALV

image121
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.
DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
CALL SCREEN 100.

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

MODULE 100_pbo OUTPUT.
 
PERFORM inital.
 
"判断是否已分配了一个有效引用
 
IF gr_container IS NOT BOUND.
   
"创建容器
   
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_data[] ).

   
"附加刷新按钮
   
DATA: lr_functions TYPE REF TO cl_salv_functions_list.
    lr_functions
= gr_table->get_functions( ).
    lr_functions
->set_all( abap_true )."Activate All Generic ALV Functions,将激活所有的ALV内置通用按钮
   
INCLUDE <icon>.
   
DATA: l_icon TYPE string.
    l_icon
= icon_refresh.
   
"附加按钮,只适用于 可控模式 下的SALV
    lr_functions
->add_function(
      name
= 'refresh'
     
icon = l_icon
     
text = '
刷新按钮'
      tooltip
= '刷新数据'
     
"按钮存放的位置:这里在右边附加。该参数的其他取值可以查看该方法源码,可以
     
"找到该参数其他取值的蛛丝马迹
     
position = if_salv_c_function_position=>right_of_salv_functions ).

   
"显示
    gr_table
->display( ).
 
ENDIF.
ENDMODULE.

事件

DOUBLE_CLICK双击事件

image122

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.

DATA: gr_table TYPE REF TO cl_salv_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[] ).


 
"=====
获取事件对象
  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_table
->display( ).

FORM show_cell_info USING p_row TYPE i
                          p_column
TYPE lvc_fname
                          p_text
TYPE string.

 
DATA: l_row TYPE char10.
 
WRITE p_row TO l_row LEFT-JUSTIFIED.

 
CONCATENATE l_row 'Line,' p_column 'Column' p_text INTO p_text SEPARATED BY space.
 
MESSAGE i001(00) WITH p_text.

ENDFORM.
FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

LINK_CLICK热点点击事件

image123

CLASS l_cl_handle_events DEFINITION.
 
PUBLIC SECTION.
   
METHODS: on_link_click
FOR EVENT link_click OF cl_salv_events_table
                 
IMPORTING

                    row
"事件触发所在的行号
                    column
."事件触发所在的列名
ENDCLASS.

CLASS l_cl_handle_events IMPLEMENTATION.
 
METHOD on_link_click.
   
PERFORM show_cell_info USING row column 'is selected with hotspot'.
 
ENDMETHOD.
ENDCLASS.

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[] ).


 
"
设置热点列
  gr_columns = gr_table->get_columns( ).
  gr_column ?= gr_columns
->get_column( 'KEY1').
  gr_column
->set_cell_type( if_salv_c_cell_type=>hotspot
).

 
"=====获取事件对象
 
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_link_click FOR lr_event.

  gr_table
->display( ).

FORM show_cell_info USING p_row TYPE i
                          p_column
TYPE lvc_fname
                          p_text
TYPE string.

 
DATA: l_row TYPE char10.
 
WRITE p_row TO l_row LEFT-JUSTIFIED.

 
CONCATENATE l_row 'Line,' p_column 'Column' p_text INTO p_text SEPARATED BY space.
 
MESSAGE i001(00) WITH p_text.

ENDFORM.
FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

ADDED_FUNCTION附加功能(按钮)事件

在“在预设置工具栏上附加按钮”示例的基础上,增加按钮事件

image124

CLASS l_cl_handle_events DEFINITION.
 
PUBLIC SECTION.
   
METHODS: on_user_command FOR EVENT added_function OF cl_salv_events_table
                 
IMPORTING e_salv_function.

ENDCLASS.

CLASS l_cl_handle_events IMPLEMENTATION.
 
METHOD on_user_command.
   
PERFORM handle_user_command USING e_salv_function.
 
ENDMETHOD.
ENDCLASS.

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:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
END OF gt_data.

START-OF-SELECTION.
 
CALL SCREEN 100.

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
ENDFORM.

MODULE 100_pbo OUTPUT.
 
PERFORM inital.
 
"判断是否已分配了一个有效引用
 
IF gr_container IS NOT BOUND.
   
"创建容器
   
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_data[] ).

   
"附加刷新按钮
   
DATA: lr_functions TYPE REF TO cl_salv_functions_list.
    lr_functions
= gr_table->get_functions( ).
    lr_functions
->set_all( abap_true ).
   
INCLUDE <icon>.
   
DATA: l_icon TYPE string.
    l_icon
= icon_refresh.
   
"附加按钮,只适用于 可控模式 下的SALV
    lr_functions
->add_function(
      name
= 'REFRESH'
     
icon = l_icon
     
text = '
刷新按钮'
      tooltip
= '刷新数据'
     
"按钮存放的位置:这里在右边附加。该参数的其他取值可以查看该方法源码,可以
     
"找到该参数其他取值的蛛丝马迹
     
position = if_salv_c_function_position=>right_of_salv_functions ).

   
"=====获取事件对象
   
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_user_command FOR lr_event.

   
"显示
    gr_table
->display( ).

 
ENDIF.
ENDMODULE.

FORM handle_user_command USING p_function TYPE salv_de_function.
 
CASE p_function.
   
WHEN 'REFRESH'.
     
PERFORM inital.
      gr_table
->refresh( ).
 
ENDCASE.
ENDFORM.

红绿灯、灯提示信息

image125

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA: BEGIN OF gt_data OCCURS 0,
  light
(1), "信息灯列
  val
(1),
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[] ).


  gr_columns
= gr_table->get_columns( ).
  gr_columns
->set_exception_column( 'LIGHT' ).

  gr_column ?= gr_columns
->get_column( 'LIGHT' ).
  "如果不通过下面set_ddic_reference方法来设置参
  "
照某词典类型,则异常列标题固定显示为 Exception
  gr_column
->set_long_text( '异常灯' ).
 
DATA: s_ddic TYPE salv_s_ddic_reference.
  s_ddic
-field = 'FIELD'.
  s_ddic
-table = 'SALV_S_DDIC_REFERENCE'.

  gr_column
->set_ddic_reference( s_ddic ).
  gr_column ?= gr_columns
->get_column( 'VAL' ).
  gr_column
->set_long_text( '异常值' ).

 
"==========为异常列单无格不同的值添加不同的冒泡提示
 
DATA:lr_functional_settings TYPE REF TO cl_salv_functional_settings.
  lr_functional_settings
= gr_table->get_functional_settings( ).
 
DATA: lr_tooltips TYPE REF TO cl_salv_tooltips.
  lr_tooltips
= lr_functional_settings->get_tooltips( ).

  lr_tooltips
->add_tooltip(
 
"单元格显示类型:异常灯、图标、符号。即这里针对的是类型为 异常列 所有的单元格
   
type = cl_salv_tooltip=>c_type_exception
   
value = space"
不同的图形不同的冒泡提示
    tooltip
= 'Undefined').
  lr_tooltips
->add_tooltip(
   
type = cl_salv_tooltip=>c_type_exception
   
value = '1'

    tooltip
= '红灯').
  lr_tooltips
->add_tooltip(
   
type = cl_salv_tooltip=>c_type_exception
   
value = '2'

    tooltip
= '黄灯').
  lr_tooltips
->add_tooltip(
   
type = cl_salv_tooltip=>c_type_exception
   
value = '3'

    tooltip
= '绿灯').
  gr_table
->display( ). "调用实例方法显示表报

FORM inital .
  gt_data
-light = ' '. "取值范围为:空、1~3
  gt_data
-val = ''.
 
APPEND gt_data .

  gt_data
-light = '1'.
  gt_data
-val = '1'.
 
APPEND gt_data .

  gt_data
-light = '2'.
  gt_data
-val = '2'.
 
APPEND gt_data .

  gt_data
-light = '3'.
  gt_data
-val = '3'.
 
APPEND gt_data .
ENDFORM.

图标显示、图标提示信息

image126

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.
INCLUDE <icon>.
DATA: BEGIN OF gt_data OCCURS 0,
 
icon TYPE icon_d, "图标
  val
TYPE string,
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table
   
CHANGING t_table = gt_data[] ).


  gr_columns
= gr_table->get_columns( ).
  gr_column ?= gr_columns->get_column( 'ICON' ).
  gr_column
->set_icon( if_salv_c_bool_sap=>true ).
  gr_column
->set_long_text( '图标' ).

  gr_column ?= gr_columns
->get_column( 'VAL' ).
  gr_column
->set_long_text( '图标值' ).

 
"==========为图标列单无格不同的值添加不同的冒泡提示
 
DATA:lr_functional_settings TYPE REF TO cl_salv_functional_settings.
  lr_functional_settings
= gr_table->get_functional_settings( ).
 
DATA: lr_tooltips TYPE REF TO cl_salv_tooltips.
  lr_tooltips
= lr_functional_settings->get_tooltips( ).

 
DATA: lvc_value TYPE lvc_value.
  lvc_value
= icon_locked.
  lr_tooltips
->add_tooltip(
 
"单元格显示类型:异常灯、图标、符号。即这里针对的是类型为 图标列 所有的单元格
   
type = cl_salv_tooltip=>c_type_icon
   
value = lvc_value"
不同的图形不同的冒泡提示
    tooltip
= '锁定').
  lvc_value
= icon_message_warning.
  lr_tooltips
->add_tooltip(
   
type = cl_salv_tooltip=>c_type_icon
   
value =
lvc_value
    tooltip
= '
警告').
  lvc_value
= icon_unlocked.
  lr_tooltips
->add_tooltip(
   
type = cl_salv_tooltip=>c_type_icon
   
value =
lvc_value
    tooltip
= '
解锁').

  gr_table
->display( ). "调用实例方法显示表报

FORM inital .

  gt_data
-icon = icon_locked.
 
"在前面加了等号,如果不加,也会直接输出图标,而不是文字
  gt_data
-val = `=@06@`.
 
APPEND gt_data .

  gt_data
-icon = icon_message_warning.
  gt_data
-val = `=@1A@`.
 
APPEND gt_data .

  gt_data
-icon = icon_unlocked.
  gt_data
-val = `=@07@`.
 
APPEND gt_data .
ENDFORM.

设置单元格颜色

image127

DATA: gr_table TYPE REF TO cl_salv_table.
DATA: gr_columns TYPE REF TO cl_salv_columns_table.
DATA: gr_column TYPE REF TO cl_salv_column_table.

DATA: g_color TYPE lvc_s_colo.

DATA:BEGIN OF gt_data OCCURS 0,
  key1
TYPE mara-matnr,
  key2
(2),
 
color TYPE lvc_t_scol,
 
END OF gt_data.

START-OF-SELECTION.
 
PERFORM inital.
  cl_salv_table
=>factory(
   
IMPORTING r_salv_table = gr_table"用来接收工厂产生的实例
   
CHANGING t_table = gt_data[] ).

 
FIELD-SYMBOLS: <ls_outtab> LIKE gt_data.
 
DATA: lt_color TYPE lvc_t_scol,
        ls_color
TYPE lvc_s_scol.

 
LOOP AT gt_data ASSIGNING <ls_outtab>.
   
CLEAR: ls_color,lt_color.
   
IF <ls_outtab>-key1 = 'a'.
      ls_color
-fname = 'KEY1'."为哪列设置颜色
      ls_color
-color-col = '6'."颜色值
      ls_color
-color-int = '0'."加深
      ls_color
-color-inv = '0'."前景背景颜色反转
     
APPEND ls_color TO  lt_color.
   
ENDIF.
   
IF <ls_outtab>-key2 = 'bb'.
      ls_color
-fname = 'KEY2'.
      ls_color
-color-col = '5'.
      ls_color
-color-int = '0'.
      ls_color
-color-inv = '0'.
     
APPEND ls_color TO  lt_color.
   
ENDIF.
    <ls_outtab>
-color = lt_color.
 
ENDLOOP.

  gr_columns
= gr_table->get_columns( ).
  gr_columns
->set_color_column( 'COLOR' ).
  gr_table
->display( ).

FORM inital .
  gt_data
-key1 = 'a'.
  gt_data
-key2 = 'aa'.
 
APPEND gt_data.
  gt_data
-key1 = 'b'.
  gt_data
-key2 = 'bb'.
 
APPEND gt_data.
ENDFORM.

posted @ 2015-02-14 11:30  江正军  阅读(33417)  评论(2编辑  收藏  举报