ABAP学习(20):OOSALV学习hierseq ALV

4、OO SALV hierseq

       利用函数和GRID的功能发布的SALV包程序(SALV也称为New ALV)。通过类cl_salv_hierseq_table,实现hierseq类型ALV的输出。

4.1显示hierseq ALV

       通过类cl_salv_hierseq_table类对象,显示hierseqALV输出。

实例代码:

"cl_salv_hierseq_table类对象
DATA:lo_hierseq_table TYPE REF TO cl_salv_hierseq_table.
DATA:lt_spfli TYPE TABLE OF spfli.
DATA:lw_spfli LIKE LINE OF lt_spfli.
"分组控制父节点
TYPES:BEGIN OF gs_master,
      mandt TYPE spfli-mandt,
      carrid TYPE spfli-carrid,
     END OF gs_master.
DATA:gt_master TYPE TABLE OF gs_master.
DATA:gw_master LIKE LINE OF gt_master.

START-OF-SELECTION.
  PERFORM get_data.
  PERFORM dis_hierseq.

"获取数据
FORM get_data.
  "查询数据
  SELECT * INTO TABLE lt_spfli FROM spfli.
  "获取父节点数据
  LOOP AT lt_spfli INTO lw_spfli.
     CLEAR gw_master.
     gw_master-mandt = lw_spfli-mandt.
     gw_master-carrid = lw_spfli-carrid.
     APPEND gw_master TO gt_master.
  ENDLOOP.
  SORT gt_master.
  DELETE ADJACENT DUPLICATES FROM gt_master.
ENDFORM.

"显示hierseq类型ALV
FORM dis_hierseq.
  "绑定关系类对象
  DATA:lt_binding type salv_t_hierseq_binding.
  DATA:ls_binding type salv_s_hierseq_binding.

  "创建父节点和显示内表之间绑定关系
  "按照这两个参数分组
  ls_binding-master = 'MANDT'.
  ls_binding-slave  = 'MANDT'.
  append ls_binding to lt_binding.
  ls_binding-master = 'CARRID'.
  ls_binding-slave  = 'CARRID'.
  append ls_binding to lt_binding.

  "实例化cl_salv_hierseq_table对象
   TRY .
      cl_salv_hierseq_table=>factory(
        EXPORTING
          t_binding_level1_level2 = lt_binding
        IMPORTING
          r_hierseq               = lo_hierseq_table
        CHANGING
          t_table_level1           = gt_master
          t_table_level2           = lt_spfli ).
    CATCH cx_salv_data_error cx_salv_not_found.
   ENDTRY.

   "显示hierseqALV
   lo_hierseq_table->display( ).
ENDFORM.

4.2设置function

1.设置通用alv function

在调用cl_salv_hierseq_table类对象lo_hierseq_table的display方法前,设置function

实例代码:

"设置function
FORM set_function.
  DATA:lo_functions TYPE REF TO cl_salv_functions_list.
  "获取function设置类对象
  lo_functions = lo_hierseq_table->get_functions( ).
  "设置默认function
  "lo_functions->set_default( abap_true ).
  "设置所有通用function
  lo_functions->set_all( abap_true ).
  "设置自定义GUI STATUS
  "lo_hierseq_table->set_screen_status( EXPORTING pfstatus = 'ALV_STANDARD' report = sy-repid ).
ENDFORM.

4.3设置列

          通过cl_salv_hierseq_table类对象lo_hierseq_table的get_columns方法获取cl_salv_columns_hierseq类对象lo_columns。通过get_columns传入参数level= 1 or 2,获取master or slave节点。

cl_salv_columns_hierseq类对象lo_columns的方法:

       set_expand_column( expand字段 )方法,设置父节点是否可展开,父节点gt_master结构中必须有expand字段;

       set_optimize方法设置优化列宽;

       set_exception_column方法设置exception列,传入参数为exception对应字段名;

       set_color_column方法设置颜色控制列,传入参数为颜色控制列对应字段名;

       set_cell_type_column设置单元格类型控制列,传入参数为单元格控制列对应字段名;

       get_column方法获取cl_salv_column_hierseq类对象lo_column,设置单独列类型,列背景色等;

       通过cl_salv_hierseq_table类对象lo_hierseq_table的get_level方法获取cl_salv_hierseq_level类对象lo_level。

cl_salv_hierseq_level类对象lo_level方法:

  set_items_expanded方法设置父节点下子节点是否展开;

显示内表结构:

TYPES:BEGIN OF s_spfli.
  INCLUDE TYPE spfli.
  TYPES: exception TYPE C LENGTH 1.           "exception字段
  TYPES: checkbox TYPE abap_bool.             "checkbox字段
  TYPES: icon TYPE icon_d.                    "icon字段
  TYPES: symbol TYPE icon_d.                  "symbol字段
  TYPES: hotspot TYPE icon_d.                 "hotspot字段
  TYPES: t_color TYPE lvc_t_scol.             "控制行背景色
  TYPES: t_celltype TYPE salv_t_int4_column,  "控制单元格类型
     END OF s_spfli.
DATA:lt_spfli TYPE TABLE OF s_spfli.
DATA:lw_spfli LIKE LINE OF lt_spfli.
"分组控制父节点
TYPES:BEGIN OF gs_master,
      mandt TYPE spfli-mandt,
      carrid TYPE spfli-carrid,
      expand TYPE c,  "是否展开
     END OF gs_master.
DATA:gt_master TYPE TABLE OF gs_master.
DATA:gw_master LIKE LINE OF gt_master.

实例代码:

"设置列
FORM set_columns.
  DATA:lo_columns TYPE REF TO cl_salv_columns_hierseq.
  DATA:lo_column TYPE REF TO cl_salv_column_hierseq.
  DATA:lo_level TYPE REF TO cl_salv_hierseq_level.

  "批量设置单元,行颜色,类型
  DATA:lt_color TYPE lvc_t_scol.
  DATA:ls_color TYPE lvc_s_scol.
  DATA:lt_celltype TYPE salv_t_int4_column.
  DATA:ls_celltype TYPE salv_s_int4_column.

  TRY .
    "获取columns类对象
    "level就是对应设置父节点为1,子节点内容为2
    lo_columns = lo_hierseq_table->get_columns( level = 1 ).
    "获取父节点中列
    lo_column ?= lo_columns->get_column( 'MANDT' ).
    "设置MANDT不显示,这里只是汇总父节点上MANDT列不显示,子节点依旧显示MANDT信息
*    lo_column->set_technical( abap_true ).
*    lo_column->set_visible( abap_true ).

    "设置展开父节点
    lo_columns->set_expand_column( 'EXPAND' ).
    "获取level对象
    lo_level = lo_hierseq_table->get_level( 1 ).
    "设置父节点下item展开
    lo_level->set_items_expanded( ).

    "设置icon,exception,symbol等域值
    "include <icon>,点击进去可以看到icon值
    "include <symbol>,点击进去可以看到symbol值
    LOOP AT lt_spfli INTO lw_spfli.
      DATA:lv_num TYPE I.
      lv_num = sy-tabix mod 4.
      CASE lv_num.
        WHEN 0.
          lw_spfli-exception = lv_num.
          lw_spfli-checkbox = abap_true.
          lw_spfli-icon = icon_okay.
          lw_spfli-symbol = sym_locked.
          lw_spfli-hotspot = icon_overview.
        WHEN 1.
          lw_spfli-exception = lv_num.
          lw_spfli-checkbox = abap_false.
          lw_spfli-icon = icon_cancel.
          lw_spfli-symbol = sym_unlocked.
          lw_spfli-hotspot = icon_overview.
        WHEN 2.
          lw_spfli-exception = lv_num.
          lw_spfli-checkbox = abap_true.
          lw_spfli-icon = icon_okay.
          lw_spfli-symbol = sym_locked.
          lw_spfli-hotspot = icon_overview.
        WHEN 3.
          lw_spfli-exception = lv_num.
          lw_spfli-checkbox = abap_true.
          lw_spfli-icon = icon_okay.
          lw_spfli-symbol = sym_locked.
          lw_spfli-hotspot = icon_overview.

          "设置整行背景颜色
          CLEAR ls_color.
          CLEAR lt_color[].
          ls_color-color-col = col_positive.
          ls_color-color-int = 0.
          ls_color-color-inv = 0.
          APPEND ls_color TO lt_color.
          lw_spfli-t_color = lt_color.

          "设置某行单元格类型
          CLEAR ls_celltype.
          CLEAR lt_celltype[].
          "当columnname为space为设置整行为对应类型,
          "否则只对应列名设置为对应类型
          ls_celltype-columnname = 'CONNID'.
          ls_celltype-value = IF_SALV_C_CELL_TYPE=>HOTSPOT.
          APPEND ls_celltype TO lt_celltype.
          lw_spfli-t_celltype = lt_celltype.
      ENDCASE.
      MODIFY lt_spfli FROM lw_spfli.
    ENDLOOP.

    "设置优化列宽
    lo_columns->set_optimize( abap_true ).

    "获取子节点行,设置子节点列
    lo_columns = lo_hierseq_table->get_columns( level = 2 ).
    "设置technical列
    lo_column ?= lo_columns->get_column( 'MANDT' ).
    lo_column->set_technical( abap_true ).
    "设置是否可见
    "lo_column->set_visible( abap_true ).

    "设置exception列
    lo_columns->set_exception_column( 'EXCEPTION' ).
    "设置icon列
    lo_column ?= lo_columns->get_column( 'ICON' ).
    lo_column->set_icon( abap_true ).
    lo_column->set_long_text( 'ICON' ).
    "设置symbol列
    lo_column ?= lo_columns->get_column( 'SYMBOL' ).
    lo_column->set_symbol( abap_true ).
    lo_column->set_long_text( 'SYMBOL' ).
    "设置checkbox列
    lo_column ?= lo_columns->get_column( 'CHECKBOX' ).
    lo_column->set_cell_type( IF_SALV_C_CELL_TYPE=>CHECKBOX ).
    lo_column->set_long_text( 'CHECKBOX' ).
    "设置hotspot列
    lo_column ?= lo_columns->get_column( 'HOTSPOT' ).
    lo_column->set_cell_type( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
    lo_column->set_icon( abap_true ).
    lo_column->set_long_text( 'HOTSPOT' ).

    "设置列背景色
    "include <color>,点击可以看到有多种color
    lo_column ?= lo_columns->get_column( 'CARRID' ).
    DATA: ls_colo TYPE lvc_s_colo.
    ls_colo-col = col_negative.
    ls_colo-int = 0.
    ls_colo-inv = 0.
    lo_column->set_color( ls_colo ).

    "设置颜色控制列生效
    lo_columns->set_color_column( 'T_COLOR' ).

    "设置单元格类型控制列生效
    lo_columns->set_cell_type_column( 'T_CELLTYPE' ).
  CATCH cx_salv_not_found cx_salv_data_error.
  ENDTRY.
ENDFORM.
View Code

 

4.4设置tooltips

       通过cl_salv_hierseq_table类对象lo_hierseq_table的get_functional_settings方法获取cl_salv_functional_settings类对象lo_functional_settings;

       通过lo_functional_settings调用方法get_tooltips,获取cl_salv_tooltips类对象lo_tooltips;

       通过lo_tooltips调用方法add_tooltip,添加tooltip;

实例代码:

"设置tooltips
FORM set_tooltips.
  DATA:lo_functional_settings TYPE REF TO cl_salv_functional_settings.
  DATA:lo_tooltips TYPE REF TO cl_salv_tooltips.
  DATA:lo_value TYPE lvc_value.

  "获取functional_setting对象
  lo_functional_settings = lo_hierseq_table->get_functional_settings( ).
  "获取tooltips对象
  lo_tooltips = lo_functional_settings->get_tooltips( ).

  "添加tooltip
  "type:单元格类型
  "value:单元格值
  "显示tooltip信息
  "好像只能设置icon,exception,symbol,color类型tooltip
  TRY.
      lo_tooltips->add_tooltip(
        type       = cl_salv_tooltip=>c_type_exception
        value      = '1'
        tooltip    = 'My Red Light Tooltip' ).

      lo_value = icon_okay.
      lo_tooltips->add_tooltip(
        type       = cl_salv_tooltip=>c_type_icon
        value      = lo_value
        tooltip    = 'ok' ).
    CATCH cx_salv_existing.
  ENDTRY.
ENDFORM.
View Code

4.5 设置events事件

          定义事件处理类lcl_handle_events,并实现相应处理逻辑。

          通过cl_salv_hierseq_table类对象lo_hierseq_table,调用方法get_event方法获取cl_salv_events_hierseq类对象lo_events;

          创建lcl_handle_events类对象gr_events;

          注册gr_events事件处理方法给lo_events对应事件;

实例代码:

"定义事件处理类
CLASS lcl_handle_events DEFINITION.
  PUBLIC SECTION.
    "用户自定义function
    METHODS:on_user_command FOR EVENT added_function OF cl_salv_events_hierseq IMPORTING E_SALV_FUNCTION.
    "执行function前调用
    METHODS:on_before_salv_function FOR EVENT before_salv_function OF cl_salv_events_hierseq IMPORTING E_SALV_FUNCTION.
    "执行function后调用
    METHODS:on_after_salv_function FOR EVENT after_salv_function OF  cl_salv_events_hierseq IMPORTING E_SALV_FUNCTION.
    "双击
    METHODS:on_double_click FOR EVENT double_click OF cl_salv_events_hierseq
        IMPORTING level row column.
    "单击,hotspot
    METHODS:on_link_click FOR EVENT link_click OF cl_salv_events_hierseq
        IMPORTING level row column.
ENDCLASS.
"实现事件处理类
CLASS lcl_handle_events IMPLEMENTATION.
  METHOD on_user_command.
    MESSAGE s000(0k) WITH e_salv_function.
  ENDMETHOD.
  METHOD on_before_salv_function.
    MESSAGE s000(0k) WITH 'before:' e_salv_function.
  ENDMETHOD.
  METHOD on_after_salv_function.
    MESSAGE s000(0k) WITH 'after:' e_salv_function.
  ENDMETHOD.
  METHOD on_double_click.
    MESSAGE s000(0k) WITH 'level:' level 'row:' row.
  ENDMETHOD.
  METHOD on_link_click.
    MESSAGE s000(0k) WITH 'level:' level 'row:' row.
  ENDMETHOD.
ENDCLASS.

"设置事件
FORM set_events.
  DATA:lo_events TYPE REF TO cl_salv_events_hierseq.
  DATA:gr_events TYPE REF TO lcl_handle_events.
  "获取事件类对象
  lo_events = lo_hierseq_table->get_event( ).
  "创建事件处理类对象
  CREATE OBJECT gr_events.
  "注册事件处理类给事件类对象
  SET HANDLER gr_events->on_user_command FOR lo_events.
  SET HANDLER gr_events->on_before_salv_function FOR lo_events.
  SET HANDLER gr_events->on_after_salv_function FOR lo_events.
  SET HANDLER gr_events->on_double_click FOR lo_events.
  SET HANDLER gr_events->on_link_click FOR lo_events.
ENDFORM.
View Code

4.6 设置display

          通过cl_salv_hierseq_table类对象lo_hierseq_table的get_display_settings方法获取cl_salv_display_settings类对象lo_display_settings;

          调用cl_salv_display_settings类对象方法,设置ALV显示;

实例代码:

"设置display
FORM set_display.
  DATA:lo_display_settings TYPE REF TO cl_salv_display_settings.
  "获取display_settings类对象
  lo_display_settings = lo_hierseq_table->get_display_settings( ).
  "设置header文本
  lo_display_settings->set_list_header( 'list header info' ).
  "设置sort时是否合并
  lo_display_settings->set_no_merging( abap_true ).
  "设置竖直线
  lo_display_settings->set_vertical_lines( abap_true ).
  "设置水平线
  lo_display_settings->set_horizontal_lines( abap_true ).
ENDFORM.
View Code

4.7设置显示为popup

          调用cl_salv_hierseq_table类对象lo_hierseq_table的set_screen_popup方法,设置显示为弹窗。

实例代码:

"设置显示为弹窗
FORM set_popup.
  "直接调用cl_salv_hierseq_table类对象方法set_screen_popup
  lo_hierseq_table->set_screen_popup(
    start_column = 1
    end_column = 100
    start_line = 2
    end_line = 50
   ).
ENDFORM.

4.8设置selection

          通过cl_salv_hierseq_table类对象lo_hierseq_table,调用get_selections方法获取cl_salv_selections类对象lo_selections。

          调用lo_selections方法设置selection选择行,列;

       注意:在hierseq list ALV中不能设置选择单元格;

实例代码:

"设置selections
"hierseq ALV list不能选择cell
FORM set_selections.
  DATA:lo_selections TYPE REF TO cl_salv_selections.
  "选择行列单元格
  DATA:lt_rows TYPE salv_t_row.
  DATA:lt_columns TYPE salv_t_column.
  DATA:ls_cell TYPE salv_s_cell.
  TRY.
    "获取selections对象
    lo_selections = lo_hierseq_table->get_selections( level = 1 ).

    "设置selection mode
    lo_selections->set_selection_mode( IF_SALV_C_SELECTION_MODE=>SINGLE ).

    "设置选择列
    APPEND 'CARRID' TO lt_columns.
    lo_selections->set_selected_columns( lt_columns ).

    "设置选择行
    APPEND 1 TO lt_rows.
    APPEND 2 TO lt_rows.
    lo_selections->set_selected_rows( lt_rows ).

    "获取选择列
    lo_selections->get_selected_columns( ).
    "获取选择行
    lo_selections->get_selected_rows( ).
    "获取当前单元格
    lo_selections->get_current_cell( ).
    "设置当前单元格
    ls_cell-row = 3.
    ls_cell-columnname = 'CONNID'.
    lo_selections = lo_hierseq_table->get_selections( level = 2 ).
    lo_selections->set_current_cell( ls_cell ).
  CATCH cx_salv_not_found.
  ENDTRY.
ENDFORM.
View Code

4.9刷新显示

          直接调用cl_salv_hierseq_table类对象lo_hierseq_table方法refresh刷新屏幕显示。

实例代码:

"刷新显示
FORM set_refresh.
  "刷新显示
  lo_hierseq_table->refresh( ).
ENDFORM.

4.10关闭窗口

          直接调用cl_salv_hierseq_table类对象lo_hierseq_table方法close_screen关闭窗口。

实例代码:

"关闭窗口
FORM set_close_screen.
  "关闭窗口
  lo_hierseq_table->close_screen( ).
ENDFORM.

4.11设置layout

实例代码:

"设置layout
FORM set_layout.
  DATA:lo_layout TYPE REF TO cl_salv_layout.
  DATA:ls_key TYPE salv_s_layout_key.
  DATA:ls_layout_info TYPE salv_s_layout_info.
  DATA:layout TYPE disvariant-variant.

  "获取layout类对象
  lo_layout = lo_hierseq_table->get_layout( ).
  "设置layout key
  ls_key-report = sy-repid.
  lo_layout->set_key( ls_key ).
  "设置保存限制
  "RESTRICT_USER_DEPENDANT:仅保持user可用
  "RESTRICT_NONE:无限制
  "RESTRICT_USER_INDEPENDANT:所有user可用
  lo_layout->set_save_restriction( IF_SALV_C_LAYOUT=>RESTRICT_NONE ).
  "设置使用default layout
  lo_layout->set_default( abap_true ).
  "设置初始化layout
  ls_layout_info = cl_salv_layout_service=>f4_layouts(
    s_key    = ls_key
    restrict =  IF_SALV_C_LAYOUT=>RESTRICT_NONE ).
  layout = ls_layout_info-layout.
  lo_layout->set_initial_layout( layout ).

  "获取当前layout
  lo_layout->get_current_layout( ).
  "获取默认layout
  lo_layout->get_default_layout( ).
  "获取初始化layout
  lo_layout->get_initial_layout( ).
  "获取f4layout
  lo_layout->f4_layouts( ).

ENDFORM.
View Code

4.12设置排序、汇总、过滤器

          通过cl_salv_hierseq_table类对象lo_hierseq_table获取cl_salv_aggregations类对象,lo_aggregations设置汇总;

          通过cl_salv_hierseq_table类对象lo_hierseq_table获取cl_salv_sorts类对象,lo_sorts设置排序;

          通过cl_salv_hierseq_table类对象lo_hierseq_table获取cl_salv_filters类对象,lo_filters设置过滤器;

实例代码:

"设置排序,汇总
FORM set_sort_agg.
   DATA:lo_aggregations TYPE REF TO cl_salv_aggregations.
   DATA:lo_sorts TYPE REF TO cl_salv_sorts.
   DATA:lo_filters TYPE REF TO cl_salv_filters.
   TRY .
   "获取汇总对象
   lo_aggregations = lo_hierseq_table->get_aggregations( level = 2 ).
   "columnname:汇总列,aggregation汇总规则
   "AVERAGE:均值,TOTAL:加总,MINIMUM最小值,MAXIMUM最大值
   lo_aggregations->add_aggregation(
    EXPORTING
      columnname = 'PERIOD'
      aggregation = IF_SALV_C_AGGREGATION=>TOTAL ).
   lo_aggregations->SET_NUMERICAL_AGGREGATION( abap_true ).

   "获取排序对象
   "subtotal是否子item分组汇总
   "sequence排序规则
   lo_sorts = lo_hierseq_table->get_sorts( level = 1 ).
   lo_sorts->add_sort(
    columnname = 'CARRID'
    sequence = IF_SALV_C_SORT=>SORT_DOWN
    subtotal = abap_true
   ).

   "获取过滤器对象
   lo_filters = lo_hierseq_table->get_filters( level = 1 ).
   lo_filters->add_filter(
    columnname = 'CARRID'
    low = 'AA'
    sign = 'I'
    option = 'EQ'
   ).
      CATCH cx_salv_not_found cx_salv_data_error cx_salv_existing.
   ENDTRY.
ENDFORM.
View Code

 

 

 

 

 

posted @ 2020-05-23 14:08  渔歌晚唱  阅读(963)  评论(0编辑  收藏  举报