首页 > 代码库 > OO ALV

OO ALV

REPORT ZJ_TE6.DATA:P_MATNR LIKE MARC-MATNR, "物料     P_WERKS LIKE MARC-WERKS. " 公司DATA:OK_CODE  LIKE SY-UCOMM,      SAVE_OK LIKE SY-UCOMM.DATA: P1 TYPE REF TO CL_GUI_CUSTOM_CONTAINER,  " 控制对象      G_GRID TYPE REF TO CL_GUI_ALV_GRID.      "ALV对象DATA: GT_FIELDCAT TYPE LVC_T_FCAT, " 功能C: 设定fieldcat      GS_FIELDCAT TYPE LVC_S_FCAT.DATA: LS_LAYOUT  TYPE LVC_S_LAYO.  "设置样式DATA: GT_EXCLUDE TYPE UI_FUNCTIONS.TYPES: BEGIN OF GT_STR ,  MATNR  LIKE MARC-MATNR, "物料  WERKS  LIKE MARC-WERKS, "工厂  EKGRP  LIKE MARC-EKGRP, "物料组  AUSME  LIKE MARC-AUSME,  EDIT TYPE LVC_T_STYL,"控制单元格编辑  END OF GT_STR.DATA:GT_DATA TYPE TABLE OF GT_STR  WITH HEADER LINE.*----------------------------------------------------------------------**       CLASS LCL_EVENT_HANDLER DEFINITION*----------------------------------------------------------------------***----------------------------------------------------------------------*CLASS LCL_EVENT_HANDLER DEFINITION.  PUBLIC SECTION.    METHODS HANDLE_DOUBLE       FOR EVENT  DOUBLE_CLICK OF CL_GUI_ALV_GRID IMPORTING E_ROW E_COLUMN.  " 双击事件    METHODS HANDLE_TOOLBAR      FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID IMPORTING E_OBJECT. " 设定增加自定义ALV工具栏的按钮    METHODS HANDLE_USER_COMMAND FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID IMPORTING E_UCOMM. " 设定响应user command    "某字段设置热键后,单击此单元格时触发    METHODS  HANDLE_CELL_CLICK   FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID      IMPORTING        E_ROW_ID        E_COLUMN_ID        ES_ROW_NO.ENDCLASS.                    "LCL_EVENT_HANDLER DEFINITION*----------------------------------------------------------------------**       CLASS LCL_EVENT_HANDLER IMPLEMENTATION*----------------------------------------------------------------------***----------------------------------------------------------------------*CLASS LCL_EVENT_HANDLER IMPLEMENTATION.  METHOD HANDLE_DOUBLE.    MESSAGE S000(Z000) WITH 双击事件.  ENDMETHOD.                    "HANDLE_DOUBLE  "新增按钮  METHOD  HANDLE_TOOLBAR.    DATA: LS_TOOLBAR TYPE STB_BUTTON.    CLEAR LS_TOOLBAR.    LS_TOOLBAR-TEXT = 新增按钮.    LS_TOOLBAR-FUNCTION = ADD1.    LS_TOOLBAR-ICON = ICON_CALCULATION.    APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.  ENDMETHOD.                    "HANDLE_TOOLBAR  " 新增按钮响应  METHOD HANDLE_USER_COMMAND.    CASE  E_UCOMM.      WHEN ADD1.        MESSAGE S000(Z000) WITH  你点击了新增按钮.    ENDCASE.  ENDMETHOD.                    "HANDLE_USER_COMMAND  METHOD HANDLE_CELL_CLICK.    "获取单击字段    DATA: L_MSG      TYPE STRING,          L_WA_CLICK LIKE LINE OF GT_DATA,          L_ROW      TYPE STRING,          L_FIELD    TYPE STRING.    L_ROW      = ES_ROW_NO-ROW_ID.               "选中行    L_FIELD    = E_COLUMN_ID.                    "选中的字段    READ TABLE GT_DATA INTO L_WA_CLICK INDEX ES_ROW_NO-ROW_ID.    IF SY-SUBRC = 0.      "得到所单击值      CONCATENATE 您选中第 L_ROW  行,字段为: L_FIELD  ,选中值为 INTO L_MSG.      MESSAGE L_MSG TYPE I.    ENDIF.  ENDMETHOD.                    "handle_cell_clickENDCLASS.                    "LCL_EVENT_HANDLER IMPLEMENTATIONDATA: GR_EVENT TYPE REF TO LCL_EVENT_HANDLER.START-OF-SELECTION.  CALL SCREEN 2000.*&---------------------------------------------------------------------**&      Module  STATUS_2000  OUTPUT*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*MODULE STATUS_2000 OUTPUT.  SET PF-STATUS STATUS_2000.  PERFORM GET_DATA.  "取值  PERFORM ALV_STYLE. "样式  PERFORM ALV_FIELD.  PERFORM EXCLUDE_TB_FUNCTIONS  TABLES GT_EXCLUDE.  "隐藏标准按钮  PERFORM ALV_SHOW.  "ALV 显示ENDMODULE.                 " STATUS_2000  OUTPUT*&---------------------------------------------------------------------**&      Module  USER_COMMAND_2000  INPUT*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*MODULE USER_COMMAND_2000 INPUT.  CLEAR:SAVE_OK.  SAVE_OK = OK_CODE.  CLEAR:OK_CODE.  CASE SAVE_OK.    WHEN CANCEL .      LEAVE PROGRAM.    WHEN BACK .      LEAVE PROGRAM.    WHEN LEAVE .      LEAVE PROGRAM.  ENDCASE.ENDMODULE.                 " USER_COMMAND_2000  INPUT*&---------------------------------------------------------------------**&      Form  get_data*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM GET_DATA.  RANGES: R_MATNR FOR MARC-MATNR,          R_WERKS FOR MARC-WERKS.  IF P_MATNR IS NOT INITIAL.    R_MATNR-LOW = P_MATNR.    R_MATNR-SIGN = I.    R_MATNR-OPTION = CP.    APPEND R_MATNR.  ENDIF.  IF P_WERKS IS  INITIAL.    P_WERKS = 2000.  ENDIF.  SELECT   MATNR  WERKS  EKGRP AUSME  FROM MARC INTO CORRESPONDING FIELDS OF TABLE GT_DATA    UP TO 100 ROWS    WHERE  WERKS EQ P_WERKS     AND MATNR IN R_MATNR.ENDFORM.                    "get_data*&---------------------------------------------------------------------**&      Form  ALV_FIELD*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM ALV_FIELD.  GS_FIELDCAT-FIELDNAME = MATNR.    " 字段名  GS_FIELDCAT-COLTEXT = 物料.       " 显示名称  GS_FIELDCAT-EDIT = X.             " 是否可编辑  APPEND GS_FIELDCAT TO GT_FIELDCAT.  "  CLEAR GS_FIELDCAT.  GS_FIELDCAT-FIELDNAME = WERKS.    " 字段名  GS_FIELDCAT-COLTEXT = 工厂.       " 显示名称  GS_FIELDCAT-EDIT = X.  APPEND GS_FIELDCAT TO GT_FIELDCAT.  "  CLEAR GS_FIELDCAT.  GS_FIELDCAT-FIELDNAME = EKGRP.    " 字段名  GS_FIELDCAT-COLTEXT = 物料组.       " 显示名称  GS_FIELDCAT-EDIT = X.  APPEND GS_FIELDCAT TO GT_FIELDCAT.  "  CLEAR GS_FIELDCAT.  GS_FIELDCAT-FIELDNAME = AUSME.    " 字段名  GS_FIELDCAT-COLTEXT = 交货单位.       " 显示名称  APPEND GS_FIELDCAT TO GT_FIELDCAT.  "  CLEAR GS_FIELDCAT.ENDFORM.                    "ALV_FIELD*&---------------------------------------------------------------------**&      Form  ALV_STYLE*&---------------------------------------------------------------------**       text  样式*----------------------------------------------------------------------*FORM ALV_STYLE.  LS_LAYOUT-ZEBRA = X.  LS_LAYOUT-GRID_TITLE = 物料信息查询.  LS_LAYOUT-SMALLTITLE = X.  LS_LAYOUT-SEL_MODE = ‘‘.  "  ls_layout-no_TOOLBAR = ‘X‘.    " 隐藏所有按钮 " LS_LAYOUT-SEL_MODE = ‘A‘. LS_LAYOUT-STYLEFNAME = EDIT.   "设置单元格 LS_LAYOUT-CWIDTH_OPT = X.ENDFORM.                    "ALV_STYLE*&---------------------------------------------------------------------**&      Form  ALV_EVENT*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM ALV_EVENT.  CREATE OBJECT GR_EVENT.  SET HANDLER GR_EVENT->HANDLE_DOUBLE FOR G_GRID.  SET HANDLER GR_EVENT->HANDLE_TOOLBAR FOR G_GRID.     "增加自定义ALV工具栏的按钮  SET HANDLER GR_EVENT->HANDLE_USER_COMMAND FOR G_GRID. "  设定响应user command  SET HANDLER GR_EVENT->HANDLE_CELL_CLICK  FOR G_GRID.ENDFORM.                    "ALV_EVENT*&---------------------------------------------------------------------**&      Form  ALV_SHOW*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM ALV_SHOW.  IF P1 IS INITIAL.    "创建容器对象    CREATE OBJECT P1     EXPORTING*      parent                      =       CONTAINER_NAME              = P1        .    "   创建 ALV_FRID 对象    CREATE OBJECT G_GRID      EXPORTING        I_PARENT          = P1      EXCEPTIONS        ERROR_CNTL_CREATE = 1        ERROR_CNTL_INIT   = 2        ERROR_CNTL_LINK   = 3        ERROR_DP_CREATE   = 4        OTHERS            = 5.    " 设置事件    PERFORM ALV_EVENT.    "数据显示    CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY     " 基本显示    EXPORTING*          i_buffer_active               =*          i_bypassing_buffer            =*          i_consistency_check           =*          i_structure_name              =*          is_variant                    =*          i_save                        = ‘X‘*          i_default                     = ‘X‘           IS_LAYOUT                     = LS_LAYOUT   "设定layout*          is_print                      =*          it_special_groups             =           IT_TOOLBAR_EXCLUDING          = GT_EXCLUDE  "设定屏蔽标准ALV 工具栏的按钮*          it_hyperlink                  =*          it_alv_graphics               =*          it_except_qinfo               =*          ir_salv_adapter               =        CHANGING          IT_OUTTAB                     = GT_DATA[]          IT_FIELDCATALOG               = GT_FIELDCAT[] "设定fieldcat*          it_sort                       =*          it_filter                     =        EXCEPTIONS          INVALID_PARAMETER_COMBINATION = 1          PROGRAM_ERROR                 = 2          TOO_MANY_LINES                = 3          OTHERS                        = 4              .  ELSE.    PERFORM CHANG_EDIT_DATA.    "    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY  "刷新ALV 最新的值     EXCEPTIONS       FINISHED       = 1       OTHERS         = 2           .  ENDIF.ENDFORM  .                  "ALV_SHOW*&---------------------------------------------------------------------**&      Form  CHANGE_EDIT_MODE*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM CHANGE_EDIT_MODE.  IF G_GRID IS NOT INITIAL.    IF G_GRID->IS_READY_FOR_INPUT( ) EQ 0.      CALL METHOD G_GRID->SET_READY_FOR_INPUT  "实现alv中可编辑的字段可见进行编辑        EXPORTING          I_READY_FOR_INPUT = 1.    ELSE.      CALL METHOD G_GRID->CHECK_CHANGED_DATA. "  (该方法:就是获取画面数据改变的事件,并把相应的数据修改到内表)      CALL METHOD G_GRID->SET_READY_FOR_INPUT        EXPORTING          I_READY_FOR_INPUT = 0.    ENDIF.  ENDIF.ENDFORM.                    "CHANGE_EDIT_MODE*&---------------------------------------------------------------------**&      Form  chang_edit_data*&---------------------------------------------------------------------**       text*----------------------------------------------------------------------*FORM CHANG_EDIT_DATA.  FIELD-SYMBOLS:<LINE> TYPE GT_STR,               <FIELD> TYPE ANY.  DATA:LS_COMP TYPE ABAP_COMPDESCR,       LT_STRUCT TYPE REF TO CL_ABAP_STRUCTDESCR.  DATA:LS_CELL_EDIT TYPE LVC_S_STYL,"单元格编辑控制       LT_CELL_EDIT TYPE LVC_T_STYL.  LT_STRUCT ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( GT_STR ).  LOOP AT GT_DATA ASSIGNING <LINE>.    REFRESH:<LINE>-EDIT[].    CLEAR:LS_CELL_EDIT.    DO 4 TIMES.      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <LINE> TO <FIELD>.      "不为空的设置不可以编辑      IF <FIELD> IS NOT INITIAL.        READ TABLE LT_STRUCT->COMPONENTS INTO LS_COMP INDEX SY-INDEX.        LS_CELL_EDIT-FIELDNAME = LS_COMP-NAME.   "字段        LS_CELL_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED. "控制样式        INSERT  LS_CELL_EDIT INTO TABLE LT_CELL_EDIT   .        CLEAR:LS_CELL_EDIT.      ENDIF.    ENDDO.    <LINE>-EDIT = LT_CELL_EDIT.    REFRESH:LT_CELL_EDIT[].  ENDLOOP.ENDFORM.                    "chang_edit_data*&---------------------------------------------------------------------**&      Form  exclude_tb_functions*&---------------------------------------------------------------------**       text   将标准按钮从屏幕中拿掉*----------------------------------------------------------------------**      -->PT_EXCLUDE text*----------------------------------------------------------------------*FORM EXCLUDE_TB_FUNCTIONS  TABLES PT_EXCLUDE TYPE UI_FUNCTIONS .  DATA: LS_EXCLUDE TYPE UI_FUNC.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_MAXIMUM .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_MINIMUM .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SUBTOT .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SUM .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_AVERAGE .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_MB_SUM .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_MB_SUBTOT .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SORT_ASC.  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SORT_DSC .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_FIND .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_FILTER .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_PRINT .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_PRINT_PREV .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_MB_EXPORT .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_GRAPH .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_MB_VIEW .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_DETAIL .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_HELP .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_INFO .  APPEND LS_EXCLUDE TO PT_EXCLUDE.  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_MB_VARIANT.  APPEND LS_EXCLUDE TO PT_EXCLUDE.ENDFORM.                    "EXCLUDE_TB_FUNCTIONS

 

OO ALV