2011年3月30日 星期三

[Code] Quick Reference for ALV_LIST report.

快速建立 ALV_LIST 報表
以下分為幾個段落,可以快速的建立 ALV_LIST 的報表。

Define

TYPE-POOLS: slis.

* Global variables for handling ALV functionality
CONSTANTS: alv_status   TYPE slis_formname VALUE 'SET_STATUS',
           alv_ucommd   TYPE slis_formname VALUE 'USER_COMMAND'.
DATA:      alv_repid    LIKE sy-repid,
           alv_field    TYPE slis_fieldcat_alv,
           alv_event    TYPE slis_alv_event,
           alv_layout   TYPE slis_layout_alv,
           alv_sort     TYPE slis_sortinfo_alv,
           alv_print    TYPE slis_print_alv,
           alv_t_field  TYPE slis_t_fieldcat_alv,
           alv_t_events TYPE slis_t_event,
           alv_t_sort   TYPE slis_t_sortinfo_alv.

INITIALIZATION

*----------------
* INITIALIZATION
*----------------
INITIALIZATION.
  alv_repid = sy-repid.

Data_Print

報表顯示的資料請放在 gt_itab 的 Internal table 裡面。
FORM data_print.
  PERFORM alvevent.
  PERFORM alvfield.
  PERFORM alvsort.

  alv_layout-colwidth_optimize = 'X'.
  alv_layout-get_selinfos      = 'X'.
  alv_layout-zebra             = 'X'.
*  alv_layout-numc_sum          = 'X'
*  alv_layout-box_fieldname     = 'SELEC'.
*  alv_layout-box_tabname       = 'GT_VBRK'.
*  alv_layout-f2code            = '&F2C'.
*  alv_layout-no_totalline      = 'X'.
*  alv_layout-group_change_edit = 'X'.

  alv_print-no_print_listinfos = 'X'.
  alv_print-no_print_selinfos  = 'X'.
  alv_print-no_coverpage       = 'X'.

  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      i_callback_program       = alv_repid
      i_callback_pf_status_set = alv_status
      i_callback_user_command  = alv_ucommd
      it_fieldcat              = alv_t_field
      i_save                   = 'A'
      it_events                = alv_t_events
      is_layout                = alv_layout
      it_sort                  = alv_t_sort
      is_print                 = alv_print
    TABLES
      t_outtab                 = gt_itab
    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.

ALV_EVENT

*&---------------------------------------------------------------------*
*&      Form  alvevent
*&---------------------------------------------------------------------*
*       ALV Event
*----------------------------------------------------------------------*
FORM alvevent .
  CLEAR alv_event.
  alv_event-name = slis_ev_top_of_page.
  alv_event-form = 'LIST_HEADER'.
  APPEND alv_event TO alv_t_events.
ENDFORM.                    "alvevent

ALV_FIELDS

Fields 也有 Function 可以直接建立,只是手動建立的自主性較高。
*&---------------------------------------------------------------------*
*&      Form  alvfield
*&---------------------------------------------------------------------*
*       ALV Fields
*----------------------------------------------------------------------*
FORM alvfield .
  DATA: l_count TYPE i.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'FKDAT'.
  alv_field-seltext_s     = 'DATE'.
  alv_field-key           = 'X'.             " define field as key
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'FKIMG'.
  alv_field-seltext_s     = 'Quantity'.
  alv_field-decimals_out  = '0'.
  alv_field-do_sum        = 'X'.
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'AMOU0'.
  alv_field-seltext_s     = 'Amount'.
  alv_field-currency      = 'TWD'.
  " alv_field-cfieldname    = 'WAERK'.
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'VRKME'.
  alv_field-seltext_s     = 'UoM'.
  alv_field-ref_tabname   'T006'.      " Reference Table
  alv_field-ref_fieldname 'MSEHI'.     " Reference Field
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'VKBUR'.
  alv_field-seltext_s     = 'OFFICE'.
  alv_field-no_out        = 'X'.          " No Output direct
  APPEND alv_field TO alv_t_field.
*
* .... Enter ALL of your fields at here.

ENDFORM.                    "alvfield
用 Function 建立的範例:
DATA: lv_tabix LIKE sy-tabix.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
  EXPORTING
    i_program_name         = alv_repid
    i_internal_tabname     = 'GT_ITAB'
    i_inclname             = alv_repid
  CHANGING
    ct_fieldcat            = alv_t_field
  EXCEPTIONS
    inconsistent_interface = 1
    program_error          = 2
    OTHERS                 = 3.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

LOOP AT alv_t_field INTO alv_field.
  lv_tabix = sy-tabix.
  CASE alv_field-fieldname.
    WHEN 'FKDAT'.
      alv_field-seltext_s     = 'DATE'.
      alv_field-ddictxt       = 'S'.
    WHEN 'FKIMG'.
      alv_field-seltext_s     = 'Quantity'.
      alv_field-ddictxt       = 'S'.
      alv_field-decimals_out  = '0'.
      alv_field-do_sum        = 'X'.
    WHEN 'AMOU0'.
      alv_field-seltext_s     = 'Amount'.
      alv_field-ddictxt       = 'S'.
      alv_field-currency      = 'TWD'.
    WHEN 'VKBUR'.
      alv_field-seltext_s     = 'OFFICE'.
      alv_field-ddictxt       = 'S'.
      alv_field-no_out        = 'X'.          " No Output direct
  ENDCASE.
  MODIFY alv_t_field FROM alv_field INDEX lv_tabix.
ENDLOOP.

ALV_SORT

*&---------------------------------------------------------------------*
*&      Form  alvsort
*&---------------------------------------------------------------------*
*       ALV Sort
*----------------------------------------------------------------------*
FORM alvsort .
  DATA lv_counter TYPE i.

  CLEAR alv_sort.
  lv_counter         = lv_counter + 1.
  alv_sort-spos      = lv_counter.
  alv_sort-fieldname = 'KUNSP'.
  alv_sort-tabname   = 'GT_ITAB'.
  alv_sort-subtot    = 'X'.
  alv_sort-up        = 'X'.
  alv_sort-group     = 'UL'.       " UL : Split by Line,
                                   " *  : Split by Page.
  APPEND alv_sort TO alv_t_sort.

  CLEAR alv_sort.
  lv_counter         = lv_counter + 1.
  alv_sort-spos      = lv_counter.
  alv_sort-fieldname = 'FKDAT'.
  alv_sort-tabname   = 'GT_ITAB'.
  alv_sort-up        = 'X'.
  alv_sort-group     = ' '.
  APPEND alv_sort TO alv_t_sort.

ENDFORM.                    "alvsort

Set_Status

*&---------------------------------------------------------------------*
*&      Form  set_status
*&---------------------------------------------------------------------*
*       Set Status
*----------------------------------------------------------------------*
FORM set_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD' EXCLUDING rt_extab.
ENDFORM.                    "set_status

User_Command

*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM user_command USING i_ucomm     TYPE syucomm
                         is_selfield TYPE slis_selfield.    "#EC CALLED
  DATA: lv_datum LIKE sy-datum.

  CASE i_ucomm.
    WHEN '&IC1'.             " double click
      READ TABLE gt_itab INDEX is_selfield-tabindex.
      SET PARAMETER ID 'VF'   FIELD gt_itab-vbeln.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
  ENDCASE.

ENDFORM.                               " USER_COMMAND

List_Header

*&---------------------------------------------------------------------*
*&      Form  list_header
*&---------------------------------------------------------------------*
*       List Header
*----------------------------------------------------------------------*
FORM list_header.
  DATA: lv_linsz LIKE sy-linsz.
  lv_linsz = sy-linsz - 20.
  WRITE:   /001(sy-linsz)  'XXX sheet'  CENTERED,
           /001            'Date :'  ,sy-datum.
  WRITE AT: lv_linsz       'Page No :'  ,(4) sy-pagno.
  ULINE.
ENDFORM.                    "list_header

沒有留言:

張貼留言