欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

OO实现ALV-SALV-实战攻略4-SALV显示样式

通过类CL_SALV_DISPLAY_SETTINGS我们可以设置ALV的显示样式,主要包括ALV表头名称,列表颜色间隔显示,水平线是否显示,竖线是否显示等。

我们要取得类CL_SALV_DISPLAY_SETTINGS的实例,根据类CL_SALV_TABLE的方法get_display_settings可以取得显示样式的实例。

REPORT ZSALV-01.
*SALV自定义按钮功能
*变量定义
DATA: S_ALV TYPE REF TO CL_SALV_TABLE.
"class 定义
CLASS SALV_DISPLAY DEFINITION."基础类
  PUBLIC SECTION.
    METHODS: DISPLAYDATA ."方法
    EVENTS:FUNCTION ."事件
  PRIVATE SECTION.
    DATA:LR_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS,
         LR_EVENTS    TYPE REF TO CL_SALV_EVENTS,
         LR_DISPLAY   TYPE REF TO CL_SALV_DISPLAY_SETTINGS.
ENDCLASS.
CLASS SET_HANDLER DEFINITION.
  PUBLIC SECTION.
    "理解为为某个类的事件而产生的方法,相当于将2个类关联起来
    METHODS:USER_COMMAND FOR EVENT ADDED_FUNCTION OF CL_SALV_EVENTS_TABLE"直接使用该类的function事件
      IMPORTING E_SALV_FUNCTION.
ENDCLASS.

"class 实现
CLASS SALV_DISPLAY IMPLEMENTATION.
  METHOD DISPLAYDATA.
    SELECT * INTO TABLE @DATA(LT_VBAK) FROM VBAK UP TO 10 ROWS.
    TRY.
        CL_SALV_TABLE=>FACTORY( IMPORTING R_SALV_TABLE   = S_ALV  CHANGING T_TABLE = LT_VBAK ).
      CATCH CX_SALV_MSG .
    ENDTRY.
    LR_EVENTS = S_ALV->GET_EVENT( )."获取事件
    "设置ALV按钮功能
    S_ALV->SET_SCREEN_STATUS(
    PFSTATUS      =  'SALV_STANDARD'
    REPORT        =  SY-REPID
    SET_FUNCTIONS = S_ALV->C_FUNCTIONS_ALL ).
*-------------------------- 设置ALV的显示样式-------------------------------------------
    LR_DISPLAY = S_ALV->GET_DISPLAY_SETTINGS( ).
    LR_DISPLAY->SET_STRIPED_PATTERN( 'X' )."设置颜色间隔显示
    LR_DISPLAY->SET_LIST_HEADER( 'SLV展示报表DEMO' ). "设置ALV的抬头
    LR_DISPLAY->SET_HORIZONTAL_LINES( '' ).  "设置水平线是否显示
    LR_DISPLAY->SET_VERTICAL_LINES( '' ).   "设置垂直线是否显示
*-------------------------- 设置ALV的显示样式-------------------------------------------
    "=====事件注册
    DATA:HANDLER TYPE REF TO SET_HANDLER.
    CREATE OBJECT HANDLER.
    SET HANDLER HANDLER->USER_COMMAND FOR ALL INSTANCES.
    S_ALV->DISPLAY( ).
  ENDMETHOD.
ENDCLASS.

CLASS SET_HANDLER IMPLEMENTATION.
  METHOD USER_COMMAND.
    PERFORM USER_CLICK USING E_SALV_FUNCTION.
  ENDMETHOD.

ENDCLASS.
FORM USER_CLICK USING P_SALV_FUNCTION TYPE SALV_DE_FUNCTION.
  CASE P_SALV_FUNCTION.
    WHEN '&CLICK'.
      MESSAGE '你点击了我!' TYPE 'I'.
      S_ALV->REFRESH( ).
  ENDCASE.
ENDFORM.

START-OF-SELECTION.
  DATA:LR_ALV TYPE REF TO SALV_DISPLAY.
  CREATE OBJECT LR_ALV.
  LR_ALV->DISPLAYDATA( ).

 

 

 显示效果可以和上一篇作对比

posted @ 2021-04-12 09:39  萧静默  阅读(360)  评论(0编辑  收藏  举报