SALV 介绍和简单的SALV案例
一、SALV介绍
SAP Netweaver 04SAP引入了一个新的Class Cl_SALV 来进行面向对象(OO ALV) 的开发。在SAP Netweaver之前,我们可以根据ALV的展现方式(Tabular, Tree, Hierarchical),ALV的类型(List or Grid)来决定用什么来开发ALV.比如,如果想用控件框架,我们要用到class CL_GUI_ALV_GRID来创建ALV,为避免这种选择综合症,SAP推出了一个万能模式–完全基于面向对象的ALV类家族,如下:
ALV Flavour | Class |
---|---|
Simple 2D table display | CL_SALV_TABLE |
Hierarchical ALV display | CL_SALV_HIERSEQU_TABLE |
Tree AlV using class | CL_SALV_TREE |
CL_SALV_TABLE:一般ALV
CL_SALV_HIERSEQU_TABLE:以层次结构显示的ALV
CL_SALV_TREE:树形结构ALV
SALV有点很多,SALV可以像使用函数方式生成ALV那样,不用创建屏幕就可以调用的全屏方式显示的ALV
而且最重要的是,SALV的GRID报表可以在后台运行
二、实现与代码
所有的SALV类都是通过静态方法,CL_SALV_TABLE=>FACTORY 生成ALV的实例(instance),然后调用实例方法display显示ALV
下面代码是通过class CL_SALV_TABLE实现SALV的简单实例:
*&---------------------------------------------------------------------*
*& 一个简单的通过类CL_SALV_TABLE生成SALV的例子
*&---------------------------------------------------------------------*
REPORT zabap_salv1.
*
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* 定义SALV输出内表
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,
kunnr TYPE kunnr,
END OF ty_vbak.
*
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV reference
DATA: o_alv TYPE REF TO cl_salv_table.
*
METHODS:
* 抽取数据
get_data,
*
* 生成输出内容
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS. "lcl_report DEFINITION
*
*
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
*
CREATE OBJECT lo_report.
*
lo_report->get_data( ).
*
lo_report->generate_output( ).
*
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* 抽取数据
SELECT vbeln
erdat
auart
kunnr
INTO TABLE t_vbak
FROM vbak
UP TO 20 ROWS.
*
ENDMETHOD. "get_data
*
*.......................................................................
METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.
*
* exception class
DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = t_vbak ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
* 调用Display方法将数据输出到屏幕上
o_alv->display( ).
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
*
ENDCLASS. "lcl_report IMPLEMENTATION