如果输入不正确,如何禁用可编辑的 ALV 网格?

How to disable the Editable ALV Grid in case of an incorrect entry?

每当在 ALV 网格中输入无效值时,如何禁用网格中的其他字段(灰色)并强制用户修复不正确的字段。

我尝试在 DATA_CHANGED 事件中添加协议。但协议列表仅在弹出窗口中显示错误。仍然可以进行编辑,并且没有禁用任何字段。

但是如何禁用其他字段。示例行为如下所示:

此处,其他字段显示为灰色,无效条目突出显示。在用户修复此错误之前,he/she 无法继续。

屏幕 100 和容器 CCONTAINER1 粘贴此代码段

CLASS zcl_alv_test DEFINITION.

  PUBLIC SECTION.
    METHODS: constructor, display_grid, populate_grid.

    DATA: lr_rtti_struc   TYPE REF TO cl_abap_structdescr,
          it_fldcat       TYPE lvc_t_fcat,
          grid_container1 TYPE REF TO cl_gui_custom_container,
          grid1           TYPE REF TO cl_gui_alv_grid.

    DATA: BEGIN OF gs_outtab.
            INCLUDE TYPE spfli.
    DATA: celltab TYPE lvc_t_styl.
    DATA: END OF gs_outtab.
    DATA gt_outtab1 LIKE TABLE OF gs_outtab INITIAL SIZE 0.

    METHODS:
      handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid IMPORTING er_data_changed,
      handle_data_changed_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid IMPORTING e_modified et_good_cells,
      create_dynamic_fcat.

  PRIVATE SECTION.
    TYPES: ty_tab LIKE LINE OF gt_outtab1.
    METHODS: refresh_grid, fill_celltab IMPORTING p_fieldname TYPE lvc_fname
                                        CHANGING  pt_celltab  TYPE lvc_t_styl.
ENDCLASS.

CLASS zcl_alv_test IMPLEMENTATION.
  METHOD constructor.
    CREATE OBJECT grid_container1 EXPORTING container_name = 'CCONTAINER1'.
    CREATE OBJECT grid1 EXPORTING i_parent = grid_container1.

    SET HANDLER handle_data_changed FOR grid1.
    SET HANDLER handle_data_changed_finished FOR grid1.

    CALL METHOD grid1->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_enter.
  ENDMETHOD. "constructor

  METHOD fill_celltab.
    LOOP AT pt_celltab ASSIGNING FIELD-SYMBOL(<fs_fcat>).
      IF <fs_fcat>-fieldname = p_fieldname.
        <fs_fcat>-style = cl_gui_alv_grid=>mc_style_enabled.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.

  METHOD handle_data_changed.
    DATA: lt_celltab TYPE lvc_t_styl.

    LOOP AT it_fldcat ASSIGNING FIELD-SYMBOL(<fs_fcat>).
      INSERT VALUE lvc_s_styl( style = cl_gui_alv_grid=>mc_style_disabled fieldname = <fs_fcat>-fieldname ) INTO TABLE lt_celltab.
    ENDLOOP.
    MODIFY gt_outtab1 FROM VALUE ty_tab( celltab = lt_celltab ) TRANSPORTING celltab WHERE connid IS NOT INITIAL.

    LOOP AT er_data_changed->mt_mod_cells ASSIGNING FIELD-SYMBOL(<mods>).
      READ TABLE gt_outtab1 ASSIGNING FIELD-SYMBOL(<sym>) INDEX <mods>-row_id.
      fill_celltab( EXPORTING p_fieldname = <mods>-fieldname
                                CHANGING pt_celltab = <sym>-celltab ).
      MODIFY gt_outtab1 FROM <sym> INDEX <mods>-row_id.
    ENDLOOP.
    refresh_grid( ).
  ENDMETHOD.

  METHOD handle_data_changed_finished.
    DATA: lt_celltab TYPE lvc_t_styl.

    LOOP AT it_fldcat ASSIGNING FIELD-SYMBOL(<fs_fcat>).
      IF sy-tabix MOD 2 = 0.
        DATA(style) = cl_gui_alv_grid=>mc_style_disabled.
      ELSE.
        style = cl_gui_alv_grid=>mc_style_enabled.
      ENDIF.
      INSERT VALUE lvc_s_styl( style = style fieldname = <fs_fcat>-fieldname ) INTO TABLE lt_celltab.
    ENDLOOP.

    MODIFY gt_outtab1 FROM VALUE ty_tab( celltab = lt_celltab ) TRANSPORTING celltab WHERE connid IS NOT INITIAL.
    refresh_grid( ).
  ENDMETHOD.

  METHOD create_dynamic_fcat.
    lr_rtti_struc ?= cl_abap_structdescr=>describe_by_name( 'SPFLI' ).
    DATA(comps) = lr_rtti_struc->components.

    LOOP AT comps ASSIGNING FIELD-SYMBOL(<comps>).
      IF sy-tabix MOD 2 = 0.
        DATA(edit) = abap_false.
      ELSE.
        edit = abap_true.
      ENDIF.
      APPEND VALUE lvc_s_fcat( edit = edit fieldname = <comps>-name datatype = <comps>-type_kind inttype = <comps>-type_kind intlen = <comps>-length decimals = <comps>-decimals coltext = <comps>-name lowercase = 'X' ) TO it_fldcat .
    ENDLOOP.
  ENDMETHOD. "create_dynamic_fcat

  METHOD populate_grid.
    SELECT * UP TO 1000 ROWS
    FROM spfli
    INTO CORRESPONDING FIELDS OF TABLE gt_outtab1.
  ENDMETHOD. "CHANGE_TITLE

  METHOD display_grid.
    create_dynamic_fcat( ).
    grid1->set_table_for_first_display(
      EXPORTING
        is_layout       = VALUE lvc_s_layo( zebra = abap_true stylefname = 'CELLTAB' )
      CHANGING
        it_outtab       = gt_outtab1
        it_fieldcatalog = it_fldcat ).
  ENDMETHOD. "display_grid

  METHOD refresh_grid.
    cl_gui_cfw=>flush( ).
    grid1->refresh_table_display( ).
  ENDMETHOD.
ENDCLASS.

MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MAIN100'.
  DATA: yalv TYPE REF TO zcl_alv_test.
  CREATE OBJECT yalv.
  yalv->populate_grid( ).
  yalv->display_grid( ).
ENDMODULE.

MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'RETURN'.
      LEAVE PROGRAM.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.

START-OF-SELECTION.
  CALL SCREEN 100.

它使用了 cell styles 的概念,其中 table 中的每一行都为该行中的每一列分配了 table 样式。您可以在单元格级别应用条件属性,包括可编辑性。

当通过内置 checktables/domains 抛出错误时,除了错误的单元格外,网格不是 editable,并且在更正错误时,可编辑性 returns.