SAP: ALV GRID 追加核取方塊欄位及編輯時立刻呼叫事件

samrv發表於2024-06-04

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 "

  

相關文章