随笔- 20  评论- 3  文章- 0 

ALV简单模板1

*&---------------------------------------------------------------------*
*& Report  Temp_ALV
*&
*&---------------------------------------------------------------------*
*&ALV模板代码
*&
*&
*&---------------------------------------------------------------------*

REPORT  Temp_ALV.
TYPE-POOLS: slis.
************************************************************************
*TABLES
************************************************************************
TABLES:mara.

************************************************************************
*internal table
************************************************************************
DATA:BEGIN OF i_list OCCURS 0,
      matnr LIKE mara-matnr,
      vpsta LIKE mara-vpsta,
      laeng LIKE mara-laeng,
     END OF i_list.

************************************************************************
*ALV data type
************************************************************************
DATA: l_repid LIKE sy-repid.
DATA: wa_fieldcat TYPE slis_fieldcat_alv,
      gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: g_list_top_of_page TYPE slis_t_listheader.
CONSTANTS:c_formname_top_of_page TYPE slis_formname VALUE 'TOP_OF_PAGE'.
l_repid = sy-repid.

************************************************************************
*SELECT-SRECCN
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-bl1.

SELECT-OPTIONS: zmatnr FOR mara-matnr.
SELECTION-SCREEN END OF BLOCK bl1.

************************************************************************
*SELECT-OF-SELECTION
************************************************************************
START-OF-SELECTION.
  PERFORM sub_get_data.
  IF sy-subrc <> 0 .
    MESSAGE '您查找的数据未在数据库中找到,请重新输入!' TYPE 'I' .
  ELSE.
*    PERFORM sub_process_data .
    PERFORM sub_display_data .
  ENDIF.
*************************************************************************
**END-OF-SELECTION
*************************************************************************
END-OF-SELECTION .
**&--------------------------------------------------------------------*
**&      Form  top_of_page 显示标题用的
**&--------------------------------------------------------------------*
FORM top_of_page. "显示标题用的

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = g_list_top_of_page.

ENDFORM.                    " top_of_page

*&---------------------------------------------------------------------*
*&      Form  sub_get_data 取得数据
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub_get_data.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE i_list FROM mara WHERE matnr IN zmatnr.

ENDFORM.                    "sub_get_data

*&---------------------------------------------------------------------*
*&      Form  sub_process_data 处理数据
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub_process_data.

ENDFORM.                    "sub_process_data

*&---------------------------------------------------------------------*
*&      Form  sub_display_data 显示数据
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub_display_data.
******initial datatab
  DATA: l_i_output LIKE i_list OCCURS 0 WITH HEADER LINE.  "i_list读取数据表

*****initial title
  PERFORM build_comment_summary TABLES l_i_output
    USING g_list_top_of_page[].

****initial field category
  DATA: l_i_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
  PERFORM init_alv_summary TABLES l_i_fieldcat.
****initial others
  DATA: l_s_layout TYPE slis_layout_alv.
  l_s_layout-colwidth_optimize = 'X'."字符适合宽度
*  l_s_layout-zebra = 'XXX'."行的颜色

****initial event
  DATA:l_events TYPE slis_t_event.
  PERFORM build_eventtab USING l_events[].

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = l_repid
      i_default          = 'X'
      i_save             = 'X'
*     i_layout-f2code    = ' '
      i_callback_user_command  =  'USER_COMMAND'
      it_events          = l_events[]
      is_layout          = l_s_layout
      it_fieldcat        = l_i_fieldcat[]
    TABLES
      t_outtab           = i_list"读取数据的内表,显示在alv中
    EXCEPTIONS
      program_error = 1
      other = 2 .

ENDFORM.                    "sub_display_data
*&---------------------------------------------------------------------*
*&      Form  build_eventtab
*&---------------------------------------------------------------------*
FORM build_eventtab USING v_events TYPE slis_t_event.

  DATA: l_event TYPE slis_alv_event.
  "Returns table of possible events for a list type

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = v_events.            "List type (0,1,2,3)

  DATA: l_tabix LIKE sy-tabix.
  CLEAR l_event-form.
  READ TABLE v_events WITH KEY name = slis_ev_top_of_page
        INTO l_event.

  l_tabix = sy-tabix.
  IF sy-subrc = 0.
    MOVE c_formname_top_of_page TO l_event-form.
    MODIFY v_events FROM l_event INDEX l_tabix.
  ENDIF.

ENDFORM.                    " build_eventtab

*&---------------------------------------------------------------------*
*&      Form  build_comment_summary
*&---------------------------------------------------------------------*
FORM build_comment_summary TABLES v_i_output STRUCTURE i_list   "读取数据的内表
                     USING v_list_top_of_page TYPE slis_t_listheader.

  DATA: l_line TYPE slis_listheader.
  CLEAR l_line.
  l_line-typ  = 'H'.
  l_line-key  = ''.
  l_line-info = '我的测试表'.
  APPEND l_line TO v_list_top_of_page.
ENDFORM.                    " build_comment_summary

*&---------------------------------------------------------------------*
*&      Form  init_alv_summary
*&---------------------------------------------------------------------*
FORM init_alv_summary TABLES v_i_fieldcat TYPE slis_t_fieldcat_alv.

  REFRESH v_i_fieldcat.
  CLEAR v_i_fieldcat.
  v_i_fieldcat-fieldname     = 'MATNR'.
  v_i_fieldcat-tabname       = 'I_LIST'.
  v_i_fieldcat-seltext_m     = '物料号'.
  v_i_fieldcat-key           = 'X'.
*  v_i_fieldcat-ref_fieldname = 'MATNR'. "收索帮助
*  v_i_fieldcat-ref_tabname   = 'MAKT'.
  APPEND v_i_fieldcat.

  CLEAR v_i_fieldcat.
  v_i_fieldcat-fieldname     = 'VPSTA'.
  v_i_fieldcat-tabname       = 'I_LIST'.
  v_i_fieldcat-seltext_m     = '维护全部物料状态'.
*    v_i_fieldcat-outputlen     = '20'."列的字符宽度
*    v_i_fieldcat-edit          = 'X'."可编辑的属性
  APPEND v_i_fieldcat.

  CLEAR v_i_fieldcat.
*  v_i_fieldcat-decimals_out = '0'.  "去掉该字段小数点后的0
*  v_i_fieldcat-EMPHASIZE  = 'C700'. "给该字段加上颜色
*  v_i_fieldcat-just = 'L'.  "对齐方式

  v_i_fieldcat-fieldname     = 'LAENG'.
  v_i_fieldcat-tabname       = 'I_LIST'.
  v_i_fieldcat-seltext_m     = '长度'.

*  v_i_fieldcat-do_sum = 'X'.  "总计该列的值
  APPEND v_i_fieldcat.
ENDFORM.                    " init_alv_summary
*&--------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->UCOMM      text
*     -->SELFIELD   text
*---------------------------------------------------------------------*
FORM user_command USING ucomm LIKE sy-ucomm               "接收用户的命令
  selfield TYPE slis_selfield.
  READ TABLE i_list INDEX selfield-tabindex.
  CHECK sy-subrc = 0.
  CASE ucomm.
    WHEN '&IC1'.  "&DATA_SAVE响应保存键
      CASE selfield-sel_tab_field.
        WHEN  'I_LIST-MATNR'. "这里必须大写
          SET PARAMETER ID 'MAT' FIELD i_list-matnr .          "参数id为aun
          CALL TRANSACTION  'VA03' AND  SKIP  FIRST  SCREEN."调用事务位va03的事务且跳过第一个屏幕
**************************************显示第二层ALV********************************
*        WHEN 'I_LIST-VPSTA'.
*          LOOP AT I_LIST INTO I_LIST WHERE VPSTA = I_LIST-VPSTA.
*          ENDLOOP.
*          PERFORM frm_buildfieldcat_lips.
*          PERFORM frm_display_lips.
      ENDCASE.
  ENDCASE.
ENDFORM.                    "USER_COMMAND

*&---------------------------------------------------------------------*
*&      Form  frm_buildfieldcat_lips
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_buildfieldcat_lips .
  CLEAR wa_fieldcat.
  REFRESH gt_fieldcat[].
  DEFINE add_field.  "定义宏
    wa_fieldcat-fieldname = &1.
    wa_fieldcat-reptext_ddic = &2.
    wa_fieldcat-hotspot = &3.
    append wa_fieldcat to gt_fieldcat.
  END-OF-DEFINITION.

  add_field 'MATNR' '物料号' 'X'.
  add_field 'VPSTA' '描述' ' ' .
  add_field 'LAENG' '长度' ' ' .
ENDFORM.                    " frm_buildfieldcat_ekpo
**&---------------------------------------------------------------------*
**&      Form  frm_display_lips
**&---------------------------------------------------------------------*
**       text
**----------------------------------------------------------------------*
**  -->  p1        text
**  <--  p2        text
**----------------------------------------------------------------------*
FORM frm_display_lips .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program    = l_repid
      it_fieldcat           = gt_fieldcat
      i_screen_start_column = 20
      i_screen_start_line   = 30
      i_screen_end_column   = 80
      i_screen_end_line     = 40
    TABLES
      t_outtab              = i_list."第二层alv显示数据的内表

ENDFORM.                    " frm_display_lips

posted on 2012-08-15 12:27  reagan  阅读(...)  评论(...编辑  收藏