二维码

[fmalv] 设置ALV单元格的编辑状态

Twilight发表于 2014-08-28 10:47Twilight 最后回复于 2014-08-28 10:47 [复制链接] 6041 0

在使用ALV显示列表的过程中,我们可以使用IT_FIELDCAT参数设置某一个字段的可编辑状态。
具体单元格可编辑状态设置的主要思想:首先通过EIDT参数设置列为可编辑状态;其次对输出内表进行循环将不需要编辑的行设置为不可编辑状态,如此单元格的可编辑属性设置完毕。

整个alv界面的编辑状态可通过下面2个参数控制:
  • CL_GUI_ALV_GRID=>MC_STYLE_DISABLED  不可编辑
  • CL_GUI_ALV_GRID=>MC_STYLE_ENABLED 可编辑

程序代码:
  1. TYPE-POOLS: SLIS.
  2. *- Fieldcatalog
  3. DATA: IT_FIELDCAT TYPE LVC_T_FCAT.
  4. DATA: X_FIELDCAT TYPE LVC_S_FCAT.
  5. DATA: X_LAYOUT TYPE LVC_S_LAYO.

  6. "第1步:用操作具体单元的是否可编辑的内表和工作区

  7. DATA: LS_EDIT TYPE LVC_S_STYL,
  8.       LT_EDIT TYPE LVC_T_STYL,
  9.     gr_alvgrid TYPE REF TO cl_gui_alv_grid .
  10. "第2步:在内表定义添加字段,用于控制具体行的具体单元是否可编辑
  11. DATA: BEGIN OF IT_VBAP OCCURS 0,
  12.   VBELN LIKE VBAP-VBELN,
  13.   POSNR LIKE VBAP-POSNR,
  14.   STYLE TYPE LVC_T_STYL, "FOR DISABLE
  15. END OF IT_VBAP.
  16. DATA: LS_OUTTAB LIKE LINE OF IT_VBAP.
  17. SELECT VBELN  POSNR
  18.   UP TO 100 ROWS
  19.   INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
  20. FROM VBAP.

  21. DATA:L_POS TYPE I VALUE 1.
  22. CLEAR: L_POS.
  23. L_POS = L_POS + 1.
  24. X_FIELDCAT-SELTEXT = 'VBELN'.
  25. X_FIELDCAT-FIELDNAME = 'VBELN'.
  26. X_FIELDCAT-TABNAME = 'ITAB'.
  27. X_FIELDCAT-COL_POS = L_POS.
  28. X_FIELDCAT-EDIT = 'X'.
  29. X_FIELDCAT-OUTPUTLEN = '10'.
  30. x_fieldcat-ref_field = 'VBELN'.
  31. x_fieldcat-ref_table = 'VBAK'.
  32. APPEND X_FIELDCAT TO IT_FIELDCAT.

  33. CLEAR X_FIELDCAT.
  34. L_POS = L_POS + 1.
  35. X_FIELDCAT-SELTEXT = 'POSNR'.
  36. X_FIELDCAT-FIELDNAME = 'POSNR'.
  37. X_FIELDCAT-TABNAME = 'ITAB'.
  38. X_FIELDCAT-COL_POS = L_POS.
  39. X_FIELDCAT-EDIT = 'X'.
  40. X_FIELDCAT-OUTPUTLEN = '5'.
  41. APPEND X_FIELDCAT TO IT_FIELDCAT.
  42. CLEAR X_FIELDCAT.
  43. L_POS = L_POS + 1.

  44. "第3步:设置第六行两个单元都不能输入
  45. SY-TABIX = 6.
  46. LS_EDIT-FIELDNAME = 'VBELN'.
  47. LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
  48. LS_EDIT-STYLE2 = SPACE.
  49. LS_EDIT-STYLE3 = SPACE.
  50. LS_EDIT-STYLE4 = SPACE.
  51. LS_EDIT-MAXLEN = 10.
  52. INSERT LS_EDIT INTO TABLE LT_EDIT.

  53. LS_EDIT-FIELDNAME = 'POSNR'.
  54. LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
  55. LS_EDIT-STYLE2 = SPACE.
  56. LS_EDIT-STYLE3 = SPACE.
  57. LS_EDIT-STYLE4 = SPACE.
  58. LS_EDIT-MAXLEN = 6.
  59. INSERT LS_EDIT INTO TABLE LT_EDIT.

  60. INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.
  61. "第4步:将控制数据写到内表
  62. MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE .

  63. "设置第10行只有项目不能输入
  64. clear LS_OUTTAB.
  65. refresh LT_EDIT.
  66. LS_EDIT-FIELDNAME = 'POSNR'.
  67. LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
  68. LS_EDIT-STYLE2 = SPACE.
  69. LS_EDIT-STYLE3 = SPACE.
  70. LS_EDIT-STYLE4 = SPACE.
  71. LS_EDIT-MAXLEN = 6.
  72. INSERT LS_EDIT INTO TABLE LT_EDIT.

  73. INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.

  74. SY-TABIX = 10.
  75. "将控制数据写到内表
  76. MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE .

  77. "第5步:设置控制字段
  78. X_LAYOUT-STYLEFNAME = 'STYLE'.

  79. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  80.   EXPORTING
  81.     I_CALLBACK_PROGRAM = SY-REPID
  82.     IS_LAYOUT_LVC      = X_LAYOUT
  83.     IT_FIELDCAT_LVC    = IT_FIELDCAT
  84.   TABLES
  85.     T_OUTTAB           = IT_VBAP[]
  86.   EXCEPTIONS
  87.     PROGRAM_ERROR      = 1
  88.     OTHERS             = 2.
  89. IF SY-SUBRC NE 0.
  90.   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  91.   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  92. ENDIF.
复制代码

程序执行效果:
alv 单元格编辑.png
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表