SAP: 创建ALV GRID容器

SAP 创建ALV GRID容器

 

1、主程序

*&---------------------------------------------------------------------*
*& Report  Z15_12
*& 可执行程序
*&---------------------------------------------------------------------*
*&
*& 创建 ALV GRID 容器
*& 注意每个程序都需要激活方可执行
*&---------------------------------------------------------------------*

REPORT Z15_12.


DATA: CON1_REF TYPE REF TO CL_GUI_CUSTOM_CONTAINER. " 定义容器, 参照 自定义控件"其中自定义控件命名为CON1
DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID.  "定义ALV变量,参照 CL_GUI_ALV_GRID类"
DATA: GS_VARIANT like DISVARIANT, "" 列表变量
      gs_cs_variant like disvariant.
DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT.  " 要显示在屏幕上的内表
DATA: gs_layout  type lvc_s_layo .
DATA: gt_toolbar TYPE  ui_functions .
DATA: gt_sort TYPE lvc_t_sort.  " 排序

PARAMETERS: p_var like disvariant-variant.  " 参数变量


*& 设置GRID布局
PERFORM setting_layout CHANGING gs_layout .

*& 根据用户显示或隐藏布局按钮
PERFORM setting_toolbar.

*& 排序
PERFORM setting_sort.

*& 显示检索帮助
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_var.
*&
  gs_variant-report = sy-repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      IS_VARIANT    = gs_variant
*     I_TABNAME_HEADER          =
*     I_TABNAME_ITEM            =
*     IT_DEFAULT_FIELDCAT       =
      I_SAVE        = 'A'
*     I_DISPLAY_VIA_GRID        = ' '
    IMPORTING
*     E_EXIT        =
      ES_VARIANT    = gs_variant
    EXCEPTIONS
      NOT_FOUND     = 1
      PROGRAM_ERROR = 2
      OTHERS        = 3.
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
    p_var = gs_variant-variant .
  ENDIF.



FORM setting_layout CHANGING p_layout type lvc_s_layo.
  p_layout-cwidth_opt = 'X' . "自动调节长度
  p_layout-grid_title = 'get_current_cell Method TEST'.  "标题栏"
  p_layout-sel_mode = 'D'.  " 选择模式:可选择多行"
  p_layout-zebra = 'X'.   "指定条纹(STRIPE)属性"
ENDFORM. " setting_layout.


*& 根据用户显示或隐藏布局按钮
FORM setting_toolbar.
  DATA : l_exclude TYPE UI_FUNC.

  l_exclude = cl_gui_alv_grid=>mc_fc_save_variant.
  APPEND l_exclude TO gt_toolbar.

  l_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant.
  APPEND l_exclude TO gt_toolbar.
ENDFORM.  " SETTING_TOOLBAR

*& 排序 子程序
FORM setting_sort.
  DATA: ls_sort TYPE lvc_s_sort.
  ls_sort-spos = '1'.  " 排序顺序 ,整形数字
  ls_sort-fieldname = 'CARRID'.  " 排序字段名称
  ls_sort-up = 'X' . "  升序  或  LS_SORT-DOWN = 'X'  "降序
  ls_sort-subtot = 'X' .   " 显示合计

  APPEND ls_sort to gt_sort .
ENDFORM. " setting_sort.


CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
  " 声明双击事件方法
    METHODS: handle_double_click
     FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
    IMPORTING e_row e_column.
ENDCLASS.

CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_double_click. "双击事件方法的实现"
    LEAVE TO SCREEN 0 .
  ENDMETHOD.
ENDCLASS.      " LCL_event_receiver.

DATA: event_receiver type ref to lcl_event_receiver .

*CREATE OBJECT event_receiver.
* 注册事件Handler方法
*SET HANDLER event_receiver->handle_double_click FOR g_grid .
*&


*& 查询数据,然后调用屏幕100.
START-OF-SELECTION.
  SELECT * FROM SFLIGHT
    INTO TABLE GT_SFLIGHT UP TO 25 ROWS.
  CALL SCREEN 100.

  gs_variant-report = sy-repid.
  gs_variant-username = sy-uname.

*&
*& 创建 AVL控件
  INCLUDE Z15_12_PBO.
*INCLUDE Z15_11_PBO.
*  INCLUDE Z15_10_PBO.

*

MODULE user_command_0100 INPUT.
  DATA: l_row type i,
        l_value type c,
        l_col type i,
        ls_row type lvc_s_row,
        ls_col type lvc_s_col,
        ls_roid type lvc_s_roid.

  DATA: ls_scroll type lvc_s_stbl.
  ls_scroll-row = 'X'.
  ls_scroll-col = 'X'.

*& 模式-> 选择:ABAP对象模式 ,(继续)-> 调用方法, 在接口栏=G_GRID, 类/接口栏= CL_GUI_ALV_GRID, 方法栏= get_current_cell
  CALL METHOD G_GRID->GET_CURRENT_CELL
    IMPORTING
      E_ROW     = l_row
      E_VALUE   = l_value
      E_COL     = l_col
      ES_ROW_ID = ls_row
      ES_COL_ID = ls_col
      ES_ROW_NO = ls_roid.

*&   重新查询已经显示在ALV的输出表时使用的方法。
  CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY
    EXPORTING
      IS_STABLE      = ls_scroll
      I_SOFT_REFRESH = 'X'
*    EXCEPTIONS
*     FINISHED       = 1
*     others         = 2
    .
  IF SY-SUBRC <> 0.
*   Implement suitable error handling here
  ENDIF.


ENDMODULE. " user_command_0100.

  

2、 创建 PBO 子程序

命名为  Z15_12_PBO

*----------------------------------------------------------------------*
***INCLUDE Z15_12_PBO.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Module  INIT_CON  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE INIT_CON OUTPUT.
*& 1、创建容器对象
  IF CON1_REF IS INITIAL.  " 如果容器是空的,创建容器对象"
    CREATE OBJECT CON1_REF
      EXPORTING
        CONTAINER_NAME = 'CON1'.

*& 2、创建 ALV GRID 控件 对象
    CREATE OBJECT G_GRID
      EXPORTING
        I_PARENT          = CON1_REF .
*      EXCEPTIONS
*        error_cnt1_create = 1
*        error_cnt1_init   = 2
*        error_cnt1_link   = 3
*        error_dp_create   = 4
*        OTHERS            = 5.
*    if sy-subrc <> 0.
*      MESSAGE ID sy-msgid TYPE sy-msgty number sy-msgno
*      with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
*    endif.
  ENDIF. "  CON1_REF"

CREATE OBJECT event_receiver.
* 注册事件Handler方法
SET HANDLER event_receiver->handle_double_click FOR g_grid .
*&

*& 模式-> 选择:ABAP对象模式 ,(继续)-> 调用方法, 在接口栏=G_GRID, 类/接口栏= CL_GUI_ALV_GRID, 方法栏= SET_TABLE_FOR_FIRST_DISPLAY
*& 3、 ALV GRID显示数据方法
  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      I_STRUCTURE_NAME     = 'SFLIGHT'
      i_save               = 'A'
      is_variant           = gs_variant
      i_default            = ' '
      is_layout            = gs_layout
      it_toolbar_excluding = gt_toolbar
    CHANGING
      It_OUTTAB            = GT_SFLIGHT
      it_sort              = gt_sort.

ENDMODULE.                 " INIT_CON  OUTPUT

  

3、屏幕逻辑流程序

PROCESS BEFORE OUTPUT.
* MODULE STATUS_0100.
MODULE init_con.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100.

  

 

posted @ 2024-05-17 06:58  samrv  阅读(31)  评论(0编辑  收藏  举报