通过类CL_SALV_TABLE生成SALV的例子,显示如下:
*&---------------------------------------------------------------------*
*& 一个简单的通过类CL_SALV_TABLE生成SALV的例子
*&---------------------------------------------------------------------*
REPORT z_test.
CLASS lcl_report DEFINITION DEFERRED.
DATA: lo_report TYPE REF TO lcl_report.
* 事件响应类定义
CLASS lcl_event_handler DEFINITION.
*
PUBLIC SECTION.
METHODS:
on_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
*
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
* 定义SALV输出内表
TYPES: BEGIN OF ty_vbak,
check(1) TYPE c, "定义Checkbox用的字段
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..*$*$*
*
* 定义private method来设定SALV不同特性
PRIVATE SECTION.
METHODS:
set_pf_status CHANGING co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
ENDCLASS. "lcl_report DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* 类实现
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* 抽取数据
SELECT vbeln
erdat
auart
kunnr
INTO CORRESPONDING FIELDS OF TABLE t_vbak
FROM vbak
UP TO 1 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.
*...Get all the Columns
DATA: lo_cols TYPE REF TO cl_salv_columns.
DATA: lo_event_handler TYPE REF TO lcl_event_handler.
* 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
* 调用status设置方法
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*...Process individual columns
DATA: lo_column TYPE REF TO cl_salv_column_list.
*
lo_cols = o_alv->get_columns( ).
* Change the properties of the Columns KUNNR
TRY.
lo_column ?= lo_cols->get_column( 'CHECK' ).
lo_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
lo_column->set_output_length( 10 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
*
* Get the event object
DATA: lo_events TYPE REF TO cl_salv_events_table.
lo_events = o_alv->get_event( ).
*
* Instantiate the event handler object
CREATE OBJECT lo_event_handler.
*
* event handler
SET HANDLER lo_event_handler->on_link_click FOR lo_events.
o_alv->set_screen_popup(
start_column = 10
end_column = 110
start_line = 5
end_line = 15 ).
* 调用Display方法将数据输出到屏幕上
o_alv->display( ).
MESSAGE 'aa' TYPE 'I'.
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
* status设置方法实现
METHOD set_pf_status.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
* 设置默认的标准状态栏
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).
ENDMETHOD.
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
ENDCLASS. "lcl_report IMPLEMENTATION
START-OF-SELECTION.
CREATE OBJECT lo_report.
* 抽取数据
lo_report->get_data( ).
* 生成SALV
lo_report->generate_output( ).
*----------------------------------------------------------------------*
* 事件响应类实现
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
*
METHOD on_link_click.
*
* Get the value of the checkbox and set the value accordingly
* Refersh the table
FIELD-SYMBOLS: <lfa_data> LIKE LINE OF lo_report->t_vbak.
READ TABLE lo_report->t_vbak ASSIGNING <lfa_data> INDEX row.
CHECK sy-subrc IS INITIAL.
IF <lfa_data>-check IS INITIAL.
<lfa_data>-check = 'X'.
ELSE.
CLEAR <lfa_data>-check.
ENDIF.
lo_report->o_alv->refresh( ).
ENDMETHOD. "on_link_click
*
ENDCLASS. "lcl_event_handler IMPLEMENTATION