1. 程式人生 > >【轉載】SAP ABAP ALV報表控制編輯行,編輯單元格

【轉載】SAP ABAP ALV報表控制編輯行,編輯單元格

重點備註:1.在輸出內表中增加欄位FIELD_STYLE TYPE LVC_T_STYL

                    2.設定STYLE_FNAME = 'FIELD_STYLE'.  如果不進行此步驟操作,這對單元格的屬性控制不會生效

 

在使用ALV顯示列表的過程中,我們可以使用IT_FIELDCAT引數設定某一個欄位的可編輯狀態。但是,要設定具體的單元格的可編輯狀態對於對ALV不是很瞭解的人來說是一個頭大的問題。
      具體單元格可編輯狀態設定的主要思想:首先通過EIDT引數設定列為可編輯狀態;其次對輸出內表進行迴圈將不需要編輯的行設定為不可編輯狀態,如此單元格的可編輯屬性設定完畢。下面貼上簡要程式碼。

部分程式碼:
DATA: BEGIN OF ITAB OCCURS 0,
      ZQRFH_ICON TYPE STRING,
      ZLDATE TYPE ZLDATE,
      ZLUSR TYPE ZLUSR,
      K TYPE STRING,
      FIELD_STYLE TYPE LVC_T_STYL, " 為內表新增設定編輯狀態所需的欄位  
      END OF ITAB.

S_FIELDCAT-FIELDNAME = 'ZBQFS'. " 設定列可編輯
S_FIELDCAT-EDIT = 'X'.
APPEND S_FIELDCAT TO T_FIELDCAT.

DATA STYLELIN TYPE LVC_S_STYL.

LOOP AT ITAB.
    IF ITAB-ZXMDM = 'D' OR ITAB-ZXMDM = 'F' OR ITAB-ZXMDM = 'H'.
      STYLELIN-FIELDNAME = 'ZBQFS'. " 需要編輯的列名
      STYLELIN-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED. " 設定為不可編輯狀態
      APPEND STYLELIN TO ITAB-FIELD_STYLE.
      CLEAR STYLELIN.
      MODIFY ITAB.
    ENDIF.
endloop.

X_LAYOUT-STYLE_FNAME = 'FIELD_STYLE'. " 將內表中的欄位名存入顯示格式

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'"呼叫函式
    EXPORTING
     IT_FIELDCAT_LVC    = T_FIELDCAT
      IS_LAYOUT_LVC      = X_LAYOUT
    TABLES
      T_OUTTAB           = ITAB_LB
    EXCEPTIONS
      PROGRAM_ERROR      = 1
      OTHERS             = 2.

控制單格的比較麻煩呢.要用OO了寫法了.

給個OO的例子吧.
REPORT ZALV_EDIT.
TYPE-POOLS: SLIS.
*- Fieldcatalog
DATA: IT_FIELDCAT TYPE LVC_T_FCAT.
DATA: X_FIELDCAT TYPE LVC_S_FCAT.
DATA: X_LAYOUT TYPE LVC_S_LAYO.

"第1步:用操作具體單元的是否可編輯的內表和工作區

DATA: LS_EDIT TYPE LVC_S_STYL,
      LT_EDIT TYPE LVC_T_STYL.
"第2步:在內表定義新增欄位,用於控制具體行的具體單元是否可編輯
DATA: BEGIN OF IT_VBAP OCCURS 0,
  VBELN LIKE VBAP-VBELN,
  POSNR LIKE VBAP-POSNR,
  STYLE TYPE LVC_T_STYL, "FOR DISABLE
END OF IT_VBAP.
DATA: LS_OUTTAB LIKE LINE OF IT_VBAP.
SELECT VBELN  POSNR
  UP TO 100 ROWS
  INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
FROM VBAP.


DATA:L_POS TYPE I VALUE 1.
CLEAR: L_POS.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT = 'VBELN'.
X_FIELDCAT-FIELDNAME = 'VBELN'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS = L_POS.
X_FIELDCAT-EDIT = 'X'.
X_FIELDCAT-OUTPUTLEN = '10'.
x_fieldcat-ref_field = 'VBELN'.
x_fieldcat-ref_table = 'VBAK'.
APPEND X_FIELDCAT TO IT_FIELDCAT.


CLEAR X_FIELDCAT.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT = 'POSNR'.
X_FIELDCAT-FIELDNAME = 'POSNR'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS = L_POS.
X_FIELDCAT-EDIT = 'X'.
X_FIELDCAT-OUTPUTLEN = '5'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.


"第3步:設定第六行兩個單元都不能輸入
SY-TABIX = 6.
LS_EDIT-FIELDNAME = 'VBELN'.
LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
LS_EDIT-STYLE2 = SPACE.
LS_EDIT-STYLE3 = SPACE.
LS_EDIT-STYLE4 = SPACE.
LS_EDIT-MAXLEN = 10.
INSERT LS_EDIT INTO TABLE LT_EDIT.


LS_EDIT-FIELDNAME = 'POSNR'.
LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
LS_EDIT-STYLE2 = SPACE.
LS_EDIT-STYLE3 = SPACE.
LS_EDIT-STYLE4 = SPACE.
LS_EDIT-MAXLEN = 6.
INSERT LS_EDIT INTO TABLE LT_EDIT.


INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.


"第4步:將控制資料寫到內表


MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE .






"設定第10行只有專案不能輸入


clear LS_OUTTAB.
refresh LT_EDIT.
LS_EDIT-FIELDNAME = 'POSNR'.
LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
LS_EDIT-STYLE2 = SPACE.
LS_EDIT-STYLE3 = SPACE.
LS_EDIT-STYLE4 = SPACE.
LS_EDIT-MAXLEN = 6.
INSERT LS_EDIT INTO TABLE LT_EDIT.


INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.


SY-TABIX = 10.




"將控制資料寫到內表


MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE .


"第5步:設定控制欄位
X_LAYOUT-STYLEFNAME = 'STYLE'.


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    I_CALLBACK_PROGRAM = SY-REPID
    IS_LAYOUT_LVC      = X_LAYOUT
    IT_FIELDCAT_LVC    = IT_FIELDCAT
  TABLES
    T_OUTTAB           = IT_VBAP[]
  EXCEPTIONS
    PROGRAM_ERROR      = 1
    OTHERS             = 2.
IF SY-SUBRC NE 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.