Fork me on GitHub

ABAP 基于Function ALV 实现单元格自定义搜索帮助

1.实现ALV 报表自定义搜索帮助

效果如下:

点击搜索帮助按钮,弹出框

 双击值并填入单元格内

 2.实现思路

 2.1定义变量以及F4搜索帮助自定义类,并实现

 

" 定义ALV变量
DATA: gr_grid TYPE REF TO cl_gui_alv_grid, "用于绑定ALV事件
      gs_stbl TYPE lvc_s_stbl. "用于列稳定刷新
DATA: gt_event TYPE slis_t_event WITH HEADER LINE.

"定义事件
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    "F4
    METHODS:
      handle_f4 FOR EVENT onf4 OF cl_gui_alv_grid
        IMPORTING e_fieldname   "列名
                    es_row_no     "行号
                    er_event_data
                    et_bad_cells.
ENDCLASS.

"方法实施
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD  handle_f4.

*     窗口时间参数的自定义f4检索帮助
    CASE e_fieldname.
      WHEN 'EMPLOYEE'.   "内表字段名称
        PERFORM f4_help_employee USING e_fieldname  "列名
                                       es_row_no  "行号
                                       er_event_data.
    ENDCASE.
*     设置后,alv稳定刷新
    CLEAR:gs_stbl.
    gs_stbl-row = 'X'." 基于行的稳定刷新
    gs_stbl-col = 'X'." 基于列稳定刷新
    CALL METHOD gr_grid->refresh_table_display
      EXPORTING
        is_stable = gs_stbl.
  ENDMETHOD.                    "HANDLE_F4

ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION

 

 2.2在调用ALV函数前添加如下代码

  gt_event-name = 'CALLER_EXIT'."需要添加,不加会有问题
  gt_event-form = 'FM_BUTTON'.
  APPEND gt_event.

 

 完整实现代码如下:

  1 *&---------------------------------------------------------------------*
  2 *& Report ZTEST_010
  3 *&---------------------------------------------------------------------*
  4 *&Function ALV 单元格搜索帮助
  5 *&单元格是否可编辑仅适用于 Function (REUSE_ALV_GRID_DISPLAY_LVC)
  6 *&---------------------------------------------------------------------*
  7 REPORT ztest_010.
  8 
  9 TABLES: spfli.
 10 TYPE-POOLS: slis,kcde .
 11 
 12 "ALV 展示结构
 13 TYPES : BEGIN OF typ_alv ,
 14           carrid      TYPE spfli-carrid   , "航线代码
 15           connid      TYPE spfli-connid   , "航班连接编号
 16           cityfrom    TYPE spfli-cityfrom , "起飞城市
 17           airpfrom    TYPE spfli-airpfrom , "始发机场
 18           cityto      TYPE spfli-cityto   , "到达城市
 19           airpto      TYPE spfli-airpto   , "目标机场
 20           fltime      TYPE spfli-fltime   , "航班时间
 21           deptime     TYPE spfli-deptime  , "出发时间
 22           arrtime     TYPE spfli-arrtime  , "到达时间
 23           distance    TYPE spfli-distance , "距离
 24           distid      TYPE spfli-distid   , "大距离单位(公里,英里)
 25           fltype      TYPE spfli-fltype   , "航班类型
 26           employee    TYPE char40, "
 27           field_style TYPE lvc_t_styl,  "控制字段可编辑的参数
 28           sel         TYPE c,
 29         END OF typ_alv .
 30 
 31 " 定义ALV变量
 32 DATA: gs_layout   TYPE lvc_s_layo, "ALV 控制: 布局结构
 33       gt_fieldcat TYPE lvc_t_fcat, "字段目录
 34       gs_fieldcat TYPE lvc_s_fcat,
 35       gv_repid    TYPE repid. "当前程序名
 36 DATA: gr_grid TYPE REF TO cl_gui_alv_grid, "用于绑定ALV事件
 37       gs_stbl TYPE lvc_s_stbl. "用于列稳定刷新
 38 DATA: gt_event TYPE slis_t_event WITH HEADER LINE.
 39 
 40 
 41 " 内表和工作区
 42 DATA : gs_alv TYPE typ_alv,
 43        gt_alv TYPE TABLE OF typ_alv.
 44 
 45 "定义结构中表结构
 46 DATA:gs_edit TYPE lvc_s_styl,
 47      gt_edit TYPE lvc_t_styl. "单元格的类型表
 48 
 49 
 50 
 51 "定义事件
 52 CLASS lcl_event_receiver DEFINITION.
 53   PUBLIC SECTION.
 54     "F4
 55     METHODS:
 56       handle_f4 FOR EVENT onf4 OF cl_gui_alv_grid
 57         IMPORTING e_fieldname   "列名
 58                     es_row_no     "行号
 59                     er_event_data
 60                     et_bad_cells.
 61 ENDCLASS.
 62 
 63 "方法实施
 64 CLASS lcl_event_receiver IMPLEMENTATION.
 65   METHOD  handle_f4.
 66 
 67 *     窗口时间参数的自定义f4检索帮助
 68     CASE e_fieldname.
 69       WHEN 'EMPLOYEE'.   "内表字段名称
 70         PERFORM f4_help_employee USING e_fieldname  "列名
 71                                        es_row_no  "行号
 72                                        er_event_data.
 73     ENDCASE.
 74 *     设置后,alv稳定刷新
 75     CLEAR:gs_stbl.
 76     gs_stbl-row = 'X'." 基于行的稳定刷新
 77     gs_stbl-col = 'X'." 基于列稳定刷新
 78     CALL METHOD gr_grid->refresh_table_display
 79       EXPORTING
 80         is_stable = gs_stbl.
 81   ENDMETHOD.                    "HANDLE_F4
 82 
 83 ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION
 84 
 85 
 86 *--------选择条件
 87 SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE TEXT-001.
 88 
 89 SELECT-OPTIONS:s_carrid FOR spfli-carrid , "航线
 90                s_connid FOR spfli-connid."航班
 91 SELECTION-SCREEN END OF BLOCK bl01.
 92 
 93 
 94 *&---------------------------------------------------------------------*
 95 *&   EVENT AT INITIALIZATION
 96 *&---------------------------------------------------------------------*
 97 INITIALIZATION .
 98 
 99 AT SELECTION-SCREEN OUTPUT .
100 
101 *----------------------------------------------------------------------*
102 * EVENT OCCURS AFTER THE SELECTION SCREEN HAS BEEN PROCESSED
103 *----------------------------------------------------------------------*
104 START-OF-SELECTION.
105 
106   PERFORM frm_getdata .        "获取数据
107   PERFORM frm_set_layout .     "设置显示布局
108   PERFORM frm_set_all_fieldcat."设置字段目录
109   PERFORM frm_display_data.    "展示ALV
110 
111 END-OF-SELECTION.
112 
113 
114 *&---------------------------------------------------------------------*
115 *& Form frm_getdata
116 *&---------------------------------------------------------------------*
117 *& text
118 *&---------------------------------------------------------------------*
119 *& -->  p1        text
120 *& <--  p2        text
121 *&---------------------------------------------------------------------*
122 FORM frm_getdata .
123   SELECT *
124     FROM spfli
125    WHERE spfli~carrid IN @s_carrid "航线
126     AND  spfli~connid IN @s_connid"航班
127     INTO CORRESPONDING FIELDS OF TABLE @gt_alv.
128   IF sy-subrc = 0.
129     SORT gt_alv BY carrid connid.
130   ENDIF.
131 
132   LOOP AT gt_alv INTO gs_alv.
133     IF gs_alv-carrid = 'AZ'.
134       gs_alv-employee   = '单值,不需要搜索'.
135       gs_edit-fieldname = 'EMPLOYEE'."不可编辑字段名
136       gs_edit-style = cl_gui_alv_grid=>mc_style_disabled. "设置为不可编辑
137       APPEND gs_edit TO gs_alv-field_style .
138     ENDIF.
139     MODIFY gt_alv FROM gs_alv.
140   ENDLOOP.
141 ENDFORM.
142 *&---------------------------------------------------------------------*
143 *& Form frm_display_data
144 *&---------------------------------------------------------------------*
145 *& ALV 展示
146 *&---------------------------------------------------------------------*
147 *& -->  p1        text
148 *& <--  p2        text
149 *&---------------------------------------------------------------------*
150 FORM frm_display_data .
151 
152   gt_event-name = 'CALLER_EXIT'."需要添加,不加会有问题
153   gt_event-form = 'FM_BUTTON'.
154   APPEND gt_event.
155 
156   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
157     EXPORTING
158       i_callback_program       = sy-repid
159       i_callback_pf_status_set = 'FRM_PF_STATUS'   "状态
160       i_callback_user_command  = 'FRM_ALV_USER_COMMAN'
161       is_layout_lvc            = gs_layout     "布局
162       it_events                = gt_event[]    "事件
163       it_fieldcat_lvc          = gt_fieldcat   "字段显示
164       i_save                   = 'A'
165     TABLES
166       t_outtab                 = gt_alv     "显示的内表
167     EXCEPTIONS
168       program_error            = 1
169       OTHERS                   = 2.
170 
171   IF sy-subrc <> 0.
172     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
173     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
174   ENDIF.
175 ENDFORM.
176 
177 *&---------------------------------------------------------*
178 *&      Form  FRM_PF_STATUS
179 *&---------------------------------------------------------*
180 *       ALV STATUS
181 *----------------------------------------------------------*
182 FORM frm_pf_status USING rt_extab TYPE slis_t_extab.
183   SET PF-STATUS 'STANDARD' EXCLUDING rt_extab. "排除按钮
184 ENDFORM.   "FRM_PF_STATUS
185 
186 
187 *&---------------------------------------------------------*
188 *&      Form  FRM_ALV_USER_COMMAN
189 *&---------------------------------------------------------*
190 *       ALV USER-COMMAND
191 *----------------------------------------------------------*
192 FORM frm_alv_user_comman USING r_ucomm  TYPE  sy-ucomm
193                      rs_selfield TYPE  slis_selfield ."刷新
194 
195   DATA: lr_grid  TYPE REF TO cl_gui_alv_grid.
196   CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
197     IMPORTING
198       e_grid = lr_grid.
199   CALL METHOD lr_grid->check_changed_data .
200   CASE r_ucomm.   "功能按钮
201     WHEN 'SAVE' .
202   ENDCASE.
203 ENDFORM.   "USER_COMMAND
204 
205 *&---------------------------------------------------------------------*
206 *& Form frm_set_fieldcat
207 *&---------------------------------------------------------------------*
208 *& 字段属性
209 *&---------------------------------------------------------------------*
210 *& -->  p1        text
211 *& <--  p2        text
212 *&---------------------------------------------------------------------*
213 FORM frm_set_fieldcat USING p_val1
214                             p_val2
215                             p_val3
216                             p_val4
217                             p_val5.
218   gs_fieldcat-fieldname = p_val1 .  "字段名称
219   gs_fieldcat-ref_table = p_val2.   "参考表
220   gs_fieldcat-ref_field = p_val3.   "参考字段
221   gs_fieldcat-coltext   = p_val4.   "描述文本
222   gs_fieldcat-edit      = p_val5.   "整列可编辑
223   IF gs_fieldcat-fieldname = 'EMPLOYEE'.
224     gs_fieldcat-f4availabl = 'X'. "字段尾部,添加搜索帮助的的小按钮
225   ENDIF.
226   APPEND gs_fieldcat TO gt_fieldcat.
227   CLEAR gs_fieldcat.
228 ENDFORM.
229 
230 "用于添加单元格搜索帮助按钮
231 FORM fm_button USING e_grid TYPE slis_data_caller_exit.
232   DATA: lr_event_receiver TYPE REF TO lcl_event_receiver,
233         lt_f4             TYPE lvc_t_f4,
234         ls_f4             TYPE lvc_s_f4.
235 
236   IF gr_grid IS INITIAL.
237     CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
238       IMPORTING
239         e_grid = gr_grid.
240   ENDIF.
241 
242 * 设置enter事件
243   CALL METHOD gr_grid->register_edit_event
244     EXPORTING
245       i_event_id = cl_gui_alv_grid=>mc_evt_enter
246     EXCEPTIONS
247       error      = 1
248       OTHERS     = 2.
249 
250   "注册事件,设置,触发F4的搜索帮助
251   DATA(lr_event_receiver2) = NEW lcl_event_receiver( ) ."新语法
252 *  CREATE OBJECT lr_event_receiver.
253   SET HANDLER lr_event_receiver2->handle_f4 FOR gr_grid.  "绑定CLASS事件到ALV
254 
255   ls_f4-fieldname  = 'EMPLOYEE'.   "窗口时间参数(需要定义F4帮助按钮的字段)
256   ls_f4-register   = 'X'.
257   ls_f4-getbefore  = 'X'.
258   ls_f4-chngeafter = 'X'.
259   INSERT ls_f4 INTO TABLE lt_f4.
260 
261   CALL METHOD gr_grid->register_f4_for_fields
262     EXPORTING
263       it_f4 = lt_f4[].
264 ENDFORM.
265 
266 "填充搜索帮助值,并展示回填
267 FORM f4_help_employee USING p_fieldname   TYPE lvc_fname
268                             p_row_no      TYPE lvc_s_roid
269                             er_event_data TYPE REF TO cl_alv_event_data.
270   DATA: lt_return TYPE STANDARD TABLE OF ddshretval,
271         ls_return TYPE ddshretval.
272   DATA :lv_eqart TYPE eqart."技术对象类型
273 
274   CLEAR gs_alv.
275   READ TABLE gt_alv INTO gs_alv INDEX p_row_no-row_id."获取当前行数据
276   IF sy-subrc = 0.
277     IF gs_alv-employee IS INITIAL.
278       IF p_fieldname = 'EMPLOYEE'.  "展示内表字段
279         TYPES :BEGIN OF ty_aa, "搜索帮助结构
280                  employee TYPE char40,
281                  zaaa     TYPE char40,
282                END OF ty_aa.
283 
284         DATA:lt_vs TYPE TABLE OF ty_aa, "搜索帮助表
285              ls_vs TYPE  ty_aa.
286         CLEAR:lt_vs,lt_return.
287 
288         "每个单元格对应搜索帮助值可根据条件查找,不固定
289         ls_vs-zaaa     = '10086'.
290         ls_vs-employee = '搜索帮助值1'.
291         APPEND ls_vs TO lt_vs .
292 
293         ls_vs-zaaa     = '10010'.
294         ls_vs-employee = '搜索帮助值2'.
295         APPEND ls_vs TO lt_vs .
296         SORT lt_vs BY employee .
297 
298         CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
299           EXPORTING
300 *           DDIC_STRUCTURE   = ' '
301             retfield         = 'EMPLOYEE'            "这个参数为帮助表中返回到选择屏幕的字段的参数
302 *           PVALKEY          = ' '
303             dynpprog         = sy-repid           "当前程序,不写会有问题
304             dynpnr           = sy-dynnr           "当前屏幕,不写会有问题
305           " dynprofield      = 'P_PERSON'       "选择屏幕上需要加F4帮助的字段
306             value_org        = 'S'                "默认为C但是此处不用S不行
307             callback_program = sy-repid
308         "   callback_form    = 'CB_FORM'
309           TABLES
310             value_tab        = lt_vs            "F4帮助值的表
311             return_tab       = lt_return
312           EXCEPTIONS
313             parameter_error  = 1
314             no_values_found  = 2
315             OTHERS           = 3.
316         IF sy-subrc = 0.
317 ****将选的值填入ALV字段中
318           READ TABLE gt_alv INTO gs_alv INDEX p_row_no-row_id.
319           IF sy-subrc = 0.
320             READ TABLE lt_return INTO ls_return  INDEX 1.
321             IF ls_return-fieldval IS NOT INITIAL.
322               gs_alv-employee = ls_return-fieldval.
323               CLEAR:ls_return.
324             ENDIF.
325             MODIFY gt_alv FROM gs_alv INDEX p_row_no-row_id TRANSPORTING employee.
326           ENDIF.
327           "X表示事件已经处理,不会去调取系统标准搜索帮助,(不填写参考字段参考表,也可以忽略这个标识)
328           er_event_data->m_event_handled = 'X'.
329         ENDIF.
330       ENDIF.
331     ENDIF.
332   ENDIF.
333 
334 ENDFORM.
335 *&---------------------------------------------------------------------*
336 *& Form FRM_SET_LAYOUT
337 *&---------------------------------------------------------------------*
338 *& ALV布局
339 *&---------------------------------------------------------------------*
340 FORM frm_set_layout .
341   gs_layout-cwidth_opt         = 'X'."优化列宽度
342   gs_layout-zebra              = 'X'."条纹展示
343   gs_layout-box_fname          = 'SEL'."选择列
344   gs_layout-stylefname         = 'FIELD_STYLE'.
345 ENDFORM.
346 *&---------------------------------------------------------------------*
347 *& Form FRM_SET_ALL_FIELDCAT
348 *&---------------------------------------------------------------------*
349 *& 设置字段目录
350 *&---------------------------------------------------------------------*
351 *& -->  p1        text
352 *& <--  p2        text
353 *&---------------------------------------------------------------------*
354 FORM frm_set_all_fieldcat .
355   PERFORM frm_set_fieldcat USING 'CARRID   ' 'SPFLI' 'CARRID  ' '航线代码' ''.
356   PERFORM frm_set_fieldcat USING 'CONNID   ' 'SPFLI' 'CONNID  ' '航班连接编号' ''.
357   PERFORM frm_set_fieldcat USING 'CITYFROM ' 'SPFLI' 'CITYFROM' '起飞城市' ''.
358   PERFORM frm_set_fieldcat USING 'AIRPFROM ' 'SPFLI' 'AIRPFROM' '始发机场' ''.
359   PERFORM frm_set_fieldcat USING 'CITYTO   ' 'SPFLI' 'CITYTO  ' '到达城市' ''.
360   PERFORM frm_set_fieldcat USING 'AIRPTO   ' 'SPFLI' 'AIRPTO  ' '目标机场' ''.
361   PERFORM frm_set_fieldcat USING 'FLTIME   ' 'SPFLI' 'FLTIME  ' '航班时间' ''.
362   PERFORM frm_set_fieldcat USING 'DEPTIME  ' 'SPFLI' 'DEPTIME ' '出发时间' ''.
363   PERFORM frm_set_fieldcat USING 'ARRTIME  ' 'SPFLI' 'ARRTIME ' '到达时间' ''.
364   PERFORM frm_set_fieldcat USING 'DISTANCE ' 'SPFLI' 'DISTANCE' '距离' ''.
365   PERFORM frm_set_fieldcat USING 'DISTID   ' 'SPFLI' 'DISTID  ' '大距离单位(公里,英里)' ''.
366   PERFORM frm_set_fieldcat USING 'FLTYPE   ' 'SPFLI' 'FLTYPE  ' '航班类型' ''.
367   PERFORM frm_set_fieldcat USING 'EMPLOYEE ' 'SPFLI' 'EMPLOYEE' '人物自定义帮助' 'X'.
368 ENDFORM.

 

posted @ 2023-03-14 22:53  今天123  阅读(1564)  评论(0编辑  收藏  举报
1