【转】自定义ALV控件的工具条按钮详解编程语言

  1 CLASS lcl_event_receiver DEFINITION DEFERRED. 
  2  
  3 DATA: itab TYPE TABLE OF spfli, 
  4      wa TYPE spfli. 
  5 DATA: name TYPE scrfname VALUE 'ALV', 
  6      container TYPE REF TO cl_gui_custom_container, 
  7      alv_grid TYPE REF TO cl_gui_alv_grid. 
  8 DATA: event_receiver TYPE REF TO lcl_event_receiver. 
  9 DATA: save_ok TYPE sy-ucomm, 
 10      ok_code TYPE sy-ucomm. 
 11  
 12 SELECT * FROM spfli INTO TABLE itab. 
 13  
 14 CALL SCREEN 100. 
 15  
 16 *&---------------------------------------------------------------------* 
 17 *&      Class LCL_EVENT_RECEIVER 
 18 *&---------------------------------------------------------------------* 
 19 *       Toolbar and user command event handle 
 20 *----------------------------------------------------------------------* 
 21 CLASS lcl_event_receiver DEFINITION. 
 22   PUBLIC SECTION. 
 23    METHODS: 
 24       handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid 
 25       IMPORTING e_object e_interactive, 
 26       handle_ucomm FOR EVENT user_command OF cl_gui_alv_grid 
 27       IMPORTING e_ucomm. 
 28 ENDCLASS.              "LCL_EVENT_RECEIVER 
 29  
 30 *&---------------------------------------------------------------------* 
 31 *&      Class (Implementation)  lcl_event_receiver 
 32 *&---------------------------------------------------------------------* 
 33 *       Toolbar and user command event handle 
 34 *----------------------------------------------------------------------* 
 35 CLASS lcl_event_receiver IMPLEMENTATION. 
 36   METHOD handle_toolbar. 
 37     DATA: utoolbar TYPE stb_button. 
 38    utoolbar-function = 'SHOW'. 
 39    utoolbar-icon = icon_ppe_vnode. 
 40    utoolbar-text = 'Show data info'. 
 41    utoolbar-quickinfo = 'Detail data info'. 
 42     APPEND utoolbar TO e_object->mt_toolbar. 
 43  ENDMETHOD.                   "handle_toolbar 
 44   METHOD handle_ucomm. 
 45     DATA rowid_t TYPE lvc_t_row. 
 46     DATA temp TYPE lvc_s_row. 
 47     CASE e_ucomm. 
 48      WHEN 'SHOW'. 
 49        CALL METHOD alv_grid->get_selected_rows 
 50          IMPORTING 
 51            et_index_rows = rowid_t . 
 52        IF sy-subrc = 0. 
 53          PERFORM get_selected_row_info 
 54                      TABLES 
 55                         rowid_t. 
 56        ENDIF. 
 57    ENDCASE. 
 58  ENDMETHOD.                   "handle_ucomm 
 59 ENDCLASS.              "lcl_event_receiver 
 60  
 61 *&---------------------------------------------------------------------* 
 62 *&     Form  get_selected_row_info 
 63 *&---------------------------------------------------------------------* 
 64 *      Get_selected_row_info 
 65 *----------------------------------------------------------------------* 
 66 *     -->ROWINFO   text 
 67 *----------------------------------------------------------------------* 
 68 FORM get_selected_row_info TABLES rowinfo TYPE lvc_t_row. 
 69   DATA rowid_s TYPE lvc_s_row. 
 70   DATA: str TYPE string, 
 71        step VALUE '-' . 
 72   LOOP AT rowinfo INTO rowid_s. 
 73     READ TABLE itab INTO wa INDEX rowid_s-index. 
 74     CONCATENATE wa-carrid wa-connid wa-cityfrom wa-cityto INTO str SEPARATED BY step. 
 75     MESSAGE i001(zmess) WITH str. 
 76   ENDLOOP. 
 77 ENDFORM.                   "get_selected_row_info 
 78  
 79 MODULE status_0100 OUTPUT. 
 80   IF container IS INITIAL. 
 81     CREATE OBJECT container 
 82      EXPORTING 
 83        container_name = name. 
 84     CREATE OBJECT alv_grid 
 85      EXPORTING 
 86        i_parent = container. 
 87     CALL METHOD alv_grid->set_table_for_first_display 
 88      EXPORTING 
 89        i_structure_name = 'SPFLI' 
 90      CHANGING 
 91        it_outtab       = itab. 
 92 *   Create and set event 
 93     CREATE OBJECT event_receiver. 
 94     SET HANDLER event_receiver->handle_toolbar FOR alv_grid. 
 95     SET HANDLER event_receiver->handle_ucomm FOR alv_grid. 
 96 *   Set interaction 
 97     CALL METHOD alv_grid->set_toolbar_interactive. 
 98   ENDIF. 
 99 ENDMODULE.                " STATUS_0100  OUTPUT 
100  
101 MODULE user_command_0100 INPUT. 
102   save_ok = ok_code. 
103   CLEAR ok_code. 
104   CASE save_ok. 
105     WHEN 'EXIT'. 
106      LEAVE PROGRAM. 
107   ENDCASE. 
108 ENDMODULE.                " USER_COMMAND_0100  INPUT

增加自定义按钮的结构如下:

FUNCTION                     功能代码
BUTN_TYPE                 按钮类型
                                              可用的按钮类型:
                                                 0 Button(normal)
                                                 1 Menu and default button
                                                 2 Menu
                                                 3 分割符
                                                 4 Radio button
                                                 5 Checkbox
                                                 6 Menu entry
ICON                          按钮图标(可选)
TEXT                         按钮文本(可选)
QUICKINFO               按钮的悬停文本(可选)
DISABLED                 灰化

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/19718.html

(0)
上一篇 2021年7月19日 22:25
下一篇 2021年7月19日 22:25

相关推荐

发表回复

登录后才能评论