- 很多用户不习惯原生GRID的选择模式,所以业务需要用到自定义的选择框来进行数据的操作,显示效果如图所示,增加一条选择列,且配置全选和全选全选的按钮功能,如下图所示。
- 实现这种功能需要用到Fieldcat的参数控制以及GUI状态的配置和处理
- 1.Fieldcat参数控制,将改成设置成选择框的样式,且该列可以被编辑。
** Fieldcat 变量定义
DATA:gt_fieldcat TYPE lvc_t_fcat,gs_fieldcat LIKE LINE OF gt_fieldcat.** FieldCat的宏
DATA gv_pos TYPE i.DEFINE %%add_fieldcat.gv_pos = gv_pos + 1.gs_fieldcat-col_pos = gv_pos.gs_fieldcat-fieldname = &1.gs_fieldcat-scrtext_m = &2.CASE &1. "根据fieldname字段给结构做赋值控制WHEN 'SEL'.gs_fieldcat-checkbox = 'X'. "该列选择框显示gs_fieldcat-edit = 'X'. "该列可以编辑ENDCASE.APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat.
END-OF-DEFINITION.** Fieldcat字段映射处理%%add_fieldcat:'EBELN' '采购单号', "( 前面字段值不可变 后面字段描述可变 每一行都是)'EBELP' '采购订单行号','SEL' '选择'.
- 2.GUI状态的配置和处理。需要在GUI状态添加自定义按钮,并且做数据更新。ALV增加自定义按钮参考:https://mp.csdn.net/mp_blog/creation/editor/136279698
FORM frm_user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield."应用方法将修改ALV的数据保存到内表中DATA lr_grid TYPE REF TO cl_gui_alv_grid.DATA l_tabix LIKE sy-tabix. "系统参数 行号"此处代码将用户输入读入内表(这一步不可少)CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' "自动刷新数据IMPORTINGe_grid = lr_grid.CALL METHOD lr_grid->check_changed_data.rs_selfield-refresh = 'X'. "此处代码阻止'REUSE_ALV_GRID_DISPLAY创建新的屏幕,造成屏幕多层rs_selfield-row_stable = 'X'. "保证行、列位置在自动刷新时不变rs_selfield-col_stable = 'X'.CASE r_ucomm.WHEN 'ALL'.LOOP AT gt_data INTO gw_data.gw_data-sel = 'X'.MODIFY gt_data FROM gw_data.ENDLOOP.WHEN 'SAL'.LOOP AT gt_data INTO gw_data.gw_data-sel = ''.MODIFY gt_data FROM gw_data.ENDLOOP.ENDCASE.ENDFORM.
TYPES:BEGIN OF ty_data,sel TYPE char1,ebeln TYPE ekpo-ebeln,ebelp TYPE ekpo-ebelp,END OF ty_data.DATA gt_data TYPE TABLE OF ty_data.
DATA gw_data TYPE ty_data.** Layout 变量定义
DATA gs_layout TYPE lvc_s_layo.** Fieldcat 变量定义
DATA:gt_fieldcat TYPE lvc_t_fcat,gs_fieldcat LIKE LINE OF gt_fieldcat.** FieldCat的宏
DATA gv_pos TYPE i.DEFINE %%add_fieldcat.gv_pos = gv_pos + 1.gs_fieldcat-col_pos = gv_pos.gs_fieldcat-fieldname = &1.gs_fieldcat-scrtext_m = &2.CASE &1.WHEN 'SEL'.gs_fieldcat-checkbox = 'X'. "当做选择框显示gs_fieldcat-edit = 'X'. "该字段可以编辑ENDCASE.APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat.
END-OF-DEFINITION.START-OF-SELECTION.SELECT ekpo~ebeln "采购订单抬头数据- 采购单号ekpo~ebelp "采购订单明细数据- 采购订单行号INTO CORRESPONDING FIELDS OF TABLE gt_dataFROM ekpoUP TO 20 ROWS.** Fieldcat字段映射处理%%add_fieldcat:'EBELN' '采购单号', "( 前面字段值不可变 后面字段描述可变 每一行都是)'EBELP' '采购订单行号','SEL' '选择'.** Layout布局处理gs_layout-cwidth_opt = 'X'. "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果)gs_layout-zebra = 'X'. "斑马纹输出** 调用FunctionALV需要传入的参数:展示数据,FieldCat,Layout,StatusCALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'EXPORTINGi_callback_program = sy-repidi_callback_pf_status_set = 'FRM_SET_STATUS'i_callback_user_command = 'FRM_USER_COMMAND'is_layout_lvc = gs_layoutit_fieldcat_lvc = gt_fieldcatTABLESt_outtab = gt_data[]EXCEPTIONSprogram_error = 1OTHERS = 2.FORM frm_set_status USING p_extab TYPE slis_t_extab.SET PF-STATUS 'STANDARD'.
ENDFORM.FORM frm_user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield."应用方法将修改ALV的数据保存到内表中DATA lr_grid TYPE REF TO cl_gui_alv_grid.DATA l_tabix LIKE sy-tabix. "系统参数 行号"此处代码将用户输入读入内表(这一步不可少)CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' "自动刷新数据IMPORTINGe_grid = lr_grid.CALL METHOD lr_grid->check_changed_data.rs_selfield-refresh = 'X'. "此处代码阻止'REUSE_ALV_GRID_DISPLAY创建新的屏幕,造成屏幕多层rs_selfield-row_stable = 'X'. "保证行、列位置在自动刷新时不变rs_selfield-col_stable = 'X'.CASE r_ucomm.WHEN 'ALL'.LOOP AT gt_data INTO gw_data.gw_data-sel = 'X'.MODIFY gt_data FROM gw_data.ENDLOOP.WHEN 'SAL'.LOOP AT gt_data INTO gw_data.gw_data-sel = ''.MODIFY gt_data FROM gw_data.ENDLOOP.ENDCASE.ENDFORM.