ABAP学习(11):ALV显示之OO ALV使用示例

2、OO ALV

    OOALV主要通过CL_GUI_ALV_GRID这个类来控制alv的显示。

ALV显示需要屏幕容器,容器对应类:

1、cl_gui_custom_container,默认容器alv自动占满整个容器;

2、cl_gui_docking_container,docking容器alv宽度可以直接调整;

3、cl_gui_splitter_contianer,splitter容器,可以将屏幕划分区域显示多个alv;

 

2.1、cl_gui_custom_container容器

示例1:OO ALV使用cl_gui_custom_container容器

TABLES:spfli.

CLASS cl_event_handle DEFINITION."事件处理类定义
  PUBLIC SECTION.
    "初始化ALV工具栏对象事件,如增加按钮并设定属性
    METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING
              e_object
              e_interactive.
    "该事件用于在下ALV工具栏的下拉菜单按钮中增加选项
    METHODS handle_menu_button FOR EVENT menu_button OF cl_gui_alv_grid
            IMPORTING
              e_object
              e_ucomm.
    "ALV工具栏按钮的点击事件
    METHODS handle_user_command FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING
              e_ucomm.

    "ALV表格双击事件
    METHODS handle_double_click FOR EVENT double_click OF cl_gui_alv_grid
            IMPORTING
              e_row     "作废
              e_column  "通过e_column-fieldname 获取点击字段名,点击列
              es_row_no. "通过es_row_no-row_id 返回当前行号


    "字段的字段目录HOTSPOT设置为"X",热点单击事件
    METHODS handle_hotspot_click FOR EVENT HOTSPOT_CLICK of cl_gui_alv_grid
            IMPORTING
              E_ROW_ID "作废
              E_COLUMN_ID "通过e_column_id-fieldname 获取点击字段名,点击列
              ES_ROW_NO. "通过es_row_no-row_id 返回当前行号
ENDCLASS.

DATA: gs_toolbar TYPE stb_button.
DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.

CLASS cl_event_handle IMPLEMENTATION."事件处理类实现部分
  METHOD handle_toolbar.
    gs_toolbar-function = 'B_SUM'."按钮的FunctionCode
    gs_toolbar-icon = icon_display."按钮图标,通过Tcode:ICON可以查看
    gs_toolbar-text = '总行数'."按钮标签
    gs_toolbar-butn_type = '0'."定义按钮类型,0为标准按钮,具体取值可参考这里
    APPEND gs_toolbar TO e_object->mt_toolbar."添加按钮到工具栏中

    gs_toolbar-function = 'B_LIST'."按钮的FunctionCode
    gs_toolbar-quickinfo = '自定义下拉菜单按钮'."按钮的冒泡提示
    gs_toolbar-icon = icon_biw_report_view."按钮图标
    gs_toolbar-text = '下拉菜单按钮'."按钮标签
    gs_toolbar-butn_type = '1'."定义按钮类型,1为下拉菜单按钮
    APPEND gs_toolbar TO e_object->mt_toolbar."添加下拉菜单按钮到工具栏中
  ENDMETHOD.

  METHOD handle_menu_button.
    IF e_ucomm = 'B_LIST'."给下拉菜单按钮增加选项,可以多次调用该方法以增加多行
      CALL METHOD e_object->add_function
        EXPORTING
          icon  = icon_display
          fcode = 'B_SUM'"字菜单按钮的FunCode
          text  = '显示ALV总行数'.
    ENDIF.
  ENDMETHOD.
  METHOD handle_user_command.
    DATA: sum TYPE i .
    IF e_ucomm = 'B_SUM'.
      DESCRIBE TABLE t_sflight[] LINES sum.
      MESSAGE i001(00) WITH '当前ALV表格中的数据总行数为:' sum.
    ENDIF.
  ENDMETHOD.

  "双击事件
  METHOD  handle_double_click.
    DATA:temp_message TYPE String.
    READ TABLE t_sflight INTO t_sflight INDEX es_row_no-row_id.
    temp_message = '点击列:' && e_column-fieldname && ',行号:' && es_row_no-row_id.
    MESSAGE i001(00) WITH temp_message.
  ENDMETHOD.

  "字段的字段目录HOTSPOT设置为"X",热点单击事件
  METHOD handle_hotspot_click.
    DATA:temp_message TYPE String.
    temp_message = '点击列:' && e_column_id-fieldname && ',行号:' && es_row_no-row_id.
    MESSAGE i001(00) WITH temp_message.
  ENDMETHOD.
ENDCLASS.

"定义cl_gui_alv_grid 类引用
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
"定义cl_gui_custom_container 容器
DATA:alv_container TYPE REF TO cl_gui_custom_container.
"alv显示参数
"字段目录HOTSPOT,热点单击
DATA:fieldcat TYPE lvc_t_fcat.
DATA:s_fieldcat TYPE lvc_s_fcat.
DATA:layout TYPE lvc_s_layo.
"事件
DATA: event_handle TYPE REF TO cl_event_handle."定义类对象的引用

SELECTION-SCREEN BEGIN OF SCREEN 101 as SUBSCREEN.
    SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE title.
      SELECT-OPTIONS:
        s_carrid FOR spfli-carrid,
        s_connid FOR spfli-connid.
    SELECTION-SCREEN END OF BLOCK blk1.
SELECTION-SCREEN END OF SCREEN 101.

*初始化
INITIALIZATION.
  title = '查询条件'.

START-OF-SELECTION.
  CALL SCREEN 100.

MODULE STATUS_0100 OUTPUT.
   SET PF-STATUS 'STA_0100'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0100  OUTPUT

MODULE USER_COMMAND_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'EXEC'.
      "查询数据
       SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight
        WHERE carrid IN s_carrid AND connid IN s_connid.

      IF   alv_container  IS  INITIAL.
        CREATE  OBJECT    alv_container"创建ALV容器对象
                 EXPORTING container_name =  'CONTAINER1'.
        CREATE  OBJECT  alv_grid"创建ALV控件
                EXPORTING i_parent  =  alv_container.

        CALL METHOD alv_grid->set_table_for_first_display
          EXPORTING
            i_structure_name = 'sflight'
            is_layout        = layout
            i_save           = 'X'"可以保存变式
          CHANGING
            it_outtab        = t_sflight[]
            it_fieldcatalog  = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采用默认方式显示
        CREATE OBJECT :event_handle.
        "为ALV按钮注册监听事件
        SET HANDLER :event_handle->handle_toolbar FOR alv_grid,
                     event_handle->handle_menu_button FOR alv_grid,
                     event_handle->handle_user_command FOR alv_grid,
                     event_handle->handle_double_click FOR alv_grid,
                     event_handle->handle_hotspot_click FOR alv_grid.
        "调用此方法才能激活工具栏上增加的自定义按钮
        CALL METHOD alv_grid->set_toolbar_interactive.
      ELSE.
        CALL METHOD alv_grid->refresh_table_display.
      ENDIF.
    WHEN 'BACK' or 'EXIT' or 'QUIT'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

创建屏幕100逻辑代码:

PROCESS BEFORE OUTPUT.
   "屏幕输出前操作
   MODULE STATUS_0100.
   "调用子屏幕
   CALL SUBSCREEN sub1 INCLUDING sy-repid '101'.
*
PROCESS AFTER INPUT.
  "先调用子屏幕,后面module使用到子屏幕数据
  CALL SUBSCREEN sub1.
  "屏幕输入后操作
  MODULE USER_COMMAND_0100.

 

2.2、cl_gui_docking_container容器

示例2:使用cl_gui_docking_container容器:

TABLES:sflight.

"alv显示设置
DATA:fieldcat TYPE lvc_t_fcat.
DATA:layout TYPE lvc_s_layo.

"cl_gui_alv_grid类引用
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
DATA:alv_container TYPE REF TO cl_gui_docking_container.

DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.

START-OF-SELECTION.
  CALL SCREEN 100.

MODULE USER_COMMAND_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' or 'QUIT' or 'EXIT'.
      LEAVE TO SCREEN 0.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT

MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STA_0100'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  OUT_SCREEN  OUTPUT
*&---------------------------------------------------------------------*
*       查询数据,生成docking container对象,显示alv
*----------------------------------------------------------------------*
MODULE OUT_SCREEN OUTPUT.
  "查询数据
  SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight WHERE carrid = 'AA'.

  "创建container对象
  CREATE OBJECT alv_container
    EXPORTING
      repid     = sy-repid
      dynnr     = sy-dynnr
      extension = 300."alv宽度
  "创建alv对象
  CREATE OBJECT alv_grid
    EXPORTING
      i_parent = alv_container.
  "调用显示方法
  CALL METHOD alv_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'sflight'
      is_layout        = layout
      i_save           = 'X'"可以保存变式
    CHANGING
      it_outtab        = t_sflight[]
      it_fieldcatalog  = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采用默认方式显示
ENDMODULE.                 " OUT_SCREEN  OUTPUT

 

 

2.3、cl_gui_splitter_container容器

示例3:使用cl_gui_splitter_container容器

TABLES:sflight.

"alv显示设置
DATA:fieldcat TYPE lvc_t_fcat.
DATA:layout TYPE lvc_s_layo.
"cl_gui_alv_grid类引用
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
DATA:alv_grid1 TYPE REF TO cl_gui_alv_grid.
DATA:alv_container TYPE REF TO cl_gui_docking_container.
"spillter container 对象引用
DATA:alv_splitter_container TYPE REF TO cl_gui_splitter_container.
"屏幕容器
DATA:ref_container TYPE REF TO cl_gui_container.

DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.

START-OF-SELECTION.
  CALL SCREEN 100.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' or 'QUIT' or 'EXIT'.
      LEAVE TO SCREEN 0.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STA_0100'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  OUT_SCREEN  OUTPUT
*&---------------------------------------------------------------------*
*       查询数据,生成docking container对象,显示alv
*----------------------------------------------------------------------*
MODULE OUT_SCREEN OUTPUT.
  "查询数据
  SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight WHERE carrid = 'AA'.

  "创建container对象
  CREATE OBJECT alv_container
    EXPORTING
      repid     = sy-repid
      dynnr     = sy-dynnr
      extension = 1200."alv宽度
  "创建spillter container对象
  CREATE OBJECT alv_splitter_container
    EXPORTING
      parent = alv_container
      rows = 1
      columns = 2. "将父容器分为1行两列,两个容器

  "调用cl_splitter_container对象方法,获取容器
  CALL METHOD alv_splitter_container->get_container
    EXPORTING
      row = 1
      column = 1
    RECEIVING
      container = ref_container.

  "创建左边容器alv对象
  "创建alv对象
  CREATE OBJECT alv_grid
    EXPORTING
      i_parent = ref_container.

  "调用显示方法
  CALL METHOD alv_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'sflight'
      is_layout        = layout
      i_save           = 'X'"可以保存变式
    CHANGING
      it_outtab        = t_sflight[]
      it_fieldcatalog  = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采用默认方式显示

   "调用cl_splitter_container对象方法,获取容器
  CALL METHOD alv_splitter_container->get_container
    EXPORTING
      row = 1
      column = 2
    RECEIVING
      container = ref_container.
  "创建右边容器alv对象
  "创建alv对象
  CREATE OBJECT alv_grid1
    EXPORTING
      i_parent = ref_container.

  "调用显示方法
  CALL METHOD alv_grid1->set_table_for_first_display
    EXPORTING
      i_structure_name = 'sflight'
      is_layout        = layout
      i_save           = 'X'"可以保存变式
    CHANGING
      it_outtab        = t_sflight[]
      it_fieldcatalog  = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采用默认方式显示

ENDMODULE.                 " OUT_SCREEN  OUTPUT

 

 

posted @ 2019-11-16 09:12  渔歌晚唱  阅读(7573)  评论(0编辑  收藏  举报