OOALV

作者: 聂落尘 | 来源:发表于2023-07-02 07:42 被阅读0次
    *  TCODE  SE24 查看类
    *-------------定义变量
    DATA:
      wcl_container TYPE REF TO cl_gui_custom_container, " 存放ALV的容器
      wcl_alv       TYPE REF TO cl_gui_alv_grid, " ALV的网格
      gt_fieldcat   TYPE lvc_t_fcat, " 存放字段目录的内表
      wa_fieldcat   TYPE lvc_s_fcat,
      gs_layout     TYPE lvc_s_layo. " 布局结构
    
    *------------------定义内表
    TYPES: BEGIN OF ty_alvshow,
             carrid    TYPE sflight-carrid, " 航线代码
             connid    TYPE sflight-connid, "航班连接编号
             fldate    TYPE sflight-fldate, "航班日期
             price     TYPE sflight-price, " 航空运费
             planetype TYPE sflight-planetype, " 飞机类型
             seatsmax  TYPE sflight-seatsmax, " 经济舱的最大容量
             seatsocc  TYPE sflight-seatsocc, " 占据的经济舱座位
           END OF ty_alvshow.
    
    
    DATA:lt_sflight TYPE TABLE OF ty_alvshow,
         wa_sflight TYPE ty_alvshow.
    
    DATA: ok_code LIKE sy-ucomm, " ok_code 用于接受屏幕点击事件, 这个名称要和屏幕属性中保持一致
          save_ok LIKE sy-ucomm.
    
    
    " 定义实现ALV event方法的类
    CLASS cl_event_handler DEFINITION.
      PUBLIC SECTION.
        " 声明单击事件方法
        METHODS handle_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid IMPORTING e_row_id e_column_id.
        " 声明双击方法
        METHODS handle_double_click FOR EVENT double_click OF cl_gui_alv_grid IMPORTING e_row e_column.
        " 声明 toolbar事件方法
        METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid IMPORTING e_object e_interactive.
        " 声明user command事件方法
        METHODS handle_usercommand FOR EVENT user_command OF cl_gui_alv_grid IMPORTING e_ucomm.
    ENDCLASS.
    
    CLASS cl_event_handler IMPLEMENTATION.
      " 单击事件
      METHOD handle_hotspot_click.
        CONDENSE e_row_ID NO-GAPS.
        CONDENSE e_column_id NO-GAPS.
        MESSAGE i001(00) WITH '点击事件-行号' e_row_id '列名:' e_column_id.
      ENDMETHOD.
    
      " 双击事件
      METHOD handle_double_click.
        CONDENSE e_row NO-GAPS.
        CONDENSE e_column NO-GAPS.
        MESSAGE i001(00) WITH '双击事件-行号' e_row '列名:' e_column.
      ENDMETHOD.
    
    
      " toolbar事件
      METHOD handle_toolbar.
        " toolbar加载事件
    
      ENDMETHOD.
    
      " usercommand事件
      METHOD handle_usercommand.
        CASE e_ucomm.
          WHEN 'ZADD'.
            MESSAGE '点击了保存' TYPE 'I'.
          WHEN OTHERS.
        ENDCASE.
      ENDMETHOD.
    ENDCLASS.
    
    DATA event_handler TYPE REF TO cl_event_handler. " 用于事件 cl_gui_alv_grid的event事件
    
    START-OF-SELECTION.
      CALL SCREEN 8000.
    
    *&---------------------------------------------------------------------*
    *& Module ALV_DISPLAY OUTPUT
    *&---------------------------------------------------------------------*
    *&
    *&---------------------------------------------------------------------*
    MODULE alv_display OUTPUT.
      " se41复制状态栏  可以选择系统的 程序SAPLSLVC_FULLSCREEN 状态栏STANDARD_FULLSCREEN
      " 屏幕属性 next screen 要设置为0, 这样点击返回按钮的时候才能返回
      SET PF-STATUS 'STANDARD'.
    * SET TITLEBAR 'xxx'.
      PERFORM alv_display.
    ENDMODULE.
    
    INCLUDE zlc_ooalv_01_status_8000o01.
    
    *&---------------------------------------------------------------------*
    *& Form alv_display
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM alv_display .
      IF wcl_alv IS INITIAL.
        CREATE OBJECT wcl_container
          EXPORTING
            container_name = 'CONTAINER'. " 屏幕上定制控制的名字,要大写
        CREATE OBJECT wcl_alv
          EXPORTING
            i_parent = wcl_container.
        " 获取字段目录
        PERFORM prepare_field_catlog CHANGING gt_fieldcat.
        " 获取样式
        PERFORM prepare_layout CHANGING gs_layout.
        " 获取需要显示的数据
        PERFORM get_data TABLES lt_sflight.
    
        " 注册事件
        CREATE OBJECT event_handler.
        SET HANDLER event_handler->handle_hotspot_click FOR wcl_alv.
        SET HANDLER event_handler->handle_double_click FOR wcl_alv.
        SET HANDLER event_handler->handle_toolbar FOR wcl_alv.
        "SET HANDLER event_handler->handle_usercommand FOR wcl_alv.
    
        " 显示ALV
        CALL METHOD wcl_alv->set_table_for_first_display
          EXPORTING
            is_layout                     = gs_layout
          CHANGING
            it_outtab                     = lt_sflight
            it_fieldcatalog               = gt_fieldcat
    *       it_sort                       =
    *       it_filter                     =
          EXCEPTIONS
            invalid_parameter_combination = 1
            program_error                 = 2
            too_many_lines                = 3
            OTHERS                        = 4.
        IF sy-subrc <> 0.
    *     Implement suitable error handling here
        ENDIF.
      ELSE.
        " ALV 不为空 刷新
        CALL METHOD wcl_alv->refresh_table_display
    *                              EXPORTING
    *                                is_stable      =
    *                                i_soft_refresh =
          EXCEPTIONS
            finished = 1
            OTHERS   = 2.
      ENDIF.
    
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form prepare_field_catlog
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      <-- GT_FIELDCAT
    *&---------------------------------------------------------------------*
    FORM prepare_field_catlog  CHANGING p_gt_fieldcat.
      PERFORM form_create_fieldcat USING 'CARRID' '航班代码' 'X'.
      PERFORM form_create_fieldcat USING 'CONNID' '航班连接ID' ''.
      PERFORM form_create_fieldcat USING 'FLDATE' '航班日期' ''.
      PERFORM form_create_fieldcat USING 'PRICE' '航空费用' ''.
      PERFORM form_create_fieldcat USING 'PLANETYPE' '飞机类型' ''.
      PERFORM form_create_fieldcat USING 'SEATSMAX' '经济舱的最大容量' ''.
      PERFORM form_create_fieldcat USING 'SEATSOCC' '占据的经济舱座位' ''.
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form prepare_layout
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      <-- GS_LAYOUT
    *&---------------------------------------------------------------------*
    FORM prepare_layout  CHANGING p_gs_layout TYPE lvc_s_layo.
      p_gs_layout-zebra = 'X'." 颜色交替
      "p_gs_layout-grid_title = '显示个标题'.
      p_gs_layout-cwidth_opt = 'X'. " 自动调节宽度
      p_gs_layout-smalltitle = 'X'.
    
      " oo alv默认带有工具栏
      p_gs_layout-no_toolbar = 'X'. " 隐藏toolbar工具栏
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form get_data
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *&      --> LT_SFLIGHT
    *&---------------------------------------------------------------------*
    FORM get_data  TABLES   p_lt_sflight LIKE lt_sflight.
      SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE p_lt_sflight UP TO 30 ROWS.
    ENDFORM.
    *&---------------------------------------------------------------------*
    *&      Module  USER_COMMAND_8000  INPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE user_command_8000 INPUT.
      save_ok = ok_code.
      CLEAR ok_code.
      CASE save_ok.
        WHEN 'ZADD'.
          MESSAGE '点击保存' TYPE 'I'.
          " LEAVE PROGRAM.
        WHEN OTHERS.
      ENDCASE.
    ENDMODULE.
    
    
    FORM form_create_fieldcat USING fieldname coltext hotspot.
      CLEAR wa_fieldcat.
      wa_fieldcat-fieldname = fieldname.
      wa_fieldcat-coltext = coltext.
      wa_fieldcat-hotspot = hotspot.
      wa_fieldcat-edit = 'X'.
      wa_fieldcat-ref_table = 'SFLIGHT'.
      APPEND wa_fieldcat TO gt_fieldcat.
    ENDFORM.
    
    " 监听的user_command事件为什么不生效
    " 设置GUI状态栏, 为什么点击每一个按钮都会返回到上一个页面
    

    相关文章

      网友评论

          本文标题:OOALV

          本文链接:https://www.haomeiwen.com/subject/ltrxudtx.html