OO ALV中经常会用的行单击,行双击,application tool bar上添加按钮以及按钮的相应事件,如下:
1,单击:handle_hotspot_click 事件
2,双击:handle_double_click
3,工具栏上添加自定按钮: handle_toolbar,其中,butn_type = 3. 是分隔符详细参照下面代码
4, 自定义按钮相应事件:handle_command
这些事件需要用SET HANDLER先注册,然后再在类中实现,添加自己的逻辑。
下面是个完整代码以及相关屏幕的创建过程:
1) 创建0100屏幕
屏幕PAI,PBO中流逻辑代码
1 2 3 4 5 6 |
PROCESS BEFORE OUTPUT. MODULE STATUS_0100. MODULE INIT_CON. PROCESS AFTER INPUT. MODULE USER_EXIT_0100 AT EXIT-COMMAND. |
2) 在0100屏幕上画自定义控件,命名为CUS01
在屏幕点击下面红框内的按钮创建控件CUS01.
3,程序中的Status
4,程序代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
REPORT ZTEST_OO_ALV_EVENT. DATA: gs_data TYPE spfli. DATA: gt_data LIKE TABLE OF gs_data. DATA: g_con01 TYPE REF TO cl_gui_custom_container. DATA: g_grid01 TYPE REF TO cl_gui_alv_grid. DATA: gs_fieldcat TYPE lvc_s_fcat. DATA: gt_fieldcat TYPE lvc_t_fcat. CONSTANTS: g_cus01 TYPE scrfname VALUE 'CUS01'. *----------------------------------------------------------------------* * CLASS cl_event_receiver DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_event_receiver 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_command FOR EVENT user_command OF cl_gui_alv_grid IMPORTING e_ucomm. ENDCLASS. "cl_event_receiver DEFINITION *----------------------------------------------------------------------* * CLASS cl_event_receiver IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_event_receiver 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. "handle_HOTSPOT_CLICK " 双击事件方法的实现 METHOD handle_double_click. CONDENSE e_row NO-GAPS. CONDENSE e_column NO-GAPS. MESSAGE i001(00) WITH '双击事件->行号:' e_row '、列名:' e_column. ENDMETHOD. "handle_double_click " 实现Toolbar事件方法 METHOD handle_toolbar. DATA: ls_toolbar TYPE stb_button. CLEAR: ls_toolbar. ls_toolbar-butn_type = 3. " 分隔符 APPEND ls_toolbar TO e_object->mt_toolbar. CLEAR: ls_toolbar. ls_toolbar-function = 'DISP'. " 功能码 ls_toolbar-icon = icon_display. " 图标名称 ls_toolbar-quickinfo = '显示'. " 图标的提示信息 ls_toolbar-butn_type = 0. " 0表示正常按钮 ls_toolbar-disabled = ''. " X表示灰色,不可用 ls_toolbar-text = '按钮1'. " 按钮上显示的文本 APPEND ls_toolbar TO e_object->mt_toolbar. ENDMETHOD. "handle_toolbar " 实现USER-COMMAND 事件方法 METHOD handle_command. CASE e_ucomm. WHEN 'DISP'. MESSAGE i001(00) WITH 'Toolbar事件 + USER-COMMAND事件 '. ENDCASE. ENDMETHOD. "HANDLE_COMMAND ENDCLASS. "cl_event_receiver IMPLEMENTATION DATA: event_receiver TYPE REF TO cl_event_receiver. START-OF-SELECTION. PERFORM get_data. CALL SCREEN 100. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* * text 获取数据 *----------------------------------------------------------------------* FORM get_data. SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data UP TO 30 ROWS FROM spfli. ENDFORM. "GET_DATA *&---------------------------------------------------------------------* *& Form SET_FIELDCAT *&---------------------------------------------------------------------* * text ALV显示字段文本 *----------------------------------------------------------------------* FORM set_fieldcat. CLEAR: gt_fieldcat[]. DEFINE macro_fieldcat. clear gs_fieldcat. gs_fieldcat-fieldname = &1. gs_fieldcat-coltext = &2. gs_fieldcat-hotspot = &3. append gs_fieldcat to gt_fieldcat. END-OF-DEFINITION. macro_fieldcat 'CARRID' '航线代码' 'X'. macro_fieldcat 'CONNID' '航班连接Id' ''. macro_fieldcat 'COUNTRYFR' '起飞国家' ''. macro_fieldcat 'CITYFROM' '起飞城市' ''. macro_fieldcat 'AIRPFROM' '起飞机场' ''. macro_fieldcat 'COUNTRYTO' '目标国家' ''. macro_fieldcat 'CITYTO' '目标城市' ''. macro_fieldcat 'AIRPTO' '目标机场' ''. macro_fieldcat 'FLTIME' '航班时间' ''. macro_fieldcat 'DEPTIME' '启程时间' ''. macro_fieldcat 'ARRTIME' '到达时间' ''. macro_fieldcat 'DISTANCE' '距离' ''. ENDFORM. "SET_FIELDCAT *&---------------------------------------------------------------------* *& Form SET_LAYOUT *&---------------------------------------------------------------------* * text 布局 *----------------------------------------------------------------------* * -->P_LAYOUT text *----------------------------------------------------------------------* FORM set_layout CHANGING p_layout TYPE lvc_s_layo. p_layout-cwidth_opt = 'X'. " 自动调节字段长度 p_layout-sel_mode = 'D'. " 选择模式,可选择多行 p_layout-zebra = 'X'. " 颜色交替显示 ENDFORM. "SET_LAYOUT *&---------------------------------------------------------------------* *& Module INIT_CON OUTPUT *&---------------------------------------------------------------------* * text 创建ALV *----------------------------------------------------------------------* MODULE init_con OUTPUT. DATA: ls_layout TYPE lvc_s_layo. IF g_con01 IS INITIAL. PERFORM set_fieldcat. PERFORM set_layout CHANGING ls_layout. " 创建SAP容器实例 CREATE OBJECT g_con01 EXPORTING container_name = g_cus01 " 自定义控件名称 EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 OTHERS = 6. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. " 创建GRID实例 CREATE OBJECT g_grid01 EXPORTING i_parent = g_con01 EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_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. CREATE OBJECT event_receiver. " 注册事件handler方法 SET HANDLER event_receiver->handle_hotspot_click FOR g_grid01. SET HANDLER event_receiver->handle_double_click FOR g_grid01. SET HANDLER event_receiver->handle_toolbar FOR g_grid01. SET HANDLER event_receiver->handle_command FOR g_grid01. " 调用ALV 显示方法 CALL METHOD g_grid01->set_table_for_first_display EXPORTING * i_structure_name = 'SPFLI' " 参照表结构字段显示 is_layout = ls_layout " 布局 CHANGING it_fieldcatalog = gt_fieldcat " 显示字段 it_outtab = gt_data[] EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 OTHERS = 4. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. ENDIF. ENDMODULE. " INIT_CON OUTPUT *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* * text 工具条 *----------------------------------------------------------------------* MODULE status_0100 OUTPUT. SET PF-STATUS 'S0001'. ENDMODULE. " STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& Module USER_EXIT_0100 INPUT *&---------------------------------------------------------------------* * text 取消事件 *----------------------------------------------------------------------* MODULE user_exit_0100 INPUT. LEAVE TO SCREEN 0. ENDMODULE. " USER_EXIT_0100 INPUT |
5,运行效果
以上。
发表评论