SAP: ALV GRID 追加复选框字段及编辑时立刻调用事件

10、GRID界面中选择复选框时,提示以下信息:

运行时错误:MOVE_TO_LIT_NOTALLOWED_NODATA

短文本:Assignment error: Overwriting of a protected field.

错误分析: Field “<FS_VALUE>” was to assigned a new value but this field is at least party  protected against changes.

错误原因:

218行的拼写有误:

CONCATENATE ‘GT_ITABL-’ ls_cells-fieldname into l_name .   GT_ITAB 误拼写成GT_ITABL.

 

 

*&---------------------------------------------------------------------*
*& Report  Z16_11
*& 利用 REUSE_ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
*& 添加"结束"按钮,
*&  1、关闭按钮的关闭程序事件
*& 2、 从函数组 SLVC_FULLSCREEN里复制 GUI状态下的STANDARD_FULLSCREEN .
*& 3、it_fieldcat 参数和字段目录说明
*& 10、报表 Z16_10-利用REUSE_ALV_GRID_DISPLAY(Edit模式及保存已修改数据的方法)
*& 11、
*&---------------------------------------------------------------------*

REPORT Z16_11.

TYPE-POOLS: SLIS.
DATA: GT_EVENTS TYPE SLIS_T_EVENT.
DATA: GS_EVENT TYPE SLIS_ALV_EVENT. " ALV 事件

DATA: gs_fieldcat type slis_fieldcat_alv,  " 定义字段目录结构体
      gt_fieldcat type slis_t_fieldcat_alv.   "定义字段目录内表

DATA gs_layout TYPE SLIS_LAYOUT_ALV.  " 定义布局

DATA: gs_glay type lvc_s_glay.

*& DATA gt_itab TYPE TABLE OF sflight with header line.

TYPES: BEGIN OF t_line.
        INCLUDE STRUCTURE sflight.
TYPES: CHECKBOX TYPE  C.  " 定义复选框 "
TYPES: END OF t_line. " 定义结构


*& 定义旧式 内表
DATA: BEGIN OF gt_itab2 OCCURS 0,
  carrid LIKE sflight-carrid,
  connid like sflight-connid,
  fldate like sflight-fldate,
  price like sflight-price,
  END OF gt_itab2.

*& 定义参照结构体类型的内表。
DATA: gt_itab type TABLE OF t_line WITH HEADER LINE.

SELECT * FROM SFLIGHT
   INTO CORRESPONDING FIELDS OF table GT_ITAB UP TO 25 ROWS.

MOVE 'PF_STATUS_SET'  TO GS_EVENT-NAME.
MOVE 'ALV_STATUS_SET' TO GS_EVENT-FORM.
APPEND GS_EVENT TO GT_EVENTS.

*& 为了当变更ALV数据时执行事件 ,将修改事件追加到事件表中
MOVE 'DATA_CHANGED' TO GS_EVENT-NAME.
MOVE 'ALV_DATA_CHANGED' TO GS_EVENT-FORM.
APPEND GS_EVENT TO GT_EVENTS.

CLEAR : gs_fieldcat, gt_fieldcat.
gs_fieldcat-col_pos = 1.
gs_fieldcat-fieldname = 'CHECKBOX'.
gs_fieldcat-checkbox = 'X'.
gs_fieldcat-edit = 'X'.
gs_fieldcat-seltext_m =  'Sel'.
APPEND gs_fieldcat to gt_fieldcat.


*& 字段目录中追加 CARRDI, CONNID这两个字段。
CLEAR : gs_fieldcat.
gs_fieldcat-col_pos = 2.  " 字段的输出顺序
gs_fieldcat-key = 'X'.  "主键 标识
gs_fieldcat-fieldname = 'CARRID'.  "数据库中死缓的字段名称"
gs_fieldcat-seltext_m = 'Airline Code'.  "中字段标签(20字节)"
APPEND gs_fieldcat TO gt_fieldcat.

CLEAR: gs_fieldcat .
gs_fieldcat-col_pos = 3.
* gs_fieldcat-edit = 'X'.  "  可编辑模式
gs_fieldcat-fieldname = 'CONNID'.
gs_fieldcat-seltext_m = 'Connection Number'.
APPEND gs_fieldcat to gt_fieldcat.

CLEAR: gs_fieldcat.
gs_fieldcat-col_pos = 4 .
gs_fieldcat-fieldname = 'FLDATE'.
gs_fieldcat-seltext_m = 'Flight Date'. " 起飞日期
gs_fieldcat-edit_mask = '____/__/__'.  " 显示 格式 "
APPEND gs_fieldcat to gt_fieldcat.

CLEAR: gs_fieldcat.
gs_fieldcat-col_pos = 5.
gs_fieldcat-fieldname = 'PRICE'.
gs_fieldcat-seltext_m = 'Airfare'.
APPEND gs_fieldcat to gt_fieldcat.

*& #region: 设置 布局
gs_layout-zebra = 'X'. "条纹
gs_layout-confirmation_prompt = 'X'.  " 在ALV布局变量中设置结束与否的确认选项。
gs_layout-edit = 'X'. " 设置AVL 可编辑 。

gs_glay-edt_cll_cb = 'X'.
*& #endregion: 设置 布局





**& #REGION:调用函数 REUSE_ALV_GRID_DISPLAY
**& 调用函数 REUSE_ALV_GRID_DISPLAY   ,比较 例子 Z16_01
**&  的 REUSE_ALV_LIST_DISPLAY 函数。
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
   I_CALLBACK_PROGRAM                = sy-cprog  "程序名称 ,
*   I_CALLBACK_PF_STATUS_SET          = ' '  " 'alv_status_set'
   I_CALLBACK_USER_COMMAND           = 'ALV_USER_COMMAND'  " 按钮事件命令
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  = 'SFLIGHT'
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
   I_GRID_SETTINGS                   = gs_glay
   IS_LAYOUT                         = gs_layout  " 设置布局样式
   IT_FIELDCAT                       = gt_fieldcat
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
   IT_EVENTS                         = gt_events
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
  TABLES
    T_OUTTAB                          = gt_itab
* EXCEPTIONS
*   PROGRAM_ERROR                     = 1
*   OTHERS                            = 2
          .
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
**& #ENDREGION:调用函数 REUSE_ALV_GRID_DISPLAY



*&-------------------------------------------------
*&  Form ALV_USER_COMMAND
*&-------------------------------------------------
*& ALV  "关闭" 按钮的点击事件
*&------------------------------------------------
FORM ALV_USER_COMMAND USING p_ucomm LIKE SY-ucomm
       p_selfield TYPE slis_selfield.

  "BREAK-POINT. " 单击按钮时传递的命令追加设断点的语句。
  CASE p_ucomm.
    WHEN 'CLOSE'.  " 当点击关闭时,退出程序 。
      LEAVE PROGRAM.  "离开程序(退出程序)
    WHEN '&IC1'.
      READ TABLE gt_itab INDEX p_selfield-tabindex.
      gt_itab-checkbox = 'X'.
      MODIFY gt_itab INDEX P_SELFIELD-tabindex.

    WHEN 'SAVE'.
      MODIFY sflight FROM TABLE gt_itab.  " 内表数据保存到数据库表
      IF sy-subrc EQ 0.
        commit work.  " 保存 数据到数据库表
      else.
        ROLLBACK WORK.
      ENDIF.
    WHEN OTHERS.
  ENDCASE. "  CASE条件结束。

ENDFORM.  "  ALV_USER_COMMAND

*&-------------------------------------------------
*&  Form alv_status_set
*&-------------------------------------------------
*& ALV 自定义工具栏
*&------------------------------------------------
FORM ALV_STATUS_SET USING PT_EXTAB TYPE SLIS_T_EXTAB.
  CLEAR: PT_EXTAB[].
  " 注释在画面上追加'ALV_STATUS'的语句,追加标准ALV_STATUS语句。
  "SET PF-STATUS 'ALV_STATUS'.
  SET PF-STATUS 'STANDARD_FULLSCREEN'. " 追加标准工具栏"
ENDFORM. "ALV_STATUS_SET


FORM ALV_DATA_CHANGED USING PCL_DATA TYPE REF TO
      CL_ALV_CHANGED_DATA_PROTOCOL.
  DATA: l_name(20),
        ls_cells TYPE lvc_s_modi
        .

  field-symbols <fs_value>.
  LOOP AT pcl_data->mt_mod_cells into ls_cells.
    CLEAR gt_itab.
    READ TABLE gt_itab index ls_cells-row_id.

    CONCATENATE 'GT_ITAB-' ls_cells-fieldname into l_name.
    ASSIGN (L_NAME) TO <fs_value>.
    <fs_value> = ls_cells-value.
    MODIFY gt_itab index ls_cells-row_id.

  ENDLOOP.
ENDFORM. "ALV_DATA_CHANGED "

  

 

posted @ 2024-06-04 07:57  samrv  阅读(84)  评论(0编辑  收藏  举报