BCALV_EDIT_07

本文介绍如何在SAP ALV中为特定输出表格单元格定义下拉列表框。通过四个关键步骤,包括定义额外字段、设置下拉列表并传递给ALV、设置字段为可编辑及指定列表框句柄等,实现根据不同条件展示不同的下拉选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

program bcalv_edit_07.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This example shows how to define dropdown listboxes for
* particular cells of your output table.
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Conceive that customers are only allowed to smoke in the
* first class. For this reason, if the customer is a smoker
* only the first class (F) can be chosen in column 'CLASS',
* otherwise all classes.
*-----------------------------------------------------------------
* Essential steps (search for '§')
* ~~~~~~~~~~~~~~~
* 1.Define an extra field in your output table
* 2.Define a dropdown table and pass it to ALV.
* 3.Set your dropdown field editable and assign the fieldname of the
*   corresponding additional field to DRDN_FIELD of the fieldcatalog.
* 4.Set the handle of your additional field of your output
*   table according to the listbox that shall be displayed.
*-----------------------------------------------------------------------
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

data: ok_code like sy-ucomm,
      save_ok like sy-ucomm,
      g_container type scrfname value 'BCALV_GRID_DEMO_0100_CONT1',
      g_grid  type ref to cl_gui_alv_grid,
      g_custom_container type ref to cl_gui_custom_container,
      gt_fieldcat type lvc_t_fcat,
      gs_layout type lvc_s_layo,
      g_max type i value 100.

*§1.Define an extra field in your output table
*   for each column where you want to use drop down listboxes.
*   (One additional field refers to cells of one column).
data: begin of gt_outtab occurs 0.
        include structure sbook.
data: drop_down_handle type int4.
data: end of gt_outtab.

*---------------------------------------------------------------------*
*       MAIN                                                          *
*---------------------------------------------------------------------*
end-of-selection.
  call screen 100.

*---------------------------------------------------------------------*
*       MODULE PBO OUTPUT                                             *
*---------------------------------------------------------------------*
module pbo output.
  set pf-status 'MAIN100'.
  set titlebar 'MAIN100'.
  if g_custom_container is initial.
    perform. create_and_init_alv changing gt_outtab[]
                                         gt_fieldcat.
  endif.

endmodule.
*---------------------------------------------------------------------*
*       MODULE PAI INPUT                                              *
*---------------------------------------------------------------------*
module pai input.
  save_ok = ok_code.
  clear ok_code.
  case save_ok.
    when 'EXIT'.
      perform. exit_program.
    when others.
*     do nothing
  endcase.
endmodule.
*---------------------------------------------------------------------*
*       FORM. EXIT_PROGRAM                                             *
*---------------------------------------------------------------------*
form. exit_program.
  leave program.
endform.
*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      *----------------------------------------------------------------------*
form. build_fieldcat changing pt_fieldcat type lvc_t_fcat.

  data ls_fcat type lvc_s_fcat.

  call function 'LVC_FIELDCATALOG_MERGE'
       exporting
            i_structure_name = 'SBOOK'
       changing
            ct_fieldcat      = pt_fieldcat.

  loop at pt_fieldcat into ls_fcat.
* Exchange smoker field with invoice field - just to
* make the dependance between SMOKER and CLASS more transparent
* (Smoking is only allowed in the first class).
    if ls_fcat-fieldname eq 'SMOKER'.
      ls_fcat-col_pos = 11.
      ls_fcat-outputlen = 10.
      modify pt_fieldcat from ls_fcat.

    elseif ls_fcat-fieldname eq 'INVOICE'.
      ls_fcat-col_pos = 7.
      modify pt_fieldcat from ls_fcat.

    elseif    ls_fcat-fieldname eq 'CLASS'.

*§3.Set your dropdown field editable and assign the fieldname of the
*   corresponding additional field to DRDN_FIELD of the fieldcatalog.
      ls_fcat-edit = 'X'.
      ls_fcat-drdn_field = 'DROP_DOWN_HANDLE'.
      ls_fcat-outputlen = 5.

* Field 'checktable' is set to avoid shortdumps that are caused
* by inconsistend data in check tables. You may comment this out
* when the test data of the flight model is consistent in your system.
      ls_fcat-checktable = '!'.        "do not check foreign keys

      modify pt_fieldcat from ls_fcat.
    endif.
  endloop.

endform.
*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      *      *      *----------------------------------------------------------------------*
form. create_and_init_alv changing pt_outtab type standard table
                                  pt_fieldcat type lvc_t_fcat.

  data: lt_exclude type ui_functions.

  create object g_custom_container
         exporting container_name = g_container.
  create object g_grid
         exporting i_parent = g_custom_container.

  perform. build_fieldcat changing pt_fieldcat.

* Optionally restrict generic functions to 'change only'.
*   (The user shall not be able to add new lines).
  perform. exclude_tb_functions changing lt_exclude.

  perform. set_drdn_table.

  perform. build_data changing pt_outtab.

  call method g_grid->set_table_for_first_display
       exporting it_toolbar_excluding  = lt_exclude
       changing  it_fieldcatalog       = pt_fieldcat
                 it_outtab             = pt_outtab[].

* Set editable cells to ready for input initially
  CALL METHOD g_grid->set_ready_for_input
   EXPORTING
    I_READY_FOR_INPUT = 1.

endform.                               "CREATE_AND_INIT_ALV

*&---------------------------------------------------------------------*
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      *----------------------------------------------------------------------*
form. exclude_tb_functions changing pt_exclude type ui_functions.
* Only allow to change data not to create new entries (exclude
* generic functions).

  data ls_exclude type ui_func.

  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
  append ls_exclude to pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
  append ls_exclude to pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
  append ls_exclude to pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
  append ls_exclude to pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
  append ls_exclude to pt_exclude.

endform.                               " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*&      Form  set_drdn_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  --&gt  p1        text
*  *----------------------------------------------------------------------*
form. set_drdn_table.
*§2.Define a dropdown table and pass it to ALV.
*   One listbox is referenced by a handle, e.g., '1'.
*   For each entry that shall appear in this listbox
*   you have to append a line to the dropdown table
*   with handle '1'.
*   This handle can be assigned to several columns
*   of the output table using the field catalog.

  data: lt_dropdown type lvc_t_drop,
        ls_dropdown type lvc_s_drop.

  ls_dropdown-handle = '1'.
  ls_dropdown-value = 'F'.
  append ls_dropdown to lt_dropdown.

  ls_dropdown-handle = '2'.
  ls_dropdown-value = 'F'.
  append ls_dropdown to lt_dropdown.

  ls_dropdown-handle = '2'.
  ls_dropdown-value = 'C'.
  append ls_dropdown to lt_dropdown.

  ls_dropdown-handle = '2'.
  ls_dropdown-value = 'Y'.
  append ls_dropdown to lt_dropdown.

  call method g_grid->set_drop_down_table
              exporting it_drop_down = lt_dropdown.

endform.                               " set_drdn_table
*&---------------------------------------------------------------------*
*&      Form  build_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  --&gt  p1        text
*  *----------------------------------------------------------------------*
form. build_data changing pt_outtab type standard table.

  data: lt_sbook type table of sbook,
        ls_sbook type sbook,
        l_index type i,
        ls_outtab like line of gt_outtab.

  select * from sbook into table lt_sbook up to g_max rows.  "#EC CI_NOWHERE
  if sy-subrc ne 0.
    perform. generate_entries changing lt_sbook.
  endif.
*§4.Set the handle of your additional field of your output
*   table according to the listbox that shall be displayed.
  loop at lt_sbook into ls_sbook.
    l_index = sy-tabix.
    move-corresponding ls_sbook to ls_outtab.
    clear ls_outtab-class.
* Alternate between smoker and non smoker to make
* it more obvious what this example is about
    l_index = l_index mod 2.
    if l_index eq 1.
       ls_outtab-smoker = 'X'.
    else.
       ls_outtab-smoker = ' '.
    endif.
*
    if ls_outtab-smoker eq 'X'.
      ls_outtab-drop_down_handle = '1'.
    else.
      ls_outtab-drop_down_handle = '2'.
    endif.
    append ls_outtab to pt_outtab.
  endloop.


endform.                               " build_data

*&---------------------------------------------------------------------*
*&      Form  generate_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      *----------------------------------------------------------------------*
form. generate_entries changing pt_sbook type standard table.
  data: ls_sbook type sbook,
        l_month(2) type c,
        l_day(2) type c,
        l_date(8) type c,
 l_prebookid type i.


  ls_sbook-carrid = 'LH'.
  ls_sbook-connid = '0400'.
  ls_sbook-forcurkey = 'DEM'.
  ls_sbook-loccurkey = 'USD'.
  ls_sbook-custtype = 'B'.

  do 110 times.
    l_prebookid = sy-index.

    ls_sbook-forcuram = sy-index * 10.
    ls_sbook-loccuram = ls_sbook-loccuram * 2.
    ls_sbook-customid = sy-index.
    ls_sbook-counter = 18.
    ls_sbook-agencynum = 11.

    l_month = sy-index / 10 + 1.
    do 2 times.
      l_day = 3 + l_month + sy-index * 2.
      l_date+0(4) = '2000'.
      l_date+4(2) = l_month.
      l_date+6(2) = l_day.
      ls_sbook-fldate = l_date.
      subtract 3 from l_day.
      ls_sbook-order_date+0(6) = l_date+0(6).
      ls_sbook-order_date+6(2) = l_day.
      ls_sbook-bookid = l_prebookid * 2 + sy-index.
      if sy-index eq 1.
        ls_sbook-smoker = 'X'.
      else.
        ls_sbook-smoker = space.
      endif.

      ls_sbook-luggweight = l_prebookid * 10.
      if ls_sbook-luggweight ge 1000.
        ls_sbook-wunit = 'G'.
        ls_sbook-class = 'C'.
      else.
        ls_sbook-wunit = 'KG'.
        ls_sbook-class = 'Y'.
      endif.

      if ls_sbook-bookid > 40 and ls_sbook-wunit eq 'KG'.
        ls_sbook-invoice = 'X'.
      endif.
      if ls_sbook-bookid eq 2.
        ls_sbook-cancelled = 'X'.
        ls_sbook-class = 'F'.
      endif.

      append ls_sbook to pt_sbook.
    enddo.
  enddo.
endform.                               " generate_entries

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14397246/viewspace-611090/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14397246/viewspace-611090/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值