ABAP - Function ALV 11 - 单元格下拉框

本文详细描述了如何在SAPABAP中使用ALV(AdvancedListView)创建表格时,为特定字段(如考试类型)实现动态下拉框的选择,通过内表存储选项并将其与事件处理结合起来,以提供用户友好的报表编辑体验。
摘要由CSDN通过智能技术生成
  • 有时用户为了在报表编辑时更方便一些,会提出ALV编辑时进行下拉框的赋值
  • 实现步骤
  1. 定义一个存储下拉框的内表
    **这两个字段是成组出现的 缺一不可
    TYPES:BEGIN OF ty_data,
            list    TYPE int4,                "下拉列表组
            kslx    TYPE c LENGTH 20,         "考试类型
          END OF ty_data.
    
    "*定义存储下拉列表的数据内表
    DATA:gt_ddval  TYPE lvc_t_drop,
    
    "设置下拉框内表的值
     gt_ddval[] = VALUE #(
                          ( handle = '1' value = '挂科')
                          ( handle = '1' value = '补考')
                          ( handle = '1' value = '通过')
                        ).
    
    
    
    
    

  2. Fieldcat的参数EDIT和DRDN_HNDL赋值
    DEFINE %%add_fieldcat.
      gs_fieldcat-fieldname  = &1.
      gs_fieldcat-scrtext_m  = &2.
      gs_fieldcat-edit       = &3.  "当前列可编辑
      gs_fieldcat-drdn_field = &4.  "下拉列表
      APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat.
    END-OF-DEFINITION.
    
    ** Fieldcat字段映射处理
    %%add_fieldcat:'SEL'      '选择'  'X' '',  
                   'LIGHT'    '状态'  '' '',
                   'REMARK'   '备注'  '' '',
                   'NAME'     '姓名'  '' '',
                   'SCORE'    '分数'  '' '',
                   'KSLX'     '考试类型' 'X' 'LIST'. "这里面的LIST就是ALV输出内表定义里的list

  3. 生成下拉列表,通过事件GRID和内表连接在一起
    **定义事件内表
    gt_events TYPE slis_t_event,
    gw_events TYPE slis_alv_event.
    
    **把使 GRID 和 内表 连接在一起的事件存入事件内表
    gw_events-name = 'CALLER_EXIT'.    "这里是子例程名字
    gw_events-form = 'CALLER_EXIT'.     "这里是子例程名字
    APPEND gw_events TO gt_events.
    
    FORM caller_exit USING ls_data TYPE slis_data_caller_exit.
      DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = l_ref_alv.
      CALL METHOD l_ref_alv->set_drop_down_table
        EXPORTING
          it_drop_down = gt_ddval.
    ENDFORM.
    
     
  4. Function ALV调用 
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
        EXPORTING
          it_events          = gt_events
        TABLES
          t_outtab           = gt_data.



  • 完整代码 
    TYPES:BEGIN OF ty_data,
            sel    TYPE char1,
            light  TYPE iconname,
            name   TYPE char10,
            score  TYPE p LENGTH 2 DECIMALS 1,
            remark TYPE c LENGTH 20,
            list   TYPE int4,                "下拉列表组1
            kslx   TYPE c LENGTH 20,
          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.
    
    "*定义存储下拉列表的数据
    DATA:gt_ddval  TYPE lvc_t_drop,
         gs_ddval  TYPE lvc_s_drop,
    
         gt_events TYPE slis_t_event,
         gw_events TYPE slis_alv_event.
    
    DEFINE %%add_fieldcat.
      gs_fieldcat-fieldname  = &1.
      gs_fieldcat-scrtext_m  = &2.
      gs_fieldcat-edit       = &3.  "当前列可编辑
      gs_fieldcat-drdn_field = &4.  "下拉列表
      APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat.
    END-OF-DEFINITION.
    
    START-OF-SELECTION.
    
      PERFORM get_data.
      PERFORM dis_falv.
    
    FORM get_data.
      CLEAR:gt_data[],gt_ddval[].
    
      gt_data[] = VALUE #(
                         ( sel = '' light = '' name = 'TOM'    score = '58' list = 1 )
                         ( sel = '' light = '' name = 'JEERY'  score = '98' list = 1 )
                         ( sel = '' light = '' name = 'MASTER' score = '68' list = 1 )
                       ).
      "设置下拉框的值
      gt_ddval[] = VALUE #(
                          ( handle = '1' value = '挂科')
                          ( handle = '1' value = '补考')
                          ( handle = '1' value = '通过')
                        ).
    
    
      LOOP AT gt_data INTO gw_data.
        IF gw_data-score < 60.
          gw_data-light =  icon_red_light.
          gw_data-remark = '不及格'.
        ELSEIF gw_data-score >= 60 AND  gw_data-score < 85.
          gw_data-light =  ICON_yellow_LIGHT.
          gw_data-remark = '良好'.
        ELSEIF gw_data-score >= 85.
          gw_data-light =  ICON_green_LIGHT.
          gw_data-remark = '优秀'.
        ENDIF.
        MODIFY gt_data FROM gw_data.
      ENDLOOP.
    ENDFORM.
    
    
    FORM dis_falv.
    ** Fieldcat字段映射处理
      %%add_fieldcat:'SEL'      '选择'  'X' '',
                     'LIGHT'    '状态'  '' '',
                     'REMARK'   '备注'  '' '',
                     'NAME'     '姓名'  '' '',
                     'SCORE'    '分数'  '' '',
                     'KSLX'     '考试类型' 'X' 'LIST'."这里面的LIST就是ALV输出内表定义里的list
    
    ** Layout布局处理
      gs_layout-cwidth_opt = 'X'.     "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果)
      gs_layout-zebra = 'X'.          "斑马纹输出
    
    ** 事件处理
      gw_events-name = 'CALLER_EXIT'.
      gw_events-form = 'CALLER_EXIT'.
      APPEND gw_events TO gt_events.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
        EXPORTING
          i_callback_program = sy-repid
          is_layout_lvc      = gs_layout
          it_fieldcat_lvc    = gt_fieldcat[]
          it_events          = gt_events
        TABLES
          t_outtab           = gt_data.
    ENDFORM.
    
    FORM caller_exit USING ls_data TYPE slis_data_caller_exit.
      DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = l_ref_alv.
      CALL METHOD l_ref_alv->set_drop_down_table
        EXPORTING
          it_drop_down = gt_ddval.
    ENDFORM.

ABAP FUNCTION ALV 中添加状态栏下拉框按钮,可以使用函数模块 `REUSE_ALV_HIERSEQ_LIST_DISPLAY`。该函数模块可以在状态栏中添加下拉框按钮,用于切换不同的层次结构。 以下是添加下拉框按钮的代码示例: ``` REPORT ztest. DATA: it_list TYPE STANDARD TABLE OF sflight, wa_list TYPE sflight. DATA: it_fieldcat TYPE slis_t_fieldcat_alv. FIELD-SYMBOLS: <fs_fieldcat> TYPE slis_fieldcat_alv. * 初始化 ALV 数据 SELECT * FROM sflight INTO TABLE it_list. * 初始化字段目录 CLEAR: it_fieldcat. wa_fieldcat-fieldname = 'CARRID'. wa_fieldcat-ref_table = 'SCARR'. wa_fieldcat-ref_field = 'CARRID'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'CONNID'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'FLDATE'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'SEATSMAX'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'SEATSOCC'. APPEND wa_fieldcat TO it_fieldcat. CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'PF_STATUS' is_layout = VALUE #( ( hierarchy_seq = 'CARRID' ) ) TABLES t_fieldcat = it_fieldcat t_outtab = it_list. * 定义层次结构下拉框的值集合 DATA: it_hier TYPE STANDARD TABLE OF lvc_s_layo WITH HEADER LINE. it_hier-fieldname = 'HIERARCHY_SEQ'. it_hier-seltext_l = '层次结构'. it_hier-selopt = 'O'. it_hier-no_out = 'X'. APPEND it_hier. it_hier-fieldname = 'CARRID'. it_hier-seltext_l = '按航空公司'. it_hier-selopt = 'C'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'FLDATE'. it_hier-seltext_l = '按航班日期'. it_hier-selopt = 'D'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'SEATSMAX'. it_hier-seltext_l = '按最大座位数'. it_hier-selopt = 'S'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'SEATSOCC'. it_hier-seltext_l = '按已占用座位数'. it_hier-selopt = 'O'. it_hier-no_out = ' '. APPEND it_hier. * 设置层次结构下拉框 SET PF-STATUS 'PF_STATUS' EXCLUDING it_hier. * PF-STATUS 事件处理程序 MODULE pf_status OUTPUT. SET PF-STATUS 'PF_STATUS'. SET TITLEBAR 'TITLE'. ENDMODULE. * AT SELECTION-SCREEN 事件处理程序 AT SELECTION-SCREEN ON HIERARCHY_SEQ. LOOP AT it_hier ASSIGNING FIELD-SYMBOL(<fs_hier>). IF <fs_hier>-fieldname = 'HIERARCHY_SEQ'. CONTINUE. ENDIF. IF <fs_hier>-selopt = 'X'. CLEAR: it_list. SELECT * FROM sflight INTO TABLE it_list ORDER BY carrid. ELSE. CLEAR: it_list. SELECT * FROM sflight INTO TABLE it_list ORDER BY ( <fs_hier>-fieldname ). ENDIF. CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'PF_STATUS' is_layout = VALUE #( ( hierarchy_seq = <fs_hier>-fieldname ) ) TABLES t_fieldcat = it_fieldcat t_hierarchy = it_list. ENDLOOP. ``` 在代码中,使用了 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块来显示 ALV 报表,并在 `is_layout` 参数中指定默认的层次结构。在 `pf_status` 事件处理程序中设置状态栏的标题,并在 `AT SELECTION-SCREEN` 事件处理程序中根据用户选择的层次结构重新查询数据并刷新 ALV 报表。 通过在 `it_hier` 中定义层次结构下拉框的值集合,并在 `SET PF-STATUS` 语句中排除该值集合,即可在状态栏中添加下拉框按钮。在 `AT SELECTION-SCREEN` 事件处理程序中,根据用户选择的层次结构重新查询数据,并使用 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块刷新 ALV 报表显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值