ALV代码模版(艾米版)

2021-10-23 16:11发布


          点击此处--->   EasySAP.com 群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)

REPORT ZZXXXX_ALV.


INCLUDE ZZXXXX_ALV_TOP."ALV声明定义"
INCLUDE ZZXXXX_ALV_C01."ALV 事件类"
INCLUDE ZZXXXX_ALV_F01."自开发逻辑"
INCLUDE ZZXXXX_ALV_F02."ALV 复用逻辑集合
INCLUDE ZZXXXX_ALV_PBO.
INCLUDE ZZXXXX_ALV_PAI.
INCLUDE ZZXXXX_ALV_MAIN. "事件块


1.INCLUDE : ZZXXXX_ALV_TOP

代码涉及各种定义, 选择屏幕,宏,常量

 

"---声明结构---
TABLES BKPF.

"---创建选择屏幕---
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-T01.

SELECT-OPTIONS S_BELNR   FOR  BKPF-BELNR NO-EXTENSION NO INTERVALS OBLIGATORY MEMORY ID BLN.
SELECT-OPTIONS S_GJAHR   FOR  BKPF-GJAHR NO-EXTENSION NO INTERVALS OBLIGATORY MEMORY ID BLN.
SELECT-OPTIONS S_BUKRS   FOR  BKPF-BUKRS NO-EXTENSION NO INTERVALS OBLIGATORY MEMORY ID BLN.

SELECTION-SCREEN END OF BLOCK B1.

"---定义---

TYPES BEGIN OF TY_DATA.
        INCLUDE TYPE BKPF.
TYPES END OF TY_DATA.


TYPES BEGIN OF TY_ALV.
        INCLUDE TYPE TY_DATA.
TYPES:   FSTYLE    TYPE LVC_T_STYL,
         CELLCOL   TYPE LVC_T_SCOL,
         COLOR     TYPE CHAR4,
         LIGHTS    TYPE CHAR4,
         MSG       TYPE CHAR255,
         SEL,
         CHK.
TYPES  END OF TY_ALV.


DATA GT_DATA  TYPE TABLE OF TY_DATA.
DATA GT_ALV   TYPE TABLE OF TY_ALV.


"请注意博客不支持'<>'   自行加上
FIELD-SYMBOLS FS_DATA TYPE TY_DATA.
FIELD-SYMBOLS FS_ALV  TYPE TY_ALV. 
"---Constants---
CONSTANTS CNS_A TYPE CHAR1 VALUE 'A'.
CONSTANTS CNS_B TYPE CHAR1 VALUE 'B'.
CONSTANTS CNS_C TYPE CHAR1 VALUE 'C'.
CONSTANTS CNS_D TYPE CHAR1 VALUE 'D'.
CONSTANTS CNS_E TYPE CHAR1 VALUE 'E'.
CONSTANTS CNS_H TYPE CHAR1 VALUE 'H'.
CONSTANTS CNS_I TYPE CHAR1 VALUE 'I'.
CONSTANTS CNS_P TYPE CHAR1 VALUE 'P'.
CONSTANTS CNS_J TYPE CHAR1 VALUE 'J'.
CONSTANTS CNS_S TYPE CHAR1 VALUE 'S'.
CONSTANTS CNS_U TYPE CHAR1 VALUE 'U'.
CONSTANTS CNS_N TYPE CHAR1 VALUE 'N'.
CONSTANTS CNS_X TYPE CHAR1 VALUE 'X'.

"---ALV 相关定义---
DATA GS_LAYOUT       TYPE LVC_S_LAYO.
DATA GS_FIELD        TYPE LVC_S_FCAT.
DATA GS_SORT_LVC     TYPE LVC_S_SORT .
DATA GS_FIELDCAT     TYPE LVC_S_FCAT.
DATA GS_STABLE       TYPE LVC_S_STBL.
*GS_STABLE-ROW = CNS_X.
*GS_STABLE-COL = CNS_X.

DATA GT_FIELD        TYPE LVC_T_FCAT.
DATA GT_SORT_LVC     TYPE LVC_T_SORT .
DATA GT_EVENTS       TYPE SLIS_T_EVENT.
DATA GT_FIELDCAT_LVC TYPE LVC_T_FCAT.

"宏定义

DEFINE CHECK_TABLE.

  IF &1[] IS INITIAL.

    MESSAGE TEXT-E01 TYPE 'S' DISPLAY LIKE 'E'."TEXT-E01:所选数据不存在!
    LEAVE LIST-PROCESSING.

  ENDIF.

END-OF-DEFINITION.

DEFINE CLEAR_TABLE.
  CLEAR &1.
  CLEAR &1[].
END-OF-DEFINITION.

DEFINE %.

  PERFORM USING &1 &2.

END-OF-DEFINITION.

FIELD-SYMBOLS  TYPE ANY.

DEFINE HOTS.

  CLEAR L_FIELDNAME.
  CONCATENATE 'US_ALV-' &1 INTO L_FIELDNAME.

  ASSIGN (L_FIELDNAMETO .

  IF SY-SUBRC 0.

    IF  NE '0' OR  IS NOT INITIAL.

      LV_STYLEROW-FIELDNAME &1.
      LV_STYLEROW-STYLE     CL_GUI_ALV_GRID=>MC_STYLE_HOTSPOT.

      INSERT LV_STYLEROW INTO TABLE US_ALV-FSTYLE.

    ENDIF.

  ENDIF.

END-OF-DEFINITION.

DEFINE ENABLED.

  CLEAR L_FIELDNAME.
  CONCATENATE 'US_ALV-' &1 INTO L_FIELDNAME.

  ASSIGN (L_FIELDNAMETO .

  IF SY-SUBRC 0.

    IF  NE '0' OR  IS NOT INITIAL.

      READ TABLE US_ALV-FSTYLE TRANSPORTING NO FIELDS WITH  KEY STYLE  CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

      IF SY-SUBRC 0.

        DELETE US_ALV-FSTYLE WHERE STYLE  CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

      ENDIF.

      LV_STYLEROW-FIELDNAME &1.
      LV_STYLEROW-STYLE     CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.

      INSERT LV_STYLEROW INTO TABLE US_ALV-FSTYLE.

    ENDIF.

  ENDIF.

END-OF-DEFINITION.

DEFINE DISABLED.

  CLEAR L_FIELDNAME.
  CONCATENATE 'US_ALV-' &1 INTO L_FIELDNAME.

  ASSIGN (L_FIELDNAMETO .

  IF SY-SUBRC 0.

    IF  NE '0' OR  IS NOT INITIAL.

      READ TABLE US_ALV-FSTYLE TRANSPORTING NO FIELDS WITH  KEY STYLE  CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.

      IF SY-SUBRC 0.

        DELETE US_ALV-FSTYLE WHERE STYLE  CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.

      ENDIF.

      LV_STYLEROW-FIELDNAME &1.
      LV_STYLEROW-STYLE     CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

      INSERT LV_STYLEROW INTO TABLE US_ALV-FSTYLE.

    ENDIF.

  ENDIF.

END-OF-DEFINITION.

DEFINE BUTTON.

  CLEAR L_FIELDNAME.
  CONCATENATE 'US_ALV-' &1 INTO L_FIELDNAME.

  ASSIGN (L_FIELDNAMETO .

  IF SY-SUBRC 0.

    READ TABLE US_ALV-FSTYLE TRANSPORTING NO FIELDS WITH  KEY FIELDNAME =  &1
                                                             STYLE CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.

    IF SY-SUBRC 0.

      DELETE US_ALV-FSTYLE WHERE FIELDNAME &1 AND  STYLE CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.

      CLEAR .

    ENDIF.

    LV_STYLEROW-FIELDNAME &1.
    LV_STYLEROW-STYLE     CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.

    INSERT LV_STYLEROW INTO TABLE US_ALV-FSTYLE.

     &2.

  ENDIF.

END-OF-DEFINITION.

DEFINE SET_COLOR.
  LS_CELLCOLOR-FNAME &1.
  LS_CELLCOLOR-COLOR-COL &2." '6'.
  LS_CELLCOLOR-COLOR-INT &3."'0'.
  LS_CELLCOLOR-COLOR-INV &4." '1'.
  INSERT LS_CELLCOLOR INTO TABLE &5.
END-OF-DEFINITION.

DEFINE ADD_FIELD.

  CLEAR GS_FIELD.
  GS_FIELD-FIELDNAME   &1.
  GS_FIELD-SCRTEXT_M   &2.
  GS_FIELD-EDIT        &3.
  GS_FIELD-OUTPUTLEN   &4.
  GS_FIELD-CHECKBOX    &5.
  GS_FIELD-COL_OPT     &6.

  IF  GS_FIELD-FIELDNAME 'BUZEI'.

    GS_FIELD-NO_ZERO = CNS_X

  ENDIF.

  APPEND GS_FIELD TO GT_FIELD.

END-OF-DEFINITION. 


2. INCLUDE : ZZXXXX_ALV_C01

ALV 各种事件操作 本地类

*&---------------------------------------------------------------------*
*&  包含                ZZXXXX_ALV_C01
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       CLASS LCL_EVENTS DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENTS DEFINITION.

  PUBLIC SECTION.

    TYPES TY_T_INS TYPE TABLE OF REF TO  CL_GUI_ALV_GRID.

    DATA PS_STABLE TYPE LVC_S_STBL.

    METHODS HANDLE_DATA_CHANGED_FINISHED
             FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID
                IMPORTING  E_MODIFIED
                           ET_GOOD_CELLS
                           SENDER.

    METHODS HANDLE_DATA_CHANGED
           FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
               IMPORTING ER_DATA_CHANGED
                         E_ONF4
                         E_ONF4_BEFORE
                         E_ONF4_AFTER
                         E_UCOMM
                         SENDER.

    METHODSHANDLE_BUTTON_CLICK
           FOR EVENT BUTTON_CLICK OF CL_GUI_ALV_GRID
               IMPORTING ES_COL_ID
                         ES_ROW_NO
                         SENDER.

    METHODS HANDLE_TOOLBAR
           FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
               IMPORTING E_OBJECT
                         E_INTERACTIVE
                         SENDER.

    METHODS HANDLE_HOTSPOT_CLICK
          FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
               IMPORTING E_ROW_ID
                         E_COLUMN_ID
                         ES_ROW_NO
                         SENDER.

    METHODS HANDLE_DOUBLE_CLICK
          FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
               IMPORTING E_ROW
                         E_COLUMN
                         ES_ROW_NO
                         SENDER.

    METHODS  HANDLE_USER_COMMAND
          FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
               IMPORTING E_UCOMM
                         SENDER.

    METHODS GET_REFRESH_LIST
            EXPORTING ERT_ALV TYPE TY_T_INS.

    METHODS SET_REFRESH_LIST
            IMPORTING IR_ALV TYPE REF TO CL_GUI_ALV_GRID.


    METHODS CONSTRUCTOR.

  PRIVATE SECTION.

    DATA PV_REFRESH TYPE C.

    CLASS-DATA PCDT_ALV TYPE TABLE OF REF TO CL_GUI_ALV_GRID..

ENDCLASS.                    "LCL_EVENT_RECEIVER DEFINITION

*----------------------------------------------------------------------*
*       CLASS LCL_EVENTS IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENTS IMPLEMENTATION.

  METHOD CONSTRUCTOR.

    PS_STABLE-ROW CNS_X.
    PS_STABLE-COL CNS_X.

  ENDMETHOD.                    "CONSTRUCTOR

  METHOD HANDLE_DATA_CHANGED_FINISHED.

    PERFORM HANDLE_DATA_CHANGED_FINISHED USING E_MODIFIED ET_GOOD_CELLS SENDER PV_REFRESH.

  ENDMETHOD.                    "HANDLE_DATA_CHANGED_FINISHED

  METHOD HANDLE_DATA_CHANGED.

    PV_REFRESH CNS_X.

  ENDMETHOD.                    "HANDLE_DATA_CHANGED

  METHOD HANDLE_BUTTON_CLICK.

    PERFORM HANDLE_BUTTON_CLICK USING ES_COL_ID
                                      ES_ROW_NO
                                      SENDER.

  ENDMETHOD.                    "HANDLE_BUTTON_CLICK

  METHOD HANDLE_HOTSPOT_CLICK.

    PERFORM HANDLE_HOTSPOT_CLICK USING E_ROW_ID
                                       E_COLUMN_ID
                                       ES_ROW_NO
                                       SENDER.

  ENDMETHOD.                    "HANDLE_HOTSPOT_CLICK

  METHOD HANDLE_TOOLBAR.

    PERFORM  HANDLE_TOOLBAR USING  E_OBJECT
                                   E_INTERACTIVE
                                   SENDER.

  ENDMETHOD.                    "HANDLE_TOOLBAR

  METHOD HANDLE_DOUBLE_CLICK.

    PERFORM HANDLE_DOUBLE_CLICK USING E_ROW
                                      E_COLUMN
                                      ES_ROW_NO
                                      SENDER.

  ENDMETHOD.                    "HANDLE_DOUBLE_CLICK


  METHOD HANDLE_USER_COMMAND.

    PERFORM HANDLE_USER_COMMAND USING E_UCOMM
                                      SENDER.

    PERFORM REREHSH_ALL_ALVS USING ME .

  ENDMETHOD.                    "HANDLE_USER_COMMAND


  METHOD SET_REFRESH_LIST.
    APPEND IR_ALV TO PCDT_ALV.
  ENDMETHOD.                    "GET_REFRESH_LIST

  METHOD GET_REFRESH_LIST.
    ERT_ALV   =  PCDT_ALV.
  ENDMETHOD.                    "GET_REFRESH_LIST

ENDCLASS.                    "LCL_EVENTS IMPLEMENTATION
*&---------------------------------------------------------------------*
*&      Form  HANDLE_DATA_CHANGED_FINISHED
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SENDER  text
*----------------------------------------------------------------------*
FORM HANDLE_DATA_CHANGED_FINISHED  USING  E_MODIFIED
                                          ET_GOOD_CELLS TYPE LVC_T_MODI
                                          SENDER        TYPE REF TO CL_GUI_ALV_GRID
                                          UV_REFRESH.

  CASE SENDER.
    WHEN GR_GRID.



    WHEN OTHERS.
  ENDCASE.

ENDFORM.                    " HANDLE_DATA_CHANGED_FINISHED
*&---------------------------------------------------------------------*
*&      Form  HANDLE_BUTTON_CLICK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ES_COL_ID  text
*      -->P_ES_ROW_NO  text
*      -->P_SENDER  text
*----------------------------------------------------------------------*
FORM HANDLE_BUTTON_CLICK  USING ES_COL_ID  TYPE  LVC_S_COL
                                ES_ROW_NO  TYPE LVC_S_ROID
                                SENDER     TYPE REF TO CL_GUI_ALV_GRID.

ENDFORM.                    " HANDLE_BUTTON_CLICK
*&---------------------------------------------------------------------*
*&      Form  HANDLE_HOTSPOT_CLICK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_E_ROW_ID  text
*      -->P_E_COLUMN_ID  text
*      -->P_ES_ROW_NO  text
*      -->P_SENDER  text
*----------------------------------------------------------------------*
FORM HANDLE_HOTSPOT_CLICK  USING  E_ROW_ID     TYPE  LVC_S_ROW
                                  E_COLUMN_ID  TYPE  LVC_S_COL
                                  ES_ROW_NO    TYPE  LVC_S_ROID
                                  SENDER       TYPE REF TO CL_GUI_ALV_GRID.

ENDFORM.                    " HANDLE_HOTSPOT_CLICK
*&---------------------------------------------------------------------*
*&      Form  HANDLE_TOOLBAR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_E_OBJECT  text
*      -->P_E_INTERACTIVE  text
*      -->P_SENDER  text
*----------------------------------------------------------------------*
FORM HANDLE_TOOLBAR  USING  E_OBJECT      TYPE REF TO     CL_ALV_EVENT_TOOLBAR_SET
                            E_INTERACTIVE TYPE CHAR01
                            SENDER        TYPE REF TO CL_GUI_ALV_GRID.

  PERFORM SUB_EXCLUDING_TOOLBAR CHANGING E_OBJECT->MT_TOOLBAR.

*  PERFORM SUB_ADD_BUTTON USING 'SEL'            " 功能键
*                               ICON_SELECT_ALL  " 图标
*                               '0'              " 类型
*                               '选择'           " 文本
*                               1                " 位置
*                   CHANGING E_OBJECT->MT_TOOLBAR.

ENDFORM.                    " HANDLE_TOOLBAR

*&---------------------------------------------------------------------*
*&      Form  HANDLE_DOUBLE_CLICK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->E_ROW      text
*      -->E_COLUMN   text
*      -->ES_ROW_NO  text
*      -->SENDER     text
*----------------------------------------------------------------------*
FORM HANDLE_DOUBLE_CLICK  USING  E_ROW     TYPE  LVC_S_ROW
                                 E_COLUMN  TYPE  LVC_S_COL
                                 ES_ROW_NO TYPE LVC_S_ROID
                                 SENDER    TYPE REF TO CL_GUI_ALV_GRID.

ENDFORM.                    " HANDLE_DOUBLE_CLICK
*&---------------------------------------------------------------------*
*&      Form  HANDLE_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_E_UCOMM  text
*      -->P_SENDER  text
*----------------------------------------------------------------------*
FORM HANDLE_USER_COMMAND  USING    E_UCOMM TYPE SY-UCOMM
                                   SENDER  TYPE REF TO CL_GUI_ALV_GRID.

  CASE SENDER.
    WHEN GR_GRID"LVC




    WHEN OTHERS.
  ENDCASE.

ENDFORM.                    " HANDLE_USER_COMMAND

*&---------------------------------------------------------------------*
*&      Form  REREHSH_ALL_ALVS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ME         text
*----------------------------------------------------------------------*
FORM REREHSH_ALL_ALVS  USING  ME TYPE  REF TO  LCL_EVENTS.

  DATA LTR_ALV      TYPE TABLE OF REF TO CL_GUI_ALV_GRID.
  DATA LSR_ALV      TYPE REF TO CL_GUI_ALV_GRID.

  CALL METHOD ME->GET_REFRESH_LIST
    IMPORTING
      ERT_ALV LTR_ALV.

  LOOP AT LTR_ALV INTO LSR_ALV.

    IF LTR_ALV IS NOT INITIAL.

      CALL METHOD LSR_ALV->REFRESH_TABLE_DISPLAY
        EXPORTING
          IS_STABLE ME->PS_STABLE.

    ENDIF.

  ENDLOOP.

ENDFORM.                    " REREHSH_ALL_ALVS


*&---------------------------------------------------------------------*
*&      Form  SET_GRID_VARI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->CS_VARI    text
*----------------------------------------------------------------------*
FORM SET_GRID_VARI  CHANGING CS_VARI TYPE DISVARIANT.

  CS_VARI-REPORT      SY-REPID.
  CS_VARI-HANDLE      SPACE.
  CS_VARI-LOG_GROUP   SPACE.
  CS_VARI-USERNAME    SPACE.
  CS_VARI-VARIANT     SPACE.
  CS_VARI-TEXT        SPACE.
  CS_VARI-DEPENDVARS  SPACE.

ENDFORM.                    " SET_GRID_VARI

*&---------------------------------------------------------------------*
*&      Form  SET_GRID_LAYO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->CS_LAYO    text
*----------------------------------------------------------------------*
FORM SET_GRID_LAYO  CHANGING CS_LAYO TYPE LVC_S_LAYO.
*...
  CS_LAYO-STYLEFNAME  CNS_STYLEFNAME.

  CS_LAYO-CWIDTH_OPT  SPACE."
  CS_LAYO-ZEBRA       SPACE.
  CS_LAYO-SMALLTITLE  SPACE.
  CS_LAYO-GRAPHICS    SPACE.
  CS_LAYO-FRONTEND    SPACE.
  CS_LAYO-TEMPLATE    SPACE.

*... ALV-Control: Gridcustomizing
  CS_LAYO-NO_HGRIDLN  SPACE.
  CS_LAYO-NO_VGRIDLN  SPACE.
  CS_LAYO-NO_ROWMARK  SPACE.
  CS_LAYO-NO_HEADERS  SPACE.
  CS_LAYO-NO_MERGING  SPACE.
  CS_LAYO-NO_TOOLBAR  SPACE.
  CS_LAYO-GRID_TITLE  SPACE.
  CS_LAYO-SEL_MODE    CNS_D.

  CS_LAYO-BOX_FNAME   CNS_BOX_FNAME .
  CS_LAYO-SGL_CLK_HD  SPACE.

*... ALV-Control: Summenoptionen
  CS_LAYO-TOTALS_BEF  SPACE.
  CS_LAYO-NO_TOTLINE  SPACE.
  CS_LAYO-NUMC_TOTAL  SPACE.
  CS_LAYO-NO_UTSPLIT  SPACE.

*... ALV-Control: Exceptions
  CS_LAYO-EXCP_FNAME  CNS_EXCP_FNAME.
  CS_LAYO-EXCP_ROLLN  SPACE.
  CS_LAYO-EXCP_CONDS  SPACE.
  CS_LAYO-EXCP_LED    SPACE.

*...
  CS_LAYO-DETAILINIT  SPACE.
  CS_LAYO-DETAILTITL  SPACE.
  CS_LAYO-KEYHOT      SPACE.
  CS_LAYO-NO_KEYFIX   SPACE.
  CS_LAYO-NO_AUTHOR   SPACE.
  CLEAR CS_LAYO-S_DRAGDROP.

*... ALV-Control: Farben
  CS_LAYO-INFO_FNAME  CNS_INFO_FNAME.
  CS_LAYO-CTAB_FNAME  SPACE.

*...
  CS_LAYO-EDIT        SPACE.
  CS_LAYO-EDIT_MODE   SPACE.

*... ALV-Control: Web-Optionen
  CS_LAYO-WEBLOOK     SPACE.
  CS_LAYO-WEBSTYLE    SPACE.
  CS_LAYO-WEBROWS     SPACE.
  CS_LAYO-WEBXWIDTH   SPACE.
  CS_LAYO-WEBXHEIGHT  SPACE.

ENDFORM.                    " SET_GRID_LAYO

*&---------------------------------------------------------------------*
*&      Form  SET_GRID_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE      text
*      -->(NAME)     text
*      -->CT_FCAT    text
*----------------------------------------------------------------------*
FORM SET_GRID_FCAT   USING VALUE(NAME)
                           CHANGING CT_FCAT TYPE LVC_T_FCAT.

  CASE NAME.
    WHEN 'CHK'.

      PERFORM SET_FILEDCAT TABLES  CT_FCAT
                            USING  'ICON'
                                   SPACE
                                   SPACE
                                   SPACE
                                   SPACE
                                   CNS_X.

  ENDCASE.

ENDFORM.                    "SET_GRID_FCAT

*&---------------------------------------------------------------------*
*&      Form  SET_FILEDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->T_FIELDCAT text
*      -->VALUE      text
*      -->(P1)       text
*      -->VALUE      text
*      -->(P2)       text
*      -->VALUE      text
*      -->(P3)       text
*      -->VALUE      text
*      -->(P4)       text
*      -->VALUE      text
*      -->(P5)       text
*      -->VALUE      text
*      -->(P6)       text
*----------------------------------------------------------------------*
FORM SET_FILEDCAT  TABLES   T_FIELDCAT STRUCTURE LVC_S_FCAT
                       USING VALUE(P1)
                             VALUE(P2)
                             VALUE(P3)
                             VALUE(P4)
                             VALUE(P5)
                             VALUE(P6).

  DATAT_FIELDCAT_LVC TYPE LVC_T_FCAT,     "slis_t_fieldcat_alv WITH HEADER LINE,
        LS_FIELDCAT    TYPE LVC_S_FCAT.
  DATA  LS_LAYOUT_LVC  TYPE LVC_S_LAYO.

  LS_FIELDCAT-FIELDNAME   P1.
  LS_FIELDCAT-SCRTEXT_M   P2.
  LS_FIELDCAT-EDIT        P3.
  LS_FIELDCAT-OUTPUTLEN   P4.
  LS_FIELDCAT-CHECKBOX    P5.
  LS_FIELDCAT-COL_OPT     P6.

  APPEND LS_FIELDCAT TO T_FIELDCAT.
  CLEAR LS_FIELDCAT.

ENDFORM.                    " SET_FILEDCAT

*&---------------------------------------------------------------------*
*&      Form  REFRESH_TABLE_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->SENDER     text
*----------------------------------------------------------------------*
FORM REFRESH_TABLE_DISPLAY USING SENDER TYPE REF TO CL_GUI_ALV_GRID.

  DATASTBL TYPE LVC_S_STBL.

  STBL-ROW CNS_X."
  STBL-COL CNS_X."

  CALL METHOD SENDER->REFRESH_TABLE_DISPLAY
    EXPORTING
      IS_STABLE      STBL
      I_SOFT_REFRESH SPACE.

ENDFORM.                    " REFRESH_TABLE_DISPLAY

*&---------------------------------------------------------------------*
*&      Form  SUB_EXCLUDING_TOOLBAR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->MT_TOOLBAR text
*----------------------------------------------------------------------*
FORM SUB_EXCLUDING_TOOLBAR  CHANGING  MT_TOOLBAR TYPE TTB_BUTTON.

  DELETE MT_TOOLBAR WHERE FUNCTION '&PRINT_BACK' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&APPEND' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&REFRESH' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL_ROW' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&CUT' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&PASTE' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&INSERT_ROW' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL_ROW' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&INFO' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&MB_VIEW' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&GRAPH' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&DELETE_ROW' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&LOCAL&UNDO' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&CHECK' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&CHECK' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&INFO' .
  DELETE MT_TOOLBAR WHERE FUNCTION '&SEL' .

ENDFORM.                    " SUB_EXCLUDING_TOOLBAR

*&---------------------------------------------------------------------*
*&      Form  SUB_ADD_BUTTON
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE        text
*      -->(FUNCTION)   text
*      -->VALUE        text
*      -->(ICON)       text
*      -->VALUE        text
*      -->(BUTN_TYPE)  text
*      -->VALUE        text
*      -->(TEXT)       text
*      -->VALUE        text
*      -->(INDEX)      text
*      -->MT_TOOLBAR   text
*----------------------------------------------------------------------*
FORM SUB_ADD_BUTTON    USING VALUE(FUNCTION)
                             VALUE(ICON)
                             VALUE(BUTN_TYPE)
                             VALUE(TEXT)
                             VALUE(INDEX)
                    CHANGING MT_TOOLBAR TYPE TTB_BUTTON.


  DATAUTOOLBAR TYPE STB_BUTTON.

  CLEAR UTOOLBAR.
  UTOOLBAR-FUNCTION  FUNCTION.
  UTOOLBAR-ICON      ICON.
  UTOOLBAR-BUTN_TYPE BUTN_TYPE.
  UTOOLBAR-TEXT      TEXT.

  IF INDEX IS NOT INITIAL.

    INSERT  UTOOLBAR INTO MT_TOOLBAR  INDEX INDEX.
    CLEAR   UTOOLBAR.
  ELSE.

    APPEND UTOOLBAR TO MT_TOOLBAR.
  ENDIF.

ENDFORM.                    " SUB_ADD_BUTTON

FORM SET_GRID_EVTS  USING OBJECT TYPE REF TO CL_GUI_ALV_GRID.

  DATA LR_EVENT TYPE REF TO LCL_EVENTS.

  CREATE OBJECT LR_EVENT.

  SET HANDLER LR_EVENT->HANDLE_DATA_CHANGED_FINISHED  FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_TOOLBAR                FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_USER_COMMAND           FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_DATA_CHANGED           FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_DOUBLE_CLICK           FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_HOTSPOT_CLICK          FOR OBJECT.
  SET HANDLER LR_EVENT->HANDLE_BUTTON_CLICK           FOR OBJECT.

  CALL METHOD LR_EVENT->SET_REFRESH_LIST
    EXPORTING
      IR_ALV OBJECT.

ENDFORM.                    " SET_GRID_EVTS


FORM SUB_PBO USING UV_CONTAINER_NAME
                   UR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER
                   GRID_NAME
                   UR_GRID      TYPE REF TO CL_GUI_ALV_GRID.

  IF UR_CONTAINER IS NOT INITIAL.

    PERFORM SUB_CREATE_GRID USING UV_CONTAINER_NAME
                                  UR_CONTAINER
                                  GRID_NAME
                                  UR_GRID.

  ELSE.

    PERFORM REFRESH_TABLE_DISPLAY USING UR_GRID

  ENDIF.

ENDFORM.                    " SUB_PBO

*&---------------------------------------------------------------------*
*&      Form  SUB_CREATE_GRID
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_CREATE_GRID USING UV_CONTAINER_NAME
                           UR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER
                           GRID_NAME
                           UR_GRID      TYPE REF TO CL_GUI_ALV_GRID.

  DATALT_FCAT TYPE LVC_T_FCAT,
        LS_LAYO TYPE LVC_S_LAYO,
        LS_VARI TYPE DISVARIANT.
  DATA  LT_SORT TYPE LVC_T_SORT.

  CREATE OBJECT UR_CONTAINER
    EXPORTING
      CONTAINER_NAME UV_CONTAINER_NAME.

  CREATE OBJECT UR_GRID
    EXPORTING
      I_PARENT UR_CONTAINER.

  PERFORM SET_GRID_VARI CHANGING LS_VARI.
  PERFORM SET_GRID_LAYO CHANGING LS_LAYO.
  PERFORM SET_GRID_FCAT USING    GRID_NAME CHANGING LT_FCAT.
  PERFORM SET_GRID_EVTS USING    UR_GRID.

  CALL METHOD UR_GRID->SET_TABLE_FOR_FIRST_DISPLAY
    EXPORTING
      IS_LAYOUT       LS_LAYO
      IS_VARIANT      LS_VARI
      I_DEFAULT       CNS_X
      I_SAVE          CNS_A
    CHANGING
      IT_OUTTAB       GT_ALV[]
      IT_SORT         LT_SORT[]
      IT_FIELDCATALOG LT_FCAT.

  CALL METHOD UR_GRID->SET_TOOLBAR_INTERACTIVE.

  CALL METHOD CL_GUI_CONTROL=>SET_FOCUS
    EXPORTING
      CONTROL UR_GRID.

  CALL METHOD UR_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.

ENDFORM.                    " SUB_CREATE_GRID


3. 屏幕

  PROCESS BEFORE OUTPUT.

  MODULE PBO.

PROCESS AFTER INPUT.

  MODULE PAI.

MODULE PBO OUTPUT. 

  PERFORM SUB_PBO USING  'GR_CONTAINER'
                         GR_CONTAINER_100
                         'GR_GRID_100'
                         GR_GRID_100.

4. INCLUDE :ZZXXXX_ALV_F02

*&---------------------------------------------------------------------*
*&      Form  SUB_SHOW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_SHOW .

  PERFORM SUB_ALV TABLES GT_ALV.

ENDFORM.                    " SUB_SHOW
*&---------------------------------------------------------------------*
*&      Form  SUB_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM SUB_USER_COMMAND USING R_UCOMM     LIKE SY-UCOMM
                            RS_SELFIELD TYPE SLIS_SELFIELD.

  RS_SELFIELD-REFRESH     CNS_X.
  RS_SELFIELD-COL_STABLE  CNS_X.
  RS_SELFIELD-ROW_STABLE  CNS_X.

  CASE R_UCOMM.

    WHEN 'BACK' OR 'EXIT' OR  'CANCEL'.

      LEAVE TO SCREEN 0.

      " WHEN."

  ENDCASE.

ENDFORM.                    "SUB_USER_COMMAND

*&---------------------------------------------------------------------*
*&      Form  SUB_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->TABLE      text
*----------------------------------------------------------------------*
FORM SUB_ALV TABLES TABLE.

  IF TABLE[] IS  INITIAL.

    MESSAGE '无检索数据!' TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.

  ENDIF.

  DATA SUB_USER_COMMAND TYPE SLIS_FORMNAME      VALUE 'SUB_USER_COMMAND'.
  DATA SUB_SET_STATUS   TYPE SLIS_FORMNAME      VALUE 'SUB_SET_STATUS'.
  DATA G_VARIANT        LIKE DISVARIANT.
  DATA IT_ALV           TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
  DATA IT_SORT_LVC      TYPE LVC_T_SORT WITH HEADER LINE.
  DATA LT_FIELDCAT      TYPE LVC_T_FCAT.

  PERFORM SUB_FILEDCAT_LVC TABLES LT_FIELDCAT.

  PERFORM SUB_SORT_LVC TABLES IT_SORT_LVC.

  PERFORM SUB_SET_EVENT.

  GS_LAYOUT-EXCP_FNAME  'LIGHTS'.
*  GS_LAYOUT_LVC-CWIDTH_OPT  = 'X'.
  GS_LAYOUT-BOX_FNAME   'SEL'.
  GS_LAYOUT-STYLEFNAME  'FSTYLE'.
  GS_LAYOUT-SEL_MODE    CNS_A.
  GS_LAYOUT-CTAB_FNAME  'CELLCOL'.
  GS_LAYOUT-INFO_FNAME  'COLOR'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_CALLBACK_PROGRAM       SY-REPID
      I_CALLBACK_PF_STATUS_SET SUB_SET_STATUS
      I_CALLBACK_USER_COMMAND  SUB_USER_COMMAND
      IS_LAYOUT_LVC            GS_LAYOUT
      IT_FIELDCAT_LVC          LT_FIELDCAT[]
      IT_EVENTS                GT_EVENTS"
      I_HTML_HEIGHT_TOP        15
      I_SAVE                   CNS_A
      IT_SORT_LVC              IT_SORT_LVC[]
    TABLES
      T_OUTTAB                 TABLE
    EXCEPTIONS
      PROGRAM_ERROR            1
      OTHERS                   2.

ENDFORM.                    " SUB_ALV

*&---------------------------------------------------------------------*
*&      Form  SUB_SET_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->EXTAB      text
*----------------------------------------------------------------------*
FORM SUB_SET_STATUS USING EXTAB TYPE SLIS_T_EXTAB.

  DATA LS_EXTAB LIKE LINE OF EXTAB.

  SET PF-STATUS 'ALV' EXCLUDING EXTAB.

ENDFORM.                    "SUB_SET_STATUS

*&---------------------------------------------------------------------*
*&      Form  SUB_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  SUB_SET_EVENT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_SET_EVENT .

  DATA FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'FRM_TOP_OF_PAGE'.
  DATA LS_EVENTS            TYPE LINE OF SLIS_T_EVENT.

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      I_LIST_TYPE     0
    IMPORTING
      ET_EVENTS       GT_EVENTS
    EXCEPTIONS
      LIST_TYPE_WRONG 1
      OTHERS          2.

  IF SY-SUBRC <> 0.

    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

  ENDIF.

  LS_EVENTS-NAME 'CALLER_EXIT'.
  LS_EVENTS-FORM 'SUB_EVENT_LVC'.
  APPEND LS_EVENTS TO GT_EVENTS.

ENDFORM.                    " SUB_SET_EVENT

*&---------------------------------------------------------------------*
*&      Form  FM_BUTTON
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->E_GRID     text
*----------------------------------------------------------------------*
FORM SUB_EVENT_LVC USING E_GRID TYPE SLIS_DATA_CALLER_EXIT.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID GR_GRID.

  CALL METHOD GR_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID CL_GUI_ALV_GRID=>MC_EVT_ENTER
    EXCEPTIONS
      ERROR      1
      OTHERS     2.

  CALL METHOD GR_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
    EXCEPTIONS
      ERROR      1
      OTHERS     2.

  DATA LR_EVENTS TYPE REF TO LCL_EVENTS.

  CREATE OBJECT LR_EVENTS.

  SET HANDLER LR_EVENTS->HANDLE_DATA_CHANGED_FINISHED FOR GR_GRID.
  SET HANDLER LR_EVENTS->HANDLE_DATA_CHANGED          FOR GR_GRID.
  SET HANDLER LR_EVENTS->HANDLE_BUTTON_CLICK          FOR GR_GRID.

ENDFORM.                    "FM_BUTTON
*&---------------------------------------------------------------------*
*&      Form  SUB_FILEDCAT_LVC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_FIELDCAT  text
*----------------------------------------------------------------------*
FORM SUB_FILEDCAT_LVC  TABLES T_FIELDCAT_LVC STRUCTURE GS_FIELDCAT.
  "字段名 字段描述 是否编辑 显示长度 CHECKBOX 自动调整

*  CLEAR GT_FCAT.
*  ADD_FIELD ''      ''           '' '10' ''  ''.
*  ADD_FIELD ''      ''           '' '10' ''  ''.
*  ADD_FIELD ''      ''           '' '10' ''  ''.
*  ADD_FIELD ''      ''           '' '10' ''  ''.
*  ADD_FIELD ''      ''           '' '10' ''  ''.
*  T_FIELDCAT_LVC = GT_FCAT.

ENDFORM.                    " SUB_FILEDCAT_LVC
*&---------------------------------------------------------------------*
*&      Form  SUB_SORT_LVC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IT_SORT_LVC  text
*----------------------------------------------------------------------*
FORM SUB_SORT_LVC  TABLES T_SORT_LVC STRUCTURE LVC_S_SORT .

*  CLEAR T_SORT_LVC.

*  T_SORT_LVC-FIELDNAME = ''.  "字段"
*  T_SORT_LVC-SUBTOT    = 'X'. "合计"
*  APPEND T_SORT_LVC.

ENDFORM.                    " SUB_SORT_LVC
*&---------------------------------------------------------------------*
*&      Form  %
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->&1         text
*      -->&2         text
*----------------------------------------------------------------------*
FORM %  USING &1
              &2.

  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      PERCENTAGE &1
      TEXT       &2.

ENDFORM.                    " %


5.INCLUDE:ZZXXXX_ALV_MAIN

*&---------------------------------------------------------------------*
*&  包含                ZZXXXX_ALV_MAIN
*&---------------------------------------------------------------------*

INITIALIZATION.

  PERFORM SUB_INIT.

*&---------------------------------------------------------------------*
*&   Event at selection-screen output
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.

  PERFORM SUB_SPBO.

AT SELECTION-SCREEN.

  PERFORM SUB_SPAI.

*&---------------------------------------------------------------------*
*&   Event start-of-selection

*&---------------------------------------------------------------------*

START-OF-SELECTION.

  PERFORM SUB_GET_DATA.

*&---------------------------------------------------------------------*
*&   Event end-of-selection
*&---------------------------------------------------------------------*
END-OF-SELECTION.

  PERFORM SUB_SHOW.


6.INCLUDE:ZZXXXX_ALV_F01

*&---------------------------------------------------------------------*
*&      Form  SUB_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_INIT .

ENDFORM.                    " SUB_INIT
*&---------------------------------------------------------------------*
*&      Form  SUB_SPBO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_SPBO .

ENDFORM.                    " SUB_SPBO
*&---------------------------------------------------------------------*
*&      Form  SUB_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_GET_DATA .

  " ---取数逻辑---
  SELECT INTO CORRESPONDING FIELDS OF TABLE GT_DATA
    FROM BKPF
     WHERE BELNR IN S_BELNR
       AND GJAHR IN S_GJAHR
       AND BUKRS IN S_BUKRS.

  CLEAR_TABLE GT_ALV.

  LOOP AT GT_DATA ASSIGNING FS_DATA .

    APPEND INITIAL LINE TO GT_ALV ASSIGNING FS_ALV .

    MOVE-CORRESPONDING  FS_DATA TO  FS_ALV.

    PERFORM SUB_CHECK_DATA USING FS_ALV.

  ENDLOOP.

ENDFORM.                    " SUB_GET_DATA
*&---------------------------------------------------------------------*
*&      Form  SUB_SPAI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SUB_SPAI .

ENDFORM.                    " SUB_SPAI
*&---------------------------------------------------------------------*
*&      Form  SUB_CHECK_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_  text
*----------------------------------------------------------------------*
FORM SUB_CHECK_DATA USING US_ALV TYPE TY_ALV.

  DATA LV_STYLEROW  TYPE LVC_S_STYL.
  DATA LS_CELLCOLOR TYPE LVC_S_SCOL " 单元格颜色结构
  DATA LV_TABIX     TYPE SY-TABIX.
  DATA LV_FIELDNAME TYPE STRING.

  "BUTTON 案例 设置单元格按钮
  "配合ALV事件使用 button click方法"
*  IF US_ALV-XXXXXX IS NOT INITIAL.
*  BUTTON  'ICON1'  '@K1@'.
*  ENDIF.

*  US_ALV-COLOR = 'C411'.

*  IF US_ALV-BELNR IS NOT INITIAL.
*    "设置导航热键
*    HOTS 'BELNR'.
*
*  ENDIF.

*  IF US_ALV-XXXX IS NOT INITIAL...

*    "---设置单元格不能编辑---
*    ENABLED 'CHK'.

*    "---设置单元格颜色---
*    SET_COLOR 'CHK' '2' '0' '1'  US_ALV-CELLCOL.

*  ELSE.

*    "---设置单元格可编辑---
*    DISABLED 'CHK'.

*  ENDIF.

ENDFORM.                    " SUB_CHECK_DATA


赞赏支持