导语:最近在做WM的开发,功能比较复杂,其中涉及到对于ALV行数据的扩展处理,需要用到ALV单元格上展示按钮,并触发按钮事件,下面分享一下实现过程,文章末尾附带效果展示,如果对你帮助,请为Henry点一个赞,谢谢~
在需要实现展示按钮的ALV中添加控制字段参数「celtab」,这一步同样也在指定单元格可编辑中出现过,都是类似的原理
"行项目数据输出
TYPES: BEGIN OF ty_item,
vbeln TYPE lips-vbeln, "交货单号
posnr TYPE lips-posnr, "交货单行号
uecha TYPE lips-uecha, "上层物料
matnr TYPE lips-matnr, "物料
werks TYPE lips-werks, "工厂
lgort TYPE lips-lgort, "库存地点
charg TYPE lips-charg, "批次
ztjcw TYPE char8, "推荐仓位
lfimg TYPE lips-lfimg, "交货数量
meins TYPE lips-meins, "基本单位
arktx TYPE lips-arktx, "物料描述
zjpsl TYPE lips-lfimg, "拣配数量
“重点在这里??
celtab TYPE lvc_t_styl, "控制字段可编辑的参数
END OF ty_item.
同样的也要在layout中传递控制参数
gs_layout-stylefname = 'CELTAB'.
给ALV输出的内表,进行按钮赋值
通过函数「ICON_CREATE」先创建出来这个按钮,这里result的出参最好长一点,比如下面我的长度就可以,不然按钮的值承接不上。
DATA : lv_icon TYPE icons-text.
DATA : lv_stylerow TYPE lvc_s_styl.
" 创建按钮图标
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = 'ICON_ICON_LIST'
text = '推荐仓位'
info = '推荐仓位'
add_stdinf = 'X'
IMPORTING
result = lv_icon
EXCEPTIONS
icon_not_found = 1
outputfield_too_short = 2
OTHERS = 3.
LOOP AT gt_item ASSIGNING FIELD-SYMBOL(<fs_item>).
"添加按钮
lv_stylerow-fieldname = 'ZTJCW'.
lv_stylerow-style = cl_gui_alv_grid=>mc_style_button.
APPEND lv_stylerow TO <fs_item>-celtab.
<fs_item>-ztjcw = lv_icon.
ENDLOOP.
我这里使用OOALV实现的,所以直接添加事件就可以了,如果是Function ALV需要添加EVENT事件,然后也是一样通过调用「handle_button_click」事件实现。
具体代码如下
*--CLASS DEFINE
* LOCAL CLASS TO HANDLE SEMANTIC CHECKS
CLASS lcl_event_receiver DEFINITION DEFERRED.
DATA: g_event_receiver TYPE REF TO lcl_event_receiver.
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
"ALV 按钮事件
METHODS handle_button_click
FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING es_col_id es_row_no.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_button_click.
PERFORM button_click USING es_col_id es_row_no.
ENDMETHOD.
ENDCLASS.
我这里面是需要准备数据调用新的ALV,具体逻辑就不展示了,把框架放在这里
如果一行数据有多个单元格按钮,点击的时候可以根据p_col_id判断字段名字,我这里就一个,就偷懒了没有判断。
*&---------------------------------------------------------------------*
*& FORM BUTTON_CLICK
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> ES_COL_ID
*& --> ES_ROW_NO
*&---------------------------------------------------------------------*
FORM button_click USING p_col_id
p_row_no TYPE lvc_s_roid.
DATA : lv_lgort LIKE t001l-lgort.
RANGES r_lgort FOR t001l-lgort.
READ TABLE gt_item_alv INTO gs_item INDEX p_row_no-row_id.
IF sy-subrc = 0.
ENDIF.
CALL SCREEN 200 STARTING AT 10 2
ENDING AT 160 18. "调用屏幕展示推荐仓位
CALL METHOD gs_grid->refresh_table_display( is_stable = VALUE lvc_s_stbl( row = 'X' col = 'X' ) )." 功能A: 基本显示
ENDFORM.
CREATE OBJECT g_event_receiver. "释放需要的内容
SET HANDLER g_event_receiver->handle_button_click FOR pv_grid.
CALL METHOD pv_grid->set_table_for_first_display
EXPORTING
* I_CONSISTENCY_CHECK = 'X'
is_variant = gs_variant
it_toolbar_excluding = gt_ui_func
i_save = 'A'
i_default = 'X' "允许定义默认布局
is_layout = gs_layout
CHANGING
it_outtab = gt_item_alv
it_fieldcatalog = gt_fcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
作者:小飞猪猪猪猪猪猪猪–CSDN