sap abap alv 实现某行,单元格可编辑与不可编辑 REUSE_ALV_GRID_DISPLAY_LVC

在使用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.






你可能感兴趣的:(sap_abap,lvc_alv,SAP_ABAP)