码迷,mamicode.com
首页 > 其他好文 > 详细

function ALV

时间:2014-12-25 20:00:51      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

自我ALV 理解学习,集有 回车事件 ,自定义按钮,ALV网格颜色。

REPORT ZALV_TEST.
TYPE-POOLS:ZSLIS.

DATA:BEGIN OF GT_DATA OCCURS 0,  "显示数据内表。
     MATNR LIKE MARA-MATNR,  "物料
     MATKL LIKE MARA-MATKL,  "物料组
     BISMT LIKE MARA-BISMT,  "旧物料
     MEINS LIKE MARA-MEINS,  "基本单位
     COLOR(4) TYPE C,
  END OF GT_DATA.


DATA: BEGIN OF GT_DATA2 OCCURS 0.
        INCLUDE STRUCTURE GT_DATA.
DATA: CE_COLOR TYPE SLIS_T_SPECIALCOL_ALV.
DATA: END OF GT_DATA2.


DATA:GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: GS_LAYOUT   TYPE SLIS_LAYOUT_ALV.

DATA: GIT_EVENTS TYPE SLIS_T_EVENT.     "ALV 事件
DATA TEM_GRID  TYPE REF TO CL_GUI_ALV_GRID.


*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.
  PUBLIC SECTION.
    " 回车事件
    METHODS HANDLE_MODIFY
    FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID
    IMPORTING  E_MODIFIED ET_GOOD_CELLS.

    " 鼠标离开事件
    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.
ENDCLASS.                    "LCL_EVENT_RECEIVER DEFINITION

*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
  METHOD HANDLE_MODIFY.
    MESSAGE: S000(Z001) WITH 回车.
  ENDMETHOD.                    "HANDLE_MODIFY

  METHOD HANDLE_DATA_CHANGED.
    DATA STBL TYPE LVC_S_STBL.
    PERFORM HANDLE_DATA_CHANGED  USING ER_DATA_CHANGED.  "处理受变化的数据

*   稳定刷新
    STBL-ROW = X." 基于行的稳定刷新
    STBL-COL = X." 基于列稳定刷新
    CALL METHOD TEM_GRID->REFRESH_TABLE_DISPLAY
      EXPORTING
        IS_STABLE = STBL.
  ENDMETHOD.                    "HANDLE_MODIFY
ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION

DATA GT_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER .   " 定义类


START-OF-SELECTION.

  PERFORM GET_DATA.  "
  PERFORM SET_FIELDCAT.
  " PERFORM ALV_LAYOUT.   " 设置行颜色
  PERFORM GET_EVENT.      " 设置事件
  PERFORM ALV_SHOW.

  "PERFORM SET_COLOR.  "设置单元格颜色



*&---------------------------------------------------------------------*
*&      Form  get_event
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM GET_EVENT.

  DATA FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE FRM_TOP_OF_PAGE.
  DATA L_EVENTS TYPE LINE OF SLIS_T_EVENT.

  CALL FUNCTION REUSE_ALV_EVENTS_GET
    EXPORTING
      I_LIST_TYPE     = 0
    IMPORTING
      ET_EVENTS       = GIT_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.

  CLEAR L_EVENTS.

  READ TABLE GIT_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE INTO L_EVENTS.
  L_EVENTS-NAME = CALLER_EXIT.   "事件名
  L_EVENTS-FORM = FM_BUTTON.     "处理事件FORM
  APPEND L_EVENTS TO GIT_EVENTS.


ENDFORM.                    "get_event




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

  " 得到当前的 ALV
  CALL FUNCTION GET_GLOBALS_FROM_SLVC_FULLSCR
*   EXPORTING
*     IR_SALV_FULLSCREEN_ADAPTER       =
      IMPORTING
        E_GRID                           = TEM_GRID
          .

* 设置enter事件
  CALL METHOD TEM_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER
    EXCEPTIONS
      ERROR      = 1
      OTHERS     = 2.

* 设置 单光标焦点移开被修改单元格后既触发事件
  CALL METHOD TEM_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
    EXCEPTIONS
      ERROR      = 1
      OTHERS     = 2.

  "创建类对象
  CREATE OBJECT GT_EVENT_RECEIVER.
  " 设置方法
  SET HANDLER GT_EVENT_RECEIVER->HANDLE_MODIFY FOR TEM_GRID.
  SET HANDLER GT_EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR TEM_GRID.
ENDFORM.                    "fm_button


*&---------------------------------------------------------------------*
*&      Form  handle_data_changed
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ER_DATA_CHANGED  text
*----------------------------------------------------------------------*
FORM HANDLE_DATA_CHANGED  USING P_ER_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
  DATA:MOD_DATA TYPE LVC_T_MODI,
       WA_MOD_DATA TYPE LVC_S_MODI.

  MOD_DATA = P_ER_DATA_CHANGED->MT_MOD_CELLS.      " 获取变更后列

  DATA:ROW_ID TYPE I,
       C_VALUE(130) TYPE C,  "变更后的值
       C_NAME(30) TYPE C.    "变更的列名

  CLEAR:ROW_ID, C_VALUE,C_NAME.

  LOOP AT MOD_DATA INTO WA_MOD_DATA.
    ROW_ID = WA_MOD_DATA-ROW_ID.
    C_NAME = WA_MOD_DATA-FIELDNAME.
    C_VALUE =  WA_MOD_DATA-VALUE.
  ENDLOOP.

  MESSAGE S000(Z001) WITH  修改成功.


ENDFORM.                    "HANDLE_DATA_CHANGED


*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM GET_DATA.
  SELECT  MARA~MATNR  MARA~MATKL MARA~BISMT MARA~MEINS FROM MARA INTO TABLE GT_DATA  UP TO 100  ROWS.
ENDFORM.                    "get_data



*&---------------------------------------------------------------------*
*&      Form  SET_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM SET_FIELDCAT.
  DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.

  LS_FIELDCAT-FIELDNAME  = MATNR.
  LS_FIELDCAT-SELTEXT_L  = 物料.
  LS_FIELDCAT-KEY        = X.  "关键列
  LS_FIELDCAT-EDIT        = X.
  "  LS_FIELDCAT-EMPHASIZE  = ‘C100‘.  "列颜色
  APPEND LS_FIELDCAT TO GT_FIELDCAT.
  CLEAR: LS_FIELDCAT.

  LS_FIELDCAT-FIELDNAME  = MATKL.
  LS_FIELDCAT-SELTEXT_L  = 物料组.
  "  LS_FIELDCAT-EMPHASIZE  = ‘C200‘.  "列颜色
  APPEND LS_FIELDCAT TO GT_FIELDCAT.
  CLEAR: LS_FIELDCAT.


  LS_FIELDCAT-FIELDNAME  = BISMT.
  LS_FIELDCAT-SELTEXT_L  = 旧物料.
  "  LS_FIELDCAT-EMPHASIZE  = ‘C300‘.  "列颜色
  APPEND LS_FIELDCAT TO GT_FIELDCAT.
  CLEAR: LS_FIELDCAT.

  LS_FIELDCAT-FIELDNAME  = MEINS.
  LS_FIELDCAT-SELTEXT_L  = 基本单位.
  "  LS_FIELDCAT-EMPHASIZE  = ‘C400‘.  "列颜色
  APPEND LS_FIELDCAT TO GT_FIELDCAT.
  CLEAR: LS_FIELDCAT.
ENDFORM.                    "SET_FIELDCAT


*&---------------------------------------------------------------------*
*&      Form  ALV_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM ALV_LAYOUT.

  DATA: L_COLOR(4) TYPE C,
        L_CHAR     TYPE C,
        I          TYPE I.

  LOOP AT GT_DATA.
    L_CHAR = SY-INDEX.
    IF SY-TABIX MOD 2 = 0.
      CONCATENATE C  101 INTO L_COLOR.
    ELSE.
      CONCATENATE C  210 INTO L_COLOR.
    ENDIF.
    GT_DATA-COLOR = L_COLOR.
    MODIFY GT_DATA.
  ENDLOOP.

  GS_LAYOUT-INFO_FIELDNAME = COLOR.  "设置行的颜色
ENDFORM.                    "ALV_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  set_COLOR
*&---------------------------------------------------------------------*
*       text  设置单元个颜色
*----------------------------------------------------------------------*
FORM SET_COLOR.
  DATA: COLOR_WA TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE."单元格颜色属性内表

  LOOP AT GT_DATA.
    MOVE-CORRESPONDING GT_DATA TO GT_DATA2. "转移数据到itab_test3内表

    REFRESH COLOR_WA.
    " 第一列颜色
    COLOR_WA-COLOR-COL = 2.  "颜色代码
    COLOR_WA-COLOR-INT = 0.
    COLOR_WA-COLOR-INV = 0.
    COLOR_WA-FIELDNAME = MATNR.
    APPEND COLOR_WA.
    "    CONCATENATE L_CHAR ‘00‘ INTO ITAB_TEST3-COL1.
    " 第二列颜色
    COLOR_WA-COLOR-INT = 0.
    COLOR_WA-COLOR-INV = 1.
    COLOR_WA-FIELDNAME = MATKL.
    APPEND COLOR_WA.
    "    CONCATENATE L_CHAR ‘01‘ INTO ITAB_TEST3-COL2.
    " 第三列颜色
    COLOR_WA-COLOR-INT = 1.
    COLOR_WA-COLOR-INV = 0.
    COLOR_WA-FIELDNAME = BISMT.
    APPEND COLOR_WA.
    "    CONCATENATE L_CHAR ‘10‘ INTO ITAB_TEST3-COL3.
    GT_DATA2-CE_COLOR[] = COLOR_WA[].
    APPEND GT_DATA2.
  ENDLOOP.

  GS_LAYOUT-COLTAB_FIELDNAME = CE_COLOR.

  CALL FUNCTION REUSE_ALV_GRID_DISPLAY
    EXPORTING
      IS_LAYOUT   = GS_LAYOUT
      IT_FIELDCAT = GT_FIELDCAT[]
    TABLES
      T_OUTTAB    = GT_DATA2.


ENDFORM.                    "set_COLOR


*&---------------------------------------------------------------------*
*&      Form  ALV_SHOW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM ALV_SHOW.
  CALL FUNCTION REUSE_ALV_GRID_DISPLAY
   EXPORTING
*     I_INTERFACE_CHECK                 = ‘ ‘
*     I_BYPASSING_BUFFER                = ‘ ‘
*     I_BUFFER_ACTIVE                   = ‘ ‘
     I_CALLBACK_PROGRAM                = SY-REPID  "返回的调用程序
     I_CALLBACK_PF_STATUS_SET          = SET_PF_STATUS  "ALV状态栏
     I_CALLBACK_USER_COMMAND           = USER_COMMAND   "按钮方法
*     I_CALLBACK_TOP_OF_PAGE            = ‘ ‘
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ‘ ‘
*     I_CALLBACK_HTML_END_OF_LIST       = ‘ ‘
*     I_STRUCTURE_NAME                  =
*     I_BACKGROUND_ID                   = ‘ ‘
*     I_GRID_TITLE                      =
*     I_GRID_SETTINGS                   =
      IS_LAYOUT                         =  GS_LAYOUT
      IT_FIELDCAT                       =  GT_FIELDCAT[]
*     IT_EXCLUDING                      =
*     IT_SPECIAL_GROUPS                 =
*     IT_SORT                           =
*     IT_FILTER                         =
*     IS_SEL_HIDE                       =
*     I_DEFAULT                         = ‘X‘
*     I_SAVE                            = ‘ ‘   "是否保存变式
*     IS_VARIANT                        =
     IT_EVENTS                         =    GIT_EVENTS
*     IT_EVENT_EXIT                     =
*     IS_PRINT                          =
*     IS_REPREP_ID                      =
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE                 = 0
*     I_HTML_HEIGHT_TOP                 = 0
*     I_HTML_HEIGHT_END                 = 0
*     IT_ALV_GRAPHICS                   =
*     IT_HYPERLINK                      =
*     IT_ADD_FIELDCAT                   =
*     IT_EXCEPT_QINFO                   =
*     IR_SALV_FULLSCREEN_ADAPTER        =
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           =
*     ES_EXIT_CAUSED_BY_USER            =
    TABLES
      T_OUTTAB                          =  GT_DATA[]
*   EXCEPTIONS
*     PROGRAM_ERROR                     = 1
*     OTHERS                            = 2
            .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.                    "ALV_SHOW




*&---------------------------------------------------------------------*
*&      Form  SET_PF_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
  SET PF-STATUS STANDARD.
ENDFORM.                    "SET_PF_STATUS



*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->UCOMM      text
*      -->FILECAT    text
*----------------------------------------------------------------------*
FORM USER_COMMAND   USING UCOMM TYPE SY-UCOMM
                          FILECAT TYPE SLIS_SELFIELD.
case ucomm.
  when /&PRINT.
    MESSAGE S000(Z001) with 你点击了打印.
  endcase.

ENDFORM.                    "USER_COMMAND

 

function ALV

标签:

原文地址:http://www.cnblogs.com/charles-guo/p/4185228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!