如果输入错误,如何禁用可编辑ALV网格?

时间:2018-07-31 12:52:10

标签: abap alv

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

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

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

enter image description here

此处,其他字段显示为灰色,并突出显示无效的条目。在用户解决此错误之前,他/她无法继续进行操作。

1 个答案:

答案 0 :(得分:1)

具有包含容器CCONTAINER1的屏幕100粘贴此代码段

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的概念,其中为表中的每一行分配了样式表。在那里,您可以在单元格级别上应用条件属性,包括可编辑性。

通过内置的检查表/域引发错误时,除错误单元格外,网格不可编辑,并且在更正错误时,可编辑性返回。