abap--关于在REUSE_ALV_GRID_DISPLAY将单元或行标识不同的颜色的处理收集

这篇博客详细介绍了如何在ABAP的REUSE_ALV_GRID_DISPLAY中设置行和列的颜色,包括指定行显示不同颜色、不同列显示不同颜色以及特定单元格显示不同颜色的方法,提供了具体的代码示例。

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

一、指定行显示不同颜色
参见:http://blog.youkuaiyun.com/CompassButton/archive/2006/09/08/1195672.aspx 

二、不同列显示不同颜色
1)对于非key字段则可以通过设置字段格式(slis_fieldcat_alv)的emphasize指定,代码如下:
  data: wa_fieldcat type slis_fieldcat_alv.
  clear wa_fieldcat.
  wa_fieldcat-ref_fieldname = im_ref_field.
  wa_fieldcat-ref_tabname = im_ref_table.
  wa_fieldcat-fieldname = im_fieldname.
  wa_fieldcat-tabname   = im_tabname.
  wa_fieldcat-key       = ‘’.
  wa_fieldcat-fix_column = IM_fix.
  wa_fieldcat-checkbox  = im_checkbox.
  wa_fieldcat-do_sum     = im_dosum.
  wa_fieldcat-no_zero     = im_zero.
  wa_fieldcat-no_out     = im_out.
  wa_fieldcat-seltext_l = im_seltex_l.
  wa_fieldcat-seltext_m = im_seltex_l.
  wa_fieldcat-seltext_s = im_seltex_l.
  wa_fieldcat-outputlen = im_outputlen.
  wa_fieldcat-decimals_out = im_dec.
  wa_fieldcat--emphasize = 'C500'.
*  wa_fieldcat-col_pos   = IM_colpos.
  append wa_fieldcat to re_field.
2)对于key字段的设置参见单元颜色设置一个特例(整列的所有单元)

三、指定单元格显示不同颜色(参见红色代码)
代码
report  Ztest_fullscreencolor.
type-pools: slis.
class cl_gui_resources definition load.
types: begin of g_ty_s_test,
         select_amount            type i,
         color_rows               type i occurs 0,
         color_fields_column      type lvc_fname occurs 0,
         color_fields_cell        type lvc_fname occurs 0,
       end of   g_ty_s_test,

       begin of g_ty_s_outtab_slis.
include type alv_t_t2.
 *定义颜色设置字段
types:   color(4)  type c,
         tabcolor  type slis_t_specialcol_alv,

       end   of g_ty_s_outtab_slis,
       g_ty_t_outtab_slis type table of g_ty_s_outtab_slis.

constants: con_true         type char1 value 'X'.

field-symbols: <gt_outtab> type standard table.

data: gs_test   type g_ty_s_test,
      gt_outtab_slis  type g_ty_t_outtab_slis,
      g_repid type sy-repid.

data: g_field     type lvc_s_fcat-fieldname,
      g_int_field type i.
*----------------------------------------------------------------------*
* SELECTION-SCREEN                                                     *
*----------------------------------------------------------------------
selection-screen begin of block col with frame title text-col.
parameters:
p_col01 as checkbox.
select-options:
p_colf01 for g_int_field no intervals default 1.
parameters:
p_col02 as checkbox  default 'X'.
select-options:
p_colf02 for g_field no intervals default 'CARRID'.
parameters:
p_col03 as checkbox .
select-options:
p_colf03 for g_field no intervals default 'CONNID'.
selection-screen end of block col.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST                                 *
*----------------------------------------------------------------------*
at selection-screen on value-request for p_colf02-low.
  perform f01_f4_fcode changing p_colf02-low.

at selection-screen on value-request for p_colf03-low.
  perform f01_f4_fcode changing p_colf03-low.

*----------------------------------------------------------------------*
* START-OF-SELECTION                                                   *
*----------------------------------------------------------------------*
start-of-selection.
  g_repid               = sy-repid.

  gs_test-select_amount = 30.

  if p_col01 eq con_true.
    loop at p_colf01.
      if not p_colf01-low is initial.
        append p_colf01-low to gs_test-color_rows.
      endif.
    endloop.
  endif.
  if p_col02 eq con_true.
    loop at p_colf02.
      if not p_colf02-low is initial.
        append p_colf02-low to gs_test-color_fields_column.
      endif.
    endloop.
  endif.
  if p_col03 eq con_true.
    loop at p_colf03.
      if not p_colf03-low is initial.
        append p_colf03-low to gs_test-color_fields_cell.
      endif.
    endloop.
  endif.
*----------------------------------------------------------------------*
* END-OF-SELECTION                                                     *
*----------------------------------------------------------------------*
end-of-selection.
  perform f01_call_fullscreen.

*&---------------------------------------------------------------------*
*&      Form  f01_call_fullscreen
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_call_fullscreen .
  data: ls_layo   type slis_layout_alv,
        lt_fcat   type slis_t_fieldcat_alv.
  perform f01_get_outtab.
  perform f01_set_layo changing ls_layo.
  perform f01_set_fcat changing lt_fcat.
  perform f01_set_color changing lt_fcat  ls_layo.
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program                = g_repid
*传入控制信息
      is_layout                         = ls_layo
      it_fieldcat                       = lt_fcat

    tables
      t_outtab                          = <gt_outtab>
   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.                    " f01_call_fullscreen
*&---------------------------------------------------------------------*
*&      Form  f01_get_outtab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_get_outtab.
      select * from alv_t_t2
               into corresponding fields of table gt_outtab_slis
               up to gs_test-select_amount rows
               order by primary key.

      assign gt_outtab_slis to <gt_outtab>.
endform.                    " f01_get_outtab
*&---------------------------------------------------------------------*
*&      Form  f01_set_layo
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_set_layo changing cs_layo type slis_layout_alv.

  data: l_text00 type sy-title,
        l_text01 type sy-title,
        l_text02 type sy-title,
        l_text03 type sy-title.
*... Display options
  cs_layo-colwidth_optimize      = space.
  cs_layo-no_colhead             = space.
  cs_layo-no_hotspot             = space.
  cs_layo-zebra                  = space.
  cs_layo-no_vline               = space.
  cs_layo-no_hline               = space.
  cs_layo-cell_merge             = space.
  cs_layo-no_min_linesize        = space.
  cs_layo-min_linesize           = space.
  cs_layo-max_linesize           = space.
  sy-title = 'Test'.
  set titlebar 'D0100' with cs_layo-window_titlebar.
  cs_layo-no_uline_hs            = space.
  cs_layo-countfname             = space.
*... Exceptions
  cs_layo-lights_fieldname       = space.
  cs_layo-lights_tabname         = space.
  cs_layo-lights_rollname        = space.
  cs_layo-lights_condense        = space.
*... Sums
  cs_layo-no_sumchoice           = space.
  cs_layo-no_totalline           = space.
  cs_layo-totals_before_items    = space.
  cs_layo-totals_only            = space.
  cs_layo-totals_text            = space.
  cs_layo-no_subchoice           = space.
  cs_layo-no_subtotals           = space.
  cs_layo-subtotals_text         = space.
  cs_layo-numc_sum               = space.
  cs_layo-no_unit_splitting      = space.
*... Interaction
  cs_layo-box_fieldname          = space.
  cs_layo-box_tabname            = space.
  cs_layo-box_rollname           = space.
  cs_layo-expand_fieldname       = space.
  cs_layo-hotspot_fieldname      = space.
  cs_layo-no_input               = space.
  cs_layo-f2code                 = space.
  cs_layo-confirmation_prompt    = space.
  cs_layo-key_hotspot            = space.
  cs_layo-flexible_key           = space.
  cs_layo-reprep                 = space.
  cs_layo-group_buttons          = space.
  cs_layo-no_keyfix              = space.
  cs_layo-get_selinfos           = space.
  cs_layo-group_change_edit      = space.
  cs_layo-no_scrolling           = space.
  cs_layo-expand_all             = space.
  cs_layo-no_author              = space.
*... Detailed screen
  cs_layo-detail_popup           = space.
  cs_layo-detail_initial_lines   = space.
  cs_layo-detail_titlebar        = space.
*... PF-status
  cs_layo-def_status             = space.
*... Display variants
  cs_layo-header_text            = space.
  cs_layo-item_text              = space.
  cs_layo-default_item           = space.
*... colour
  cs_layo-info_fieldname         = space.
  cs_layo-coltab_fieldname       = space.
*... others
  cs_layo-list_append            = space.
endform.                    " f01_set_layo

*&---------------------------------------------------------------------*
*&      Form  f01_set_fcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_set_fcat changing ct_fcat type slis_t_fieldcat_alv.
  call function 'REUSE_ALV_FIELDCATALOG_MERGE'
    exporting
      i_structure_name             = 'ALV_T_T2'
    changing
      ct_fieldcat                  = ct_fcat
    exceptions
      inconsistent_interface       = 1
      program_error                = 2
      others                       = 3.
  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.                    " f01_set_fcat

*&---------------------------------------------------------------------*
*&      Form  f01_set_color
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_set_color changing ct_fcat type slis_t_fieldcat_alv
                            cs_layo type slis_layout_alv.
  field-symbols: <ls_outtab> type any,
                 <ls_fcat>   type slis_fieldcat_alv,
                 <l_any>     type any,
                 <lt_table>  type standard table.
  data: l_row         type i,
        l_field       type lvc_fname,
        l_color_index type i,
        lt_fcat       type slis_t_fieldcat_alv,
        lt_color_lvc  type lvc_t_scol,
        ls_color_lvc  type lvc_s_scol,
        lt_color_slis type slis_t_specialcol_alv,
        ls_color_slis type slis_specialcol_alv.
*Set Row Color through info_fieldname
*对不同行对Color字段填写不同颜色值

  cs_layo-info_fieldname = 'COLOR'.

  loop at gs_test-color_rows into l_row.
    read table <gt_outtab> assigning <ls_outtab> index l_row.
    if sy-subrc eq 0.
      assign component 'COLOR' of structure <ls_outtab> to <l_any>.
      if sy-subrc eq 0.
        <l_any> = 'C610'.
      endif.
    endif.
  endloop.
  lt_fcat = ct_fcat.
  delete lt_fcat where tech ne space or no_out ne space.
*对于非关键字段可以通过emphasize设置颜色
  loop at gs_test-color_fields_column into l_field.
    read table lt_fcat assigning <ls_fcat>
               with key fieldname = l_field.
    if sy-subrc eq 0 and <ls_fcat>-key eq space.
      read table ct_fcat assigning <ls_fcat>
                 with key fieldname = <ls_fcat>-fieldname.
      if sy-subrc eq 0.
        <ls_fcat>-emphasize = 'C500'.
      endif.
    else.
*对于关键字段可以通过则通过cs_layo-coltab_fieldname = 'TABCOLOR'来设置颜色
      ls_color_slis-fieldname = <ls_fcat>-fieldname.
      ls_color_slis-color-col = cl_gui_resources=>list_col_positive.
      ls_color_slis-color-int = cl_gui_resources=>list_intensified.
      ls_color_slis-color-inv = 0.
      ls_color_slis-nokeycol  = con_true.
      append ls_color_slis to lt_color_slis.
      loop at <gt_outtab> assigning <ls_outtab>.
        assign component 'TABCOLOR' of structure <ls_outtab> to <l_any>.
        if sy-subrc eq 0.
           <l_any> = lt_color_slis.
        endif.
      endloop.
    endif.
  endloop.

  cs_layo-coltab_fieldname = 'TABCOLOR'.
*TABCOLOR表可以填写多条,也就可以同行控制多个单元的颜色。

  loop at <gt_outtab> assigning <ls_outtab>.
    add 1 to l_color_index.
    clear lt_color_slis.
    case l_color_index.
      when 1.
        ls_color_slis-color-col = cl_gui_resources=>list_col_background.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 2.
        ls_color_slis-color-col = cl_gui_resources=>list_col_background.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 3.
        ls_color_slis-color-col = cl_gui_resources=>list_col_heading.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 4.
        ls_color_slis-color-col = cl_gui_resources=>list_col_heading.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 5.
        ls_color_slis-color-col = cl_gui_resources=>list_col_normal.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 6.
        ls_color_slis-color-col = cl_gui_resources=>list_col_normal.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 7.
        ls_color_slis-color-col = cl_gui_resources=>list_col_total.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 8.
        ls_color_slis-color-col = cl_gui_resources=>list_col_total.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 9.
        ls_color_slis-color-col = cl_gui_resources=>list_col_key.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 10.
        ls_color_slis-color-col = cl_gui_resources=>list_col_key.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 11.
        ls_color_slis-color-col = cl_gui_resources=>list_col_positive.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 12.
        ls_color_slis-color-col = cl_gui_resources=>list_col_positive.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 13.
        ls_color_slis-color-col = cl_gui_resources=>list_col_negative.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 14.
        ls_color_slis-color-col = cl_gui_resources=>list_col_negative.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 15.
        ls_color_slis-color-col = cl_gui_resources=>list_col_group.
        ls_color_slis-color-int = 0.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
      when 16.
        ls_color_slis-color-col = cl_gui_resources=>list_col_group.
        ls_color_slis-color-int = cl_gui_resources=>list_intensified.
        ls_color_slis-color-inv = 0.
        ls_color_slis-nokeycol  = con_true.
        clear l_color_index.
    endcase.
*查找同行多少列需要设置颜色,则生成多条颜色纪录
    loop at gs_test-color_fields_cell into l_field.
      read table lt_fcat assigning <ls_fcat>
                 with key fieldname = l_field.
      if sy-subrc eq 0.
        ls_color_slis-fieldname = <ls_fcat>-fieldname.
        append ls_color_slis to lt_color_slis.
      endif.
    endloop.
*将颜色纪录添加到数据输出表的TABCOLOR字段
    assign component 'TABCOLOR' of structure <ls_outtab> to <lt_table>.
    if sy-subrc eq 0.
      append lines of lt_color_slis to <lt_table>.
    endif.
  endloop.
endform.                    " f01_set_color
*&---------------------------------------------------------------------*
*&      Form  f01_f4_fcode
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form f01_f4_fcode changing c_value type lvc_s_fcat-fieldname.

  data: lt_fcat type slis_t_fieldcat_alv,
        ls_fcat type slis_fieldcat_alv.
  data: lt_values type table of seahlpres,
        lt_fields type table of dfies,
        lt_return type table of ddshretval,
        ls_value  type seahlpres,
        ls_field  type dfies,
        ls_return type ddshretval.
  perform f01_set_fcat changing lt_fcat.
  clear ls_field.
  ls_field-fieldname = 'FIELDNAME'.
  ls_field-intlen    = 30.
  ls_field-leng      = 30.
  ls_field-outputlen = 30.
  ls_field-scrtext_s = ls_field-fieldname.
  ls_field-scrtext_m = ls_field-fieldname.
  ls_field-scrtext_l = ls_field-fieldname.
  ls_field-reptext   = ls_field-fieldname.
  append ls_field to lt_fields.
  loop at lt_fcat into ls_fcat where tech eq space.
    ls_value-string = ls_fcat-fieldname.
    append ls_value to lt_values.
  endloop.
  call function 'F4IF_INT_TABLE_VALUE_REQUEST'
   exporting
     retfield               = 'FIELDNAME'
     display                = space
   tables
     value_tab              = lt_values
     field_tab              = lt_fields
     return_tab             = lt_return
   exceptions
     parameter_error        = 1
     no_values_found        = 2
     others                 = 3.
  if sy-subrc <> 0 and sy-subrc ne 3.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
  read table lt_return into ls_return  with key fieldname = 'FIELDNAME'.
  if sy-subrc eq 0.
    c_value = ls_return-fieldval.
  endif.
endform.                    " f01_f4_fcode

 
<think>我们正在处理一个关于SAP ABAPREUSE_ALV_GRID_DISPLAY_LVC函数模块的事件处理问题,特别是数据变更完成事件(datachangefinished)。根据用户提供的引用和问题,我们可以推断用户希望了解如何在REUSE_ALV_GRID_DISPLAY_LVC中处理数据变更完成事件。 在SAP ABAP中,REUSE_ALV_GRID_DISPLAY_LVC是一个用于显示ALV报表的函数模块。它支持多种事件,其中数据变更完成事件(data_changed_finished)是当用户在ALV网格中修改数据并完成编辑(例如按下回车离开编辑的单元格)时触发的事件。 为了处理这个事件,我们需要以下步骤: 1. 在调用REUSE_ALV_GRID_DISPLAY_LVC之前,定义一个事件处理程序(event handler)方法来处理data_changed_finished事件。 2. 在函数模块的调用参数中,将事件处理程序方法注册到相应的事件上。 但是,需要注意的是,REUSE_ALV_GRID_DISPLAY_LVC函数模块内部使用了一个ALV对象(CL_GUI_ALV_GRID的实例)。因此,我们需要在函数调用之后获取这个ALV对象实例,然后才能注册事件。然而,REUSE_ALV_GRID_DISPLAY_LVC并没有直接返回ALV对象实例,所以我们需要使用另一种方法:在函数调用时通过I_CALLBACK_PROGRAM参数指定回调程序,然后使用SET HANDLER语句在程序中注册事件处理程序。 然而,更常见且推荐的做法是使用面向对象的ALV(即直接创建CL_GUI_ALV_GRID的实例),这样可以更灵活地控制事件。但用户要求使用REUSE_ALV_GRID_DISPLAY_LVC,因此我们需要在函数调用之后获取网格对象。 我们可以通过以下方式获取网格对象: - 在调用REUSE_ALV_GRID_DISPLAY_LVC之后,使用CL_GUI_ALV_GRID=>get_metadata( )方法获取网格实例?但是这种方法并不直接。 - 实际上,在调用REUSE_ALV_GRID_DISPLAY_LVC时,我们可以通过传递一个容器(container)来显示ALV,然后从容器中获取网格对象。但如果不使用容器,则函数模块会在屏幕上创建一个网格。 另一种方法是使用函数模块返回的网格对象。REUSE_ALV_GRID_DISPLAY_LVC有一个导出参数E_GRID,它返回网格对象。因此,我们可以使用这个参数来获取网格实例,然后注册事件。 具体步骤: 1. 定义事件处理程序方法,例如:handle_data_changed_finished,该方法需要定义在类中(局部类全局类)。 2. 在调用函数之前,创建一个事件处理程序实例(即类的实例)。 3. 调用REUSE_ALV_GRID_DISPLAY_LVC,并确保获取E_GRID参数(即网格对象)。 4. 使用SET HANDLER语句将事件处理程序方法注册到网格对象的事件上。 注意:事件data_changed_finished是在类CL_GUI_ALV_GRID中定义的,因此我们可以在网格对象上注册这个事件。 下面是一个示例代码框架: ```abap REPORT ztest_alv_event. * 定义局部类来处理事件 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid IMPORTING e_modified et_good_cells. ENDCLASS. CLASS lcl_event_handler IMPLEMENTATION. METHOD handle_data_changed_finished. " 在这里处理数据变更完成事件 " e_modified 表示数据是否被修改 " et_good_cells 包含成功修改的单元格信息 ENDMETHOD. ENDCLASS. DATA: go_grid TYPE REF TO cl_gui_alv_grid, go_event_handler TYPE REF TO lcl_event_handler. START-OF-SELECTION. " 准备数据内表等 " 创建事件处理程序实例 CREATE OBJECT go_event_handler. " 调用函数模块显示ALV CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid " 其他必要的参数,如结构、布局等 IMPORTING e_grid = go_grid " 获取网格对象 TABLES t_outtab = it_data " 你的数据内表 EXCEPTIONS program_error = 1 OTHERS = 2. IF sy-subrc = 0 AND go_grid IS BOUND. " 注册事件 SET HANDLER go_event_handler->handle_data_changed_finished FOR go_grid. ENDIF. ``` 但是,请注意:REUSE_ALV_GRID_DISPLAY_LVC函数模块在调用后,网格已经显示,并且事件注册必须在网格显示之前完成?实际上,事件注册可以在网格显示之后,因为事件是未来触发的。但是,为了确保在用户操作之前事件处理程序已经注册,我们通常在显示之前注册事件。然而,由于我们只能在函数调用后获得网格对象,所以只能在之后注册。 另外,在函数模块REUSE_ALV_GRID_DISPLAY_LVC中,有一个参数I_GRID_SETTINGS,其中有一个属性NO_EDIT(用于控制是否可编辑)。如果我们需要编辑,则不能设置NO_EDIT为'X'。同时,我们还需要在布局(layout)中设置字段为可编辑(在field catalog中设置EDIT属性)。 因此,为了支持编辑,我们需要: - 在field catalog中为需要编辑的字段设置EDIT属性为'X'。 - 在布局中设置STYLEFNAME属性(如果使用单元格级编辑)者直接允许整个字段编辑。 此外,我们还需要注册编辑事件(edit event),以便在用户开始编辑时触发,但这里我们只关注数据变更完成事件。 注意:在函数模块REUSE_ALV_GRID_DISPLAY_LVC中,我们可能还需要在调用之前设置一些其他参数,比如设置编辑模式(使用I_GRID_EDIT属性)?实际上,在SAP的较新版本中,可能需要使用I_GRID_SETTINGS-EDIT_MODE来设置编辑模式。 但是,在REUSE_ALV_GRID_DISPLAY_LVC中,编辑功能的启用还需要在布局中设置EDIT_MODE(例如,设置EDIT_MODE为'X')?实际上,在布局参数(IS_LAYOUT)中有一个EDIT属性,但它是整个网格的编辑模式。我们通常设置IS_LAYOUT-EDIT = 'X'。 总结步骤: 1. 定义字段目录(field catalog),为需要编辑的字段设置EDIT属性。 2. 设置布局(layout)的EDIT属性为'X'(允许编辑)。 3. 调用函数模块,获取网格对象。 4. 注册事件处理程序。 注意:事件data_changed_finished是在用户完成编辑后触发的,它提供了修改的单元格信息。 另外,用户提到的引用[2]中提到了注册编辑事件(CL_GUI_ALV_GRID=>MC_EVT_MODIFIED),这个事件是当数据被修改时触发的,与data_changed_finished不同。data_changed_finished是在用户完成编辑(例如,按回车离开单元格)时触发,而modified事件是在数据被修改(每次修改)时触发。 因此,根据用户的需求,我们提供了data_changed_finished事件的处理方法。 如果用户希望使用REUSE_ALV_GRID_DISPLAY_LVC,并且处理datachangefinished事件,那么上述代码示例可以作为一个起点。 但是,请注意,在SAP ABAP中,事件处理程序必须在一个类的方法中实现,因此我们使用了局部类。 最后,我们生成相关问题:</think>### SAP ABAP REUSE_ALV_GRID_DISPLAY_LVC 数据变更完成事件处理 在 SAP ABAP 中,`REUSE_ALV_GRID_DISPLAY_LVC` 函数模块用于显示 ALV 报表,处理数据变更完成事件(`DATA_CHANGED_FINISHED`)需要结合面向对象 ALV 的事件处理机制。以下是完整实现步骤: #### 1. **核心实现步骤** ```abap *-- 1. 定义事件处理类 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid IMPORTING e_modified et_good_cells. ENDCLASS. CLASS lcl_event_handler IMPLEMENTATION. METHOD handle_data_finished. " 数据变更完成后的处理逻辑 IF e_modified = abap_true. " 1. 获取修改的单元格数据 " 2. 执数据验证/更新数据库 " 3. 刷新ALV显示 ENDIF. ENDMETHOD. ENDCLASS. *-- 2. 在调用ALV前初始化 DATA: go_grid TYPE REF TO cl_gui_alv_grid, go_handler TYPE REF TO lcl_event_handler. START-OF-SELECTION. CREATE OBJECT go_handler. " 创建事件处理实例 *-- 3. 调用ALV函数获取网格对象 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = ls_layout " 设置布局EDIT模式 it_fieldcat_lvc = lt_fieldcat " 字段目录 IMPORTING e_grid = go_grid " 关键:获取网格对象 TABLES t_outtab = lt_data. " 显示的数据表 *-- 4. 注册数据变更完成事件 IF go_grid IS BOUND. " 启用编辑事件 CALL METHOD go_grid->set_ready_for_input EXPORTING i_ready_for_input = 1. " 注册数据完成事件 SET HANDLER go_handler->handle_data_finished FOR go_grid. ENDIF. ``` #### 2. **关键配置说明** 1. **布局设置**:在 `ls_layout` 中启用编辑模式 ```abap ls_layout-edit = 'X'. " 允许网格编辑 ``` 2. **字段目录设置**:为可编辑字段设置属性 ```abap lt_fieldcat-edit = 'X'. " 指定字段可编辑 ``` 3. **事件参数说明**: - `e_modified`:标识数据是否被修改(`ABAP_TRUE/ABAP_FALSE`) - `et_good_cells`:包含修改后数据的单元格集合(类型 `LVC_T_MODI`) #### 3. **事件处理中的典型操作 ```abap METHOD handle_data_finished. LOOP AT et_good_cells ASSIGNING FIELD-SYMBOL(<fs_modi>). " 获取修改的值 ASSIGN COMPONENT <fs_modi>-fieldname OF STRUCTURE lt_data[<fs_modi>-row_id] TO FIELD-SYMBOL(<fs_field>). " 数据验证示例 IF <fs_modi>-value < 0. MESSAGE '值不能为负数' TYPE 'E'. ELSE. <fs_field> = <fs_modi>-value. ENDIF. ENDLOOP. " 刷新ALV显示 go_grid->refresh_table_display( ). ENDMETHOD. ``` #### 4. **注意事项** 1. 必须在调用 `REUSE_ALV_GRID_DISPLAY_LVC` 后获取 `e_grid` 对象才能注册事件[^1] 2. 使用 `set_ready_for_input(1)` 激活编辑模式[^2] 3. 事件触发条件:用户按 Enter 离开编辑单元格 4. 如需实时校验,可额外注册 `data_changed` 事件 > **性能提示**:批量操作时建议在 `handle_data_finished` 中使用 `REFRESH_TABLE_DISPLAY` 而非重新调用 ALV 函数。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值