最近经常听到网友和用户需求希望你在ALV Grid的函数中加入下来列表,其实SAP已经考虑了大家的需求,用户的需求是可以实现的。我特地将代码奉献给大家,供大家参考和学习。

代码如下:

REPORT.
************************************************************************
*                     DATEN DEFINITION                                 *
************************************************************************
TYPE-POOLS: slis.

TYPES:
       BEGIN OF t_data,
         matnr   TYPE makt-matnr,
         maktx   TYPE makt-maktx,
         spras   TYPE makt-spras,
       END   OF t_data,
       BEGIN OF t_alv,
         matnr     TYPE makt-matnr,
         maktx     TYPE makt-maktx,
         spras     TYPE makt-spras,
         dd_handle TYPE int4,
       END   OF t_alv.
DATA:
      gt_fieldcatalog      TYPE lvc_t_fcat,
      gw_fieldcatalog      TYPE lvc_s_fcat,
*定义存储下拉列表的数据     
      gt_ddval             TYPE lvc_t_drop,
      gw_ddval             TYPE lvc_s_drop,

     
      gt_events            TYPE slis_t_event,
      gw_events            TYPE slis_alv_event,
      gt_data              TYPE TABLE OF t_data,
      gw_data              TYPE          t_data,
      gt_alv               TYPE TABLE OF t_alv,
      gw_alv               TYPE          t_alv,
      g_repid              TYPE sy-repid VALUE sy-repid.
************************************************************************
*                     SELECTION SCREEN                                 *
************************************************************************
SELECT-OPTIONS: s_matnr FOR gw_data-matnr .

************************************************************************
*                     START-OF-SELECTION                               *
************************************************************************
START-OF-SELECTION.
  SELECT matnr maktx spras
  INTO TABLE gt_data up to 50 rows
  FROM makt
  WHERE matnr IN s_matnr
    and SPRAS = 'EN'.

  LOOP AT gt_data INTO gw_data.
    MOVE-CORRESPONDING gw_data TO gw_alv.
    APPEND gw_alv TO gt_alv.
  ENDLOOP.

  PERFORM creat_fieldcat.
  PERFORM creat_dropdown_values.
  PERFORM creat_event_exits.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program = g_repid
      it_fieldcat_lvc    = gt_fieldcatalog
      it_events          = gt_events
    TABLES
      t_outtab           = gt_alv.
*---------------------------------------------------------------------*
* 设置输出字段的格式
*---------------------------------------------------------------------*
FORM creat_fieldcat.
  CLEAR gw_fieldcatalog.
  gw_fieldcatalog-fieldname = 'MATNR'.
  gw_fieldcatalog-ref_field = 'MATNR'.
  gw_fieldcatalog-ref_table = 'MAKT'.
  APPEND gw_fieldcatalog TO gt_fieldcatalog.

  CLEAR gw_fieldcatalog.
  gw_fieldcatalog-fieldname = 'MAKTX'.
  gw_fieldcatalog-ref_field = 'MAKTX'.
  gw_fieldcatalog-ref_table = 'MAKT'.
  APPEND gw_fieldcatalog TO gt_fieldcatalog.

  CLEAR gw_fieldcatalog.
  gw_fieldcatalog-fieldname  = 'SPRAS'.
  gw_fieldcatalog-OUTPUTLEN = '7'.
  gw_fieldcatalog-dd_outlen = '7'.
  gw_fieldcatalog-intlen    = '2'.
  gw_fieldcatalog-inttype   = 'C'.
  gw_fieldcatalog-coltext   = 'Language'.
  gw_fieldcatalog-tooltip   = 'Language'.
  gw_fieldcatalog-seltext   = 'Language'.
  gw_fieldcatalog-drdn_field = 'DD_HANDLE'.
  gw_fieldcatalog-EDIT       = 'X'.
  APPEND gw_fieldcatalog TO gt_fieldcatalog.
ENDFORM.                    " creat_fieldcat
*---------------------------------------------------------------------*
*根据物料的所有的语言生成不同的生成下拉列表
*---------------------------------------------------------------------*
FORM creat_dropdown_values.
  DATA: l_spras TYPE makt-spras,
        l_count TYPE i.
  LOOP AT gt_alv INTO gw_alv.
    ADD 1 TO l_count.
    SELECT spras
       INTO l_spras
    FROM makt
    WHERE matnr = gw_alv-matnr.
      CLEAR gw_ddval.
      gw_ddval-handle = l_count.
      gw_ddval-value  = l_spras.
      APPEND gw_ddval TO gt_ddval.
    ENDSELECT.

    CLEAR gw_ddval.
    gw_ddval-handle = l_count.
    gw_ddval-value  = '                 '.
    APPEND gw_ddval TO gt_ddval.
*设置对应
    gw_alv-dd_handle = l_count.
    MODIFY gt_alv FROM gw_alv.
  ENDLOOP.
ENDFORM.                    " creat_dropdown_values

*---------------------------------------------------------------------*
*      Form  creat_event_exits
*---------------------------------------------------------------------*
FORM creat_event_exits .
  gw_events-name = 'CALLER_EXIT'.
  gw_events-form = 'CALLER_EXIT'.
  APPEND gw_events TO gt_events.
ENDFORM.                    " creat_event_exits
*---------------------------------------------------------------------*
*设置下拉列表,使Grid和内表能链接上
*---------------------------------------------------------------------*
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.                    "CALLER_EXIT