FUNCTION_ALV 下拉框的实现

下拉框可以用drdn_field或者使用DRDN_HNDL,这个文章主要是下拉框的基本使用,核心就是在fieldcat内表里面设置好下拉框的字段或者组的编号

文章目录

drdn_field

使用DRDN_HNDL

复制状态

完整代码

js 复制代码
*&---------------------------------------------------------------------*
*& Report Z_ALV_DROPDOWN
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*T_CODE         :
*CREATOR        :   LiuHongyu
*CREATE ON      :   2024年8月27日
*TYPE           :   Report
*Description    :   ALV下拉框
**********************************************************************
*               Change History
* ----------    -------------  ----------   -------------
* Version       Date            Change By    Description
* ----------    -------------  ----------   -------------
* 1             2024.8.27        Hongyu.Liu   创建
**********************************************************************
REPORT Z_ALV_DROPDOWN.

*----------------------Variables--------------------------*
TYPES: BEGIN OF GTY_HEAD,
         CARRID    TYPE SPFLI-CARRID, "航线
         CONNID    TYPE SPFLI-CONNID, "航班号
         PRICE     TYPE SFLIGHT-PRICE, "价格
         PLANETYPE TYPE SFLIGHT-PLANETYPE, "航班类型
         DEPTIME   TYPE SPFLI-DEPTIME, "出发时间
         DD_HANDLE TYPE INT4, "用于分辨分组的编号
       END OF GTY_HEAD.
DATA: GS_HEAD TYPE GTY_HEAD.
TYPES: BEGIN OF GTY_ITEMS,
         CARRID    TYPE SPFLI-CARRID, "航线
         CONNID    TYPE SPFLI-CONNID, "航班号
         COUNTRYFR TYPE SPFLI-COUNTRYFR, "国家/地区
         CITYFROM  TYPE SPFLI-CITYFROM, "起飞城市
         CITYTO    TYPE SPFLI-CITYTO, "到达城市
         FLTIME    TYPE SPFLI-FLTIME, "航班时间
         DISTANCE  TYPE SPFLI-DISTANCE, "距离
         CARRNAME  TYPE SCARR-CARRNAME, "航线
         CURRCODE  TYPE SCARR-CURRCODE, "航线货币
         URL       TYPE SCARR-URL, "URL
       END OF GTY_ITEMS.

DATA: BEGIN OF GS_OUT.
        INCLUDE STRUCTURE GS_HEAD. "抬头
DATA:   DETAIL TYPE TABLE OF GTY_ITEMS,
      END OF GS_OUT.
DATA: GT_OUT LIKE TABLE OF GS_OUT. "定义和GS_OUT工作区结构一样的内表

DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
      GS_FIELDCAT TYPE LVC_S_FCAT.
DATA: GS_LAYOUT   TYPE LVC_S_LAYO.   "ALV布局工作区

DATA:GS_DROP_DOWN TYPE LVC_S_DROP, "下拉框工作区
     GT_DROP_DOWN TYPE LVC_T_DROP. "下拉框内表

*定义事件内表
DATA:GT_EVENTS TYPE SLIS_T_EVENT,
     GS_EVENTS TYPE SLIS_ALV_EVENT.
*------------------------ Logic -----------------------------*

START-OF-SELECTION.
  PERFORM GET_DATA.
  PERFORM SET_FIELDCAT.
  PERFORM ALV_DISPLAY.



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_DROP_DOWN.
ENDFORM.



*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM GET_DATA .
  "抬头
  SELECT  CARRID,CONNID,PRICE FROM SFLIGHT WHERE CARRID = 'AZ' INTO TABLE @DATA(LT_HEAD) UP TO 20 ROWS.
  "细节
  SELECT A~CARRID,A~CONNID,COUNTRYFR,CITYFROM,CITYTO,FLTIME,DISTANCE,B~CARRNAME,B~CURRCODE,B~URL
    FROM SPFLI AS A
    LEFT JOIN SCARR AS B ON A~CARRID = B~CARRID
    FOR ALL ENTRIES IN  @LT_HEAD
    WHERE A~CARRID = @LT_HEAD-CARRID INTO TABLE @DATA(LT_ITEMS).

  LOOP AT LT_ITEMS ASSIGNING FIELD-SYMBOL(<FS_ITEM>).
    IF <FS_ITEM>-DISTANCE > 100.
      <FS_ITEM>-DISTANCE  = 100.
    ENDIF.
  ENDLOOP.

  LOOP AT LT_HEAD INTO GS_HEAD.
    APPEND INITIAL LINE TO GT_OUT ASSIGNING FIELD-SYMBOL(<FS_OUT>).
    <FS_OUT> = CORRESPONDING #( GS_HEAD ).
    <FS_OUT>-DETAIL = LT_ITEMS.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM SET_FIELDCAT .
  """""""""""""""""构建ALV字段"""""""""""""""""""
  "动态字段名称赋值
  DATA: LO_TABDESCR TYPE REF TO CL_ABAP_STRUCTDESCR.
  DATA: LS_FIELD_IN TYPE DFIES,
        LT_DFIES    TYPE DDFIELDS.
  LO_TABDESCR ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( GS_OUT ).
  CALL METHOD CL_SALV_DATA_DESCR=>READ_STRUCTDESCR
    EXPORTING
      R_STRUCTDESCR = LO_TABDESCR
    RECEIVING
      T_DFIES       = LT_DFIES.

  LOOP AT LT_DFIES INTO LS_FIELD_IN.
    MOVE-CORRESPONDING LS_FIELD_IN TO GS_FIELDCAT.
    GS_FIELDCAT-REF_TABLE = LS_FIELD_IN-REFTABLE. "参照类型的表,搜索帮助,域控制
    GS_FIELDCAT-REF_FIELD = LS_FIELD_IN-REFFIELD. "参照类型字段
    IF GS_FIELDCAT-COLTEXT IS INITIAL. "当COLTEXT不存在显示SCRTEXT_M
      GS_FIELDCAT-COLTEXT = LS_FIELD_IN-SCRTEXT_M.
    ENDIF.
    "设置下拉框的字段
    IF GS_FIELDCAT-FIELDNAME = 'PLANETYPE'.
      GS_FIELDCAT-EDIT = 'X'. "可编辑
      GS_FIELDCAT-DRDN_FIELD = 'DD_HANDLE'. "设置下拉框的字段是DD_HEANDLE
    ENDIF.
    IF  GS_FIELDCAT-FIELDNAME = 'DEPTIME' . "如果是出发时间字段,直接把下拉框分组设置成3
      GS_FIELDCAT-DRDN_HNDL = '3'. "DRDN_HNDL设置成3
      GS_FIELDCAT-EMPHASIZE = 'C310'."带有颜色的高亮列
    ENDIF.
    IF GS_FIELDCAT-FIELDNAME <> 'DD_HANDLE'.
      APPEND GS_FIELDCAT TO GT_FIELDCAT.
    ENDIF.

    CLEAR: LS_FIELD_IN,GS_FIELDCAT.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM ALV_DISPLAY .
  """""""""""""""""设置布局""""""""""""""""""
  GS_LAYOUT-ZEBRA = 'X'.
  GS_LAYOUT-CWIDTH_OPT = 'X'.


  """"""""""""""设置事件"""""""""""""""""""""
**把使 GRID 和 内表 连接在一起的事件存入事件内表
  GS_EVENTS-NAME = 'CALLER_EXIT'.    "这里是子例程名字
  GS_EVENTS-FORM = 'CALLER_EXIT'.     "这里是子例程名字
  APPEND GS_EVENTS TO GT_EVENTS.


  """""""""""""""""设置下拉框""""""""""""""""""
  "设置分组的字段的值
  LOOP AT GT_OUT INTO GS_OUT.
    IF GS_OUT-CONNID = '0555'.
      GS_OUT-DD_HANDLE = '1'.
    ELSE.
      GS_OUT-DD_HANDLE = '2'.
    ENDIF.

    MODIFY GT_OUT FROM GS_OUT.
    CLEAR GS_OUT.
  ENDLOOP.

  "设置下拉框
  DEFINE FILL_DROP.
    CLEAR GS_DROP_DOWN.
    GS_DROP_DOWN-HANDLE = &1.
    GS_DROP_DOWN-VALUE = &2.
    APPEND GS_DROP_DOWN TO GT_DROP_DOWN.
  END-OF-DEFINITION.
  FILL_DROP:'1' '大飞机'.
  FILL_DROP:'1' '小飞机'.
  FILL_DROP:'1' '中飞机'.
  FILL_DROP:'1' '漂亮飞机'.
  FILL_DROP:'1' '直升机'.
  FILL_DROP:'1' '战斗飞机'.
  FILL_DROP:'2' 'A飞机'.
  FILL_DROP:'2' 'B飞机'.
  FILL_DROP:'2' 'C飞机'.
  FILL_DROP:'3' '2024-08-01'.
  FILL_DROP:'3' '2024-08-02'.
  FILL_DROP:'3' '2024-08-03'.
  FILL_DROP:'3' '2024-08-07'.

  """""""""""""""""创建ALV"""""""""""""""""""
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*     I_INTERFACE_CHECK  = ' '
*     I_BYPASSING_BUFFER = I_BYPASSING_BUFFER
*     I_BUFFER_ACTIVE    = I_BUFFER_ACTIVE
      I_CALLBACK_PROGRAM = SY-REPID
*     I_CALLBACK_PF_STATUS_SET          = ' '
*     I_CALLBACK_USER_COMMAND           = ' '
*     I_CALLBACK_TOP_OF_PAGE            = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME   = I_STRUCTURE_NAME
*     I_BACKGROUND_ID    = ' '
*     I_GRID_TITLE       = I_GRID_TITLE
*     I_GRID_SETTINGS    = I_GRID_SETTINGS
      IS_LAYOUT_LVC      = GS_LAYOUT
      IT_FIELDCAT_LVC    = GT_FIELDCAT
*     IT_EXCLUDING       = IT_EXCLUDING
*     IT_SPECIAL_GROUPS_LVC             = IT_SPECIAL_GROUPS_LVC
*     IT_SORT_LVC        = IT_SORT_LVC
*     IT_FILTER_LVC      = IT_FILTER_LVC
*     IT_HYPERLINK       = IT_HYPERLINK
*     IS_SEL_HIDE        = IS_SEL_HIDE
*     I_DEFAULT          = 'X'
*     I_SAVE             = ' '
*     IS_VARIANT         = IS_VARIANT
      IT_EVENTS          = GT_EVENTS
*     IT_EVENT_EXIT      = IT_EVENT_EXIT
*     IS_PRINT_LVC       = IS_PRINT_LVC
*     IS_REPREP_ID_LVC   = IS_REPREP_ID_LVC
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE  = 0
*     I_HTML_HEIGHT_TOP  = I_HTML_HEIGHT_TOP
*     I_HTML_HEIGHT_END  = I_HTML_HEIGHT_END
*     IT_ALV_GRAPHICS    = IT_ALV_GRAPHICS
*     IT_EXCEPT_QINFO_LVC               = IT_EXCEPT_QINFO_LVC
*     IR_SALV_FULLSCREEN_ADAPTER        = IR_SALV_FULLSCREEN_ADAPTER
* IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           = E_EXIT_CAUSED_BY_CALLER
*     ES_EXIT_CAUSED_BY_USER            = ES_EXIT_CAUSED_BY_USER
    TABLES
      T_OUTTAB           = GT_OUT
* EXCEPTIONS
*     PROGRAM_ERROR      = 1
*     OTHERS             = 2
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.

核心代码

运行结果

┗( ▔, ▔ )┛

相关推荐
斌将军4 天前
BP新增页签增强
abap
LilySesy6 天前
SAP+Internet主题HTML样式选择
前端·html·sap·abap·internet服务·its·扫码枪
SAP学习成长之路6 天前
如何在SM30生成的维护表中增加选择框 CheckBox
开发语言·数据库·sap·健康医疗·abap·代码规范
SAPABAP东芝9 天前
【SAP-ABAP】MIRO--界面增强增加数据导出导入按钮
sap·abap·fico
cyqweqe1239 天前
SAP采购发票税额总额非自动计算。TAX不写行号
abap
乐乐823412 天前
ABAP:SET CURSOR FIELD设置鼠标焦点
abap
冰魄禾19 天前
SAP ABAP开发学习——BAPI
运维·开发语言·笔记·运维开发·sap·abap
Lisson 31 个月前
The ABAP program lines are wider than the internal table.
abap
Fireworks_me3 个月前
SAP 有趣的‘bug‘ 选择屏幕输入框没了
abap