ABAP ALV 单元格 设置下拉框

  最近有个用户需求希望在 ALV Grid 的函数中加入下来列表,其实SAP已经考虑了大家的需求,用户的需求是可以实现的。这个需求是很合理的,会了不难,难了不会,第一次做  比着葫芦画瓢 把这个需求实现。。。

把关键代码和关键步骤 先罗列出来,最后附上一个完整的例子代码。

*定义存储下拉列表的数据   
data: ltd_event type table of slis_alv_event,
      lth_event type slis_alv_event,
      gt_ddval  type lvc_t_drop,
      gw_ddval  type lvc_s_drop.

perform frm_get_data.                 "根据选择条件进行取数
perform frm_set_layout.            
perform frm_set_fieldcat.
                
perform creat_dropdown_values.        "相比正常的ALV 添加了这两个步骤
perform creat_event_exits .

perform frm_display_alv.
form creat_dropdown_values.
  data: l_spras type makt-spras,
        l_count type i.


  clear gw_ddval.
  gw_ddval-handle = '1'.
  gw_ddval-value  = '工程集成事业群人工+费用'.
  append gw_ddval to gt_ddval.

  clear gw_ddval.
  gw_ddval-handle = '1'.
  gw_ddval-value  = '国际部门人工+费用'.
  append gw_ddval to gt_ddval.



  clear gw_ddval.
  gw_ddval-handle = '2'.
  gw_ddval-value  = '充电设备类'.
  append gw_ddval to gt_ddval.

  clear gw_ddval.
  gw_ddval-handle = '2'.
  gw_ddval-value  = 'EPC/PC总包类 '.
  append gw_ddval to gt_ddval.

  clear gw_ddval.
  gw_ddval-handle = '2'.
  gw_ddval-value  = '纯电力设备销售类'.
  append gw_ddval to gt_ddval.

  clear gw_ddval.
  gw_ddval-handle = '2'.
  gw_ddval-value  = ' 纯工程'.
  append gw_ddval to gt_ddval.



*设置对应

endform.                    " creat_dropdown_values
*---------------------------------------------------------------------*
*      Form  creat_event_exits
*---------------------------------------------------------------------*
form creat_event_exits .
  lth_event-name = 'CALLER_EXIT'.
  lth_event-form = 'CALLER_EXIT'.
  append lth_event to ltd_event.
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

设置

*&---------------------------------------------------------------------*
*&      Form  FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*       定义ALV字段结构
*----------------------------------------------------------------------*
form frm_set_fieldcat.
  data:l_day   type sy-datum,
       l_index type c.
  clear:gw_fieldcat.
  define fieldcat.
    gw_fieldcat-fieldname = &1.
    gw_fieldcat-coltext   = &2.
    gw_fieldcat-edit      = &3.
*    gw_fieldcat-EDIT      = &3.
    gw_fieldcat-ref_table = &4.
    gw_fieldcat-ref_field = &5.
    gw_fieldcat-decimals  = 3.
    gw_fieldcat-quantity  = &6.
    gw_fieldcat-DRDN_HNDL  = &7.     "通过该字段进行分组
    append gw_fieldcat to gt_fieldcat.
    clear gw_fieldcat.
  end-of-definition.

  data : fieldname type string.

  fieldcat 'FYDL' '费用大类' 'X' '' '' '' '1'.
  fieldcat 'YWDL' '业务大类' 'X' '' '' '' '2'.
  fieldcat 'FPBL' '分配比例' 'X' '' '' '' ''.
  fieldcat 'DOUSER' '用户名' '' '' '' '' ''.
  fieldcat 'DODATE' '操作时间' '' '' '' '' ''.

endform.                    " FRM_SET_FIELDCAT

还有一个比较关键的点,别忘了

form frm_display_alv .
  call function 'REUSE_ALV_GRID_DISPLAY_LVC'
    exporting
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'FRM_PF_STATUS'
      i_callback_user_command  = 'FRM_USER_COMMAND'
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      i_default                = 'X'
      i_save                   = 'A'
      it_events                = ltd_event     "这一步一定要记得添加上
    tables
      t_outtab                 = gt_alv
    exceptions
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

endform.                    " FRM_DISPLAY_ALV

总结一下步骤:1.定义一个存储下拉框的内表

2.在设置 fieldcat 的时候 通过设置  DRDN_HNDL 字段 来对数据进行分组

3.生成下拉列表

4.添加方法   使 GRID 和 内表 连接在一起

5.记得在调用alv的时候将  it_events    = ltd_event   添加上

最后附加一段简单的实例代码,可直接粘贴下来运行测试:

report.
************************************************************************
*                     DATEN DEFINITION                                 *
************************************************************************
type-pools: slis.
tables:makt.
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 makt-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 = '1'.

  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

猜你喜欢

转载自blog.csdn.net/fengxin_/article/details/108102343