Funcation ALV 事件用法(编辑ALV ,直接保存修改数据到DB)

Posted HARU_ちゃん

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Funcation ALV 事件用法(编辑ALV ,直接保存修改数据到DB)相关的知识,希望对你有一定的参考价值。

*&---------------------------------------------------------------------*
*& REPORT  Y_ALV_OO_JAMES_4
*&
*&---------------------------------------------------------------------*
*& THIS REPORT SHOW YOU HOW TO USE FUNCATION ALV AND EVENT
*&---------------------------------------------------------------------*
REPORT Y_FCUNCA_ALV_DEMON.

TABLES:YCACS_CTRTBU.
*&---------------------------------------------------------------------*
*&  DATA DEFINE
*&---------------------------------------------------------------------*
DATA:
  CTRTBU_ID TYPE CACS_CTRTBU_ID.

DATA:
  LT_CACS_CTRTBU_TEMP TYPE TABLE OF YCACS_CTRTBU1,
  LT_CACS_CTRTBU      TYPE TABLE OF YCACS_CTRTBU1,
  LS_CACS_CTRTBU      TYPE YCACS_CTRTBU1,

* ALV DEFINIE
  GT_FIELDCAT         TYPE SLIS_T_FIELDCAT_ALV,
  GS_FIELDCAT         TYPE SLIS_FIELDCAT_ALV,
  GS_LAYOUT           TYPE SLIS_LAYOUT_ALV,

* EVENT用
  GT_EVENT            TYPE SLIS_T_EVENT,
  GS_EVENT            TYPE SLIS_ALV_EVENT,

* 当用户选择复选框时,立刻出发DATA_CHANGED 事件.
  GS_GLAY             TYPE LVC_S_GLAY.
*&---------------------------------------------------------------------*
*&  PARAMETERS DEFINITION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
SELECTION-SCREEN BEGIN OF BLOCK B01 WITH FRAME TITLE TEXT-B01.
PARAMETERS: P_CLIK AS CHECKBOX DEFAULT \'X\'.
PARAMETERS: P_EDIT AS CHECKBOX DEFAULT \'X\'.
SELECT-OPTIONS:S_COMM FOR CTRTBU_ID.
SELECTION-SCREEN END OF BLOCK B01.

*&---------------------------------------------------------------------*
*&  INITIALIZATION
*&---------------------------------------------------------------------*
INITIALIZATION.
  CLEAR:
    LT_CACS_CTRTBU_TEMP,
    LT_CACS_CTRTBU,
    LS_CACS_CTRTBU,
    GT_FIELDCAT,
    GS_LAYOUT,
    GT_EVENT,
    GS_EVENT.
*&---------------------------------------------------------------------*
*&  START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* GET DATA
  PERFORM GET_DATA.
  PERFORM SET_ALV_FIELDCAT.
  PERFORM SET_ALV_LAYOUT.
  PERFORM SET_ALV_EVENT.
  PERFORM SET_ALV_SHOW.
* 和SE80里自己设置的 GUI_STATUS 进行关联。
  FORM ALV_STATUS_SET USING PT_EXTAB TYPE SLIS_T_EXTAB.

   CLEAR: PT_EXTAB.
*   SET PF-STATUS \'ALV_STATUS\'.         " 这是我自己设置的GUI STATUS,具体方法SE80-->GUI STATUS -APPLICATION TOOLBAR
   SET PF-STATUS \'STANDARD_FULLSCREEN\'. " 这是我参照SAP 系统标准的GUI STATUS 进行设置的,功能比较齐全,具体方法,复制STANDARD_FULLSCREEN名
  ENDFORM.

* USER_COMMAND 事件
  FORM ALV_USER_COMMAND
    USING P_UCOMM    LIKE SY-UCOMM
          P_SELFIELD TYPE SLIS_SELFIELD.  "  可以获取某一个单元格内的值

    CASE P_UCOMM.
      WHEN \'CLOSE\'.
        LEAVE PROGRAM.
      WHEN \'&DATA_SAVE\'.
        MODIFY YCACS_CTRTBU FROM TABLE LT_CACS_CTRTBU.
        IF SY-SUBRC = 0.
          COMMIT WORK.
          MESSAGE \'SUCCESSS\' TYPE \'S\'.
        ELSE.
          MESSAGE \'FAIL\' TYPE \'E\'.
        ENDIF.
      WHEN OTHERS.
    ENDCASE.
  ENDFORM.

* ALV_DATA_CHANGED 事件
  FORM ALV_DATA_CHANGED USING PCL_DATA TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
    DATA:
      L_NAME(100),
      LS_CELLS TYPE LVC_S_MODI.

    FIELD-SYMBOLS <FS_VALUE>.

    LOOP AT PCL_DATA->MT_MOD_CELLS INTO LS_CELLS.


      CLEAR:LS_CACS_CTRTBU.
      READ TABLE LT_CACS_CTRTBU INTO LS_CACS_CTRTBU INDEX LS_CELLS-ROW_ID.

        CONCATENATE \'LT_CACS_CTRTBU-\'LS_CELLS-FIELDNAME INTO L_NAME.
        ASSIGN (L_NAME) TO <FS_VALUE>.
        <FS_VALUE> = LS_CELLS-VALUE.

       MODIFY LT_CACS_CTRTBU INDEX LS_CELLS-ROW_ID FROM LS_CACS_CTRTBU.

    ENDLOOP.

  ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  SET_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*       THIS PERFORM IS FOR GET ALL FIELDCAT REFERENCE CACS_CTRTBU
*----------------------------------------------------------------------*
*  -->  CACS_CTRTBU         REFERENCE
*  <--  GT_FIELDCAT
*----------------------------------------------------------------------*
FORM SET_ALV_FIELDCAT .
  CALL FUNCTION \'REUSE_ALV_FIELDCATALOG_MERGE\'
    EXPORTING
      I_PROGRAM_NAME               = \'Y_FCUNCA_ALV_DEMON\'
*     I_INTERNAL_TABNAME           =
      I_STRUCTURE_NAME             = \'CACS_CTRTBU\'
*     I_CLIENT_NEVER_DISPLAY       = \'X\'
*     I_INCLNAME                   =
*     I_BYPASSING_BUFFER           =
*     I_BUFFER_ACTIVE              =
    CHANGING
      CT_FIELDCAT                  = GT_FIELDCAT
    EXCEPTIONS
      INCONSISTENT_INTERFACE       = 1
      PROGRAM_ERROR                = 2
      OTHERS                       = 3
              .
  IF SY-SUBRC <> 0.
* IMPLEMENT SUITABLE ERROR HANDLING HERE
  ENDIF.

* ALV 显示的时候增加出2列CHECK BOX   DELETE和ADD 列
  GS_FIELDCAT-FIELDNAME      = \'DELETE_LINE\'."FIELDNAME
  GS_FIELDCAT-TABNAME        = \'YCACS_CTRTBU\'."TABNAME
  GS_FIELDCAT-CHECKBOX       = \'X\'.
*  GS_FIELDCAT-COLTEXT   = \'DELETE LINE\'##NO_TEXT .
  GS_FIELDCAT-SELTEXT_L      = \'DELETE_LINE\'.
  GS_FIELDCAT-SELTEXT_M      = \'DELETE_LINE\'.
  GS_FIELDCAT-SELTEXT_S      = \'DELETE_LINE\'.

  INSERT GS_FIELDCAT INTO TABLE GT_FIELDCAT.

  CLEAR GS_FIELDCAT.
  GS_FIELDCAT-FIELDNAME      = \'NEW_LINE\'."FIELDNAME
  GS_FIELDCAT-TABNAME        = \'YCACS_CTRTBU\'."TABNAME
  GS_FIELDCAT-CHECKBOX       = \'X\'.
  GS_FIELDCAT-SELTEXT_L      = \'NEW_LINE\'.
  GS_FIELDCAT-SELTEXT_M      = \'NEW_LINE\'.
  GS_FIELDCAT-SELTEXT_S      = \'NEW_LINE\'.

  INSERT GS_FIELDCAT INTO TABLE GT_FIELDCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  SET_ALV_LAYOUT
*&---------------------------------------------------------------------*
*       EDIT THE LAYOUT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  GS_LAYOUT        LAYOUT
*----------------------------------------------------------------------*
FORM SET_ALV_LAYOUT .
* SET LAYOUT
  GS_LAYOUT-ZEBRA             = ABAP_TRUE.
  GS_LAYOUT-COLWIDTH_OPTIMIZE = ABAP_TRUE.

* Set edit mode
  IF P_EDIT = ABAP_TRUE.
    GS_LAYOUT-EDIT = ABAP_TRUE.

    GS_GLAY-EDT_CLL_CB = ABAP_TRUE.     " 选中复选款,立刻触发data changed 事件
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  SET_ALV_GUI_STATUS
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
FORM SET_ALV_EVENT.
* SET GUI STATUS EVENT
  MOVE \'PF_STATUS_SET\'  TO GS_EVENT-NAME.   " ALV的事件名
  MOVE \'ALV_STATUS_SET\' TO GS_EVENT-FORM.   " ALV 程序内设置GUI STATUS的语句
  APPEND GS_EVENT TO GT_EVENT.

* SET ALV DATA_CHANGED EVENT
  MOVE \'DATA_CHANGED\'     TO GS_EVENT-NAME.
  MOVE \'ALV_DATA_CHANGED\' TO GS_EVENT-FORM.
  APPEND GS_EVENT TO GT_EVENT.

ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  SET_ALV_SHOW
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
FORM SET_ALV_SHOW .
  CALL FUNCTION \'REUSE_ALV_GRID_DISPLAY\'
    EXPORTING
*     I_INTERFACE_CHECK                 = \' \'
*     I_BYPASSING_BUFFER                = \' \'
*     I_BUFFER_ACTIVE                   = \' \'
      I_CALLBACK_PROGRAM                = \'Y_FCUNCA_ALV_DEMON\'
*     I_CALLBACK_PF_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                  = \'CACS_CTRTBU\'
*     I_BACKGROUND_ID                   = \' \'
*     I_GRID_TITLE                      =
*     I_GRID_SETTINGS                   = GS_GLAY   " 复选框触发data changed 事件
      IS_LAYOUT                         = GS_LAYOUT
      IT_FIELDCAT                       = GT_FIELDCAT
*     IS_SEL_HIDE                       =
*     I_DEFAULT                         = \'X\'
*     I_SAVE                            = \' \'
*     IS_VARIANT                        =
     IT_EVENTS                         = GT_EVENT
*     IT_EVENT_EXIT                     =
*     IS_PRINT                          =
*     IS_REPREP_ID                      =
    TABLES
      T_OUTTAB                          = LT_CACS_CTRTBU
   EXCEPTIONS
     PROGRAM_ERROR                     = 1
     OTHERS                            = 2
            .
  IF SY-SUBRC <> 0.
* IMPLEMENT SUITABLE ERROR HANDLING HERE
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  GET_DATA
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
FORM GET_DATA .
  IF P_CLIK = ABAP_TRUE.

    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_CACS_CTRTBU_TEMP
      FROM CACS_CTRTBU.

    IF LT_CACS_CTRTBU_TEMP IS NOT INITIAL.
      LT_CACS_CTRTBU = LT_CACS_CTRTBU_TEMP.
    ENDIF.
  ELSE.
    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_CACS_CTRTBU
      FROM CACS_CTRTBU
      WHERE CTRTBU_ID IN S_COMM.
  ENDIF.

  IF LT_CACS_CTRTBU IS INITIAL.
    MESSAGE \'SORRY, NOT ABLE TO FIND THE DATA\' TYPE \'E\'.
  ENDIF.

ENDFORM.

 

以上是关于Funcation ALV 事件用法(编辑ALV ,直接保存修改数据到DB)的主要内容,如果未能解决你的问题,请参考以下文章

编辑后不刷新 ALV。为啥?

原创:SAP LVC ALV编辑小技巧

八十八SAP中ALV事件之二,事件的定义和事件子例程

ABAP ALV例程用法

ABAP ALV例程用法

abap简单报表,alv显示,按钮事件