其他分享
首页 > 其他分享> > ABAP ALV 變色

ABAP ALV 變色

作者:互联网

REPORT Z_020.
TABLES: EKKO.

TYPE-POOLS: SLIS.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF T_EKKO,
  EBELN TYPE EKPO-EBELN,
  EBELP TYPE EKPO-EBELP,
  STATU TYPE EKPO-STATU,
  AEDAT TYPE EKPO-AEDAT,
  MATNR TYPE EKPO-MATNR,
  MENGE TYPE EKPO-MENGE,
  MEINS TYPE EKPO-MEINS,
  NETPR TYPE EKPO-NETPR,
  PEINH TYPE EKPO-PEINH,
  CELLCOLOR TYPE LVC_T_SCOL,

 END OF T_EKKO.

DATA: IT_EKKO TYPE STANDARD TABLE OF T_EKKO INITIAL SIZE 0,
      WA_EKKO TYPE T_EKKO.

*ALV data declarations
DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
      GD_TAB_GROUP TYPE SLIS_T_SP_GROUP_ALV,
      GD_LAYOUT    TYPE SLIS_LAYOUT_ALV,
      GD_REPID     LIKE SY-REPID,
      GT_EVENTS     TYPE SLIS_T_EVENT,
      GD_PRNTPARAMS TYPE SLIS_PRINT_ALV.


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  PERFORM DATA_RETRIEVAL.
  PERFORM BUILD_FIELDCATALOG.
  PERFORM BUILD_LAYOUT.
  PERFORM SET_CELL_COLOURS.
  PERFORM DISPLAY_ALV_REPORT.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM BUILD_FIELDCATALOG.

* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you  more control of the final product.

* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
*               I.e. Field type may be required in-order for
*                    the 'TOTAL' function to work.

  FIELDCATALOG-FIELDNAME   = 'EBELN'.
  FIELDCATALOG-SELTEXT_M   = 'Purchase Order'.
  FIELDCATALOG-COL_POS     = 0.
  FIELDCATALOG-OUTPUTLEN   = 10.
  FIELDCATALOG-EMPHASIZE   = 'X'.
  FIELDCATALOG-KEY         = 'X'.
*  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = 'X'.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'EBELP'.
  FIELDCATALOG-SELTEXT_M   = 'PO Item'.
  FIELDCATALOG-COL_POS     = 1.
  FIELDCATALOG-DO_SUM      = 'X'.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'STATU'.
  FIELDCATALOG-SELTEXT_M   = 'Status'.
  FIELDCATALOG-COL_POS     = 2.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'AEDAT'.
  FIELDCATALOG-SELTEXT_M   = 'Item change date'.
  FIELDCATALOG-COL_POS     = 3.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MATNR'.
  FIELDCATALOG-SELTEXT_M   = 'Material Number'.
  FIELDCATALOG-COL_POS     = 4.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MENGE'.
  FIELDCATALOG-SELTEXT_M   = 'PO quantity'.
  FIELDCATALOG-COL_POS     = 5.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'MEINS'.
  FIELDCATALOG-SELTEXT_M   = 'Order Unit'.
  FIELDCATALOG-COL_POS     = 6.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'NETPR'.
  FIELDCATALOG-SELTEXT_M   = 'Net Price'.
  FIELDCATALOG-COL_POS     = 7.
  FIELDCATALOG-OUTPUTLEN   = 15.
  FIELDCATALOG-DO_SUM      = 'X'.
  FIELDCATALOG-DATATYPE     = 'CURR'.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'PEINH'.
  FIELDCATALOG-SELTEXT_M   = 'Price Unit'.
  FIELDCATALOG-COL_POS     = 8.
  APPEND FIELDCATALOG TO FIELDCATALOG.
  CLEAR  FIELDCATALOG.
ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM BUILD_LAYOUT.
  GD_LAYOUT-NO_INPUT          = 'X'.
  GD_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  GD_LAYOUT-TOTALS_TEXT       = 'Totals'(201).
  GD_LAYOUT-COLTAB_FIELDNAME = 'CELLCOLOR'.  "CTAB_FNAME
ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT.
  GD_REPID = SY-REPID.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            I_CALLBACK_PROGRAM      = GD_REPID
            I_CALLBACK_TOP_OF_PAGE   = 'TOP-OF-PAGE'  "see FORM
*            i_callback_user_command = 'USER_COMMAND'
*            i_grid_title           = outtext
            IS_LAYOUT               = GD_LAYOUT
            IT_FIELDCAT             = FIELDCATALOG[]
*            it_special_groups       = gd_tabgroup
*           it_events               = gt_events
*           is_print                = gd_prntparams
            I_SAVE                  = 'X'
*            is_variant              = z_template
       TABLES
            T_OUTTAB                = IT_EKKO
       EXCEPTIONS
            PROGRAM_ERROR           = 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.
ENDFORM.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM DATA_RETRIEVAL.

  SELECT EBELN EBELP STATU AEDAT MATNR MENGE MEINS NETPR PEINH
   UP TO 30 ROWS
    FROM EKPO
    INTO CORRESPONDING FIELDS OF TABLE IT_EKKO.
ENDFORM.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM TOP-OF-PAGE.
*ALV Header declarations
  DATA: T_HEADER TYPE SLIS_T_LISTHEADER,
        WA_HEADER TYPE SLIS_LISTHEADER,
        T_LINE LIKE WA_HEADER-INFO,
        LD_LINES TYPE I,
        LD_LINESC(10) TYPE C.

* Title
*  wa_header-typ  = 'H'.
*  wa_header-info = 'EKKO Table Report'.
*  append wa_header to t_header.
*  clear wa_header.

* Date
*  wa_header-typ  = 'S'.
*  wa_header-key = 'Date: '.
*  CONCATENATE  sy-datum+6(2) '.'
*               sy-datum+4(2) '.'
*               sy-datum(4) INTO wa_header-info.   "todays date
*  append wa_header to t_header.
*  clear: wa_header.

* Total No. of Records Selected
  DESCRIBE TABLE IT_EKKO LINES LD_LINES.
  LD_LINESC = LD_LINES.
  CONCATENATE 'Total No. of Records Selected: ' LD_LINESC
                    INTO T_LINE SEPARATED BY SPACE.
  WA_HEADER-TYP  = 'A'.
  WA_HEADER-INFO = T_LINE.
  APPEND WA_HEADER TO T_HEADER.
  CLEAR: WA_HEADER, T_LINE.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY = T_HEADER.
*            i_logo             = 'Z_LOGO'.
ENDFORM.                    "top-of-page


*&---------------------------------------------------------------------*
*&      Form  SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*       Set colour of individual ALV cell, field
*----------------------------------------------------------------------*
FORM SET_CELL_COLOURS .
  DATA: WA_CELLCOLOR TYPE LVC_S_SCOL.
  DATA: LD_INDEX TYPE SY-TABIX.

  LOOP AT IT_EKKO INTO WA_EKKO.
    LD_INDEX = SY-TABIX.

*   Set colour of EBELN field to various colors based on sy-tabix value
    WA_CELLCOLOR-FNAME = 'EBELN'.
    WA_CELLCOLOR-COLOR-COL = SY-TABIX.  "color code 1-7, if outside rage defaults to 7
    WA_CELLCOLOR-COLOR-INT = '0'.  "1 = Intensified on, 0 = Intensified off
    WA_CELLCOLOR-COLOR-INV = '0'.  "1 = text colour, 0 = background colour
    APPEND WA_CELLCOLOR TO WA_EKKO-CELLCOLOR.
    MODIFY IT_EKKO FROM WA_EKKO INDEX LD_INDEX TRANSPORTING CELLCOLOR.

*   Set colour of NETPR field to color 4 if gt 0
    IF WA_EKKO-NETPR GT 0.
      WA_CELLCOLOR-FNAME = 'NETPR'.
      WA_CELLCOLOR-COLOR-COL = 6.  "color code 1-7, if outside rage defaults to 7
      WA_CELLCOLOR-COLOR-INT = '0'.  "1 = Intensified on, 0 = Intensified off
      WA_CELLCOLOR-COLOR-INV = '0'.  "1 = text colour, 0 = background colour
      APPEND WA_CELLCOLOR TO WA_EKKO-CELLCOLOR.
      MODIFY IT_EKKO FROM WA_EKKO INDEX LD_INDEX TRANSPORTING CELLCOLOR.
    ENDIF.

*   Set colour of AEDAT field text to red(6)
*    WA_CELLCOLOR-FNAME = 'AEDAT'.
*    WA_CELLCOLOR-COLOR-COL = 6.  "color code 1-7, if outside rage defaults to 7
*    WA_CELLCOLOR-COLOR-INT = '0'.  "1 = Intensified on, 0 = Intensified off
*    WA_CELLCOLOR-COLOR-INV = '1'.  "1 = text colour, 0 = background colour
*    APPEND WA_CELLCOLOR TO wa_ekko-CELLCOLOR.
*    MODIFY it_ekko from wa_ekko INDEX ld_index TRANSPORTING CELLCOLOR.
  ENDLOOP.

ENDFORM.                    " SET_CELL_COLOURS

标签:WA,FIELDCATALOG,變色,CELLCOLOR,ABAP,ALV,TYPE,EKKO
来源: https://blog.csdn.net/xushu2016/article/details/112665359