SAP 给ALV加标题

给ALV显示时加个标题:

首先我们去定义要显示的标题:



 

 然后再在Call Function ALV时,有两种方式触发FORM top-of-page.

 

 完整代码如下:

REPORT zcaogao6.
TYPE-POOLS:slis.
TABLES:mast,makt.

DATA:t_mast TYPE TABLE OF mast.
DATA:w_mast TYPE  mast.

TYPES:BEGIN OF t_title,
  werks TYPE aufk-werks,
  name1 TYPE t001w-name1,
  END OF t_title.

DATA :w_title TYPE t_title.
DATA: t_listheader TYPE slis_t_listheader.

*alv
DATA:gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA:gs_layout TYPE slis_layout_alv.
DATA: git_events TYPE slis_t_event.
DATA:g_repid LIKE sy-repid VALUE sy-repid.
DATA:gd_prntparams TYPE slis_print_alv.

DEFINE field_frame.
  gt_fieldcat-fieldname = &1.
  gt_fieldcat-seltext_m = &2.
  gt_fieldcat-seltext_s = &2.
  gt_fieldcat-seltext_l = &2.
  append gt_fieldcat.
  clear gt_fieldcat.
END-OF-DEFINITION.

SELECTION-SCREEN:BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS p_werks TYPE mast-werks OBLIGATORY.
SELECT-OPTIONS: s_matnr FOR mast-matnr.
SELECTION-SCREEN:END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.


START-OF-SELECTION.

  PERFORM f_getdata.
  PERFORM f_comment_build.

END-OF-SELECTION.

  PERFORM f_outdata.
*&---------------------------------------------------------------------*
*&      Form  F_GETDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_getdata .
  w_title-werks = p_werks.

  SELECT SINGLE name1 FROM t001w
    INTO w_title-name1
    WHERE werks = w_title-werks
      AND spras = sy-langu.
  SELECT * INTO TABLE t_mast FROM mast
      WHERE werks = p_werks
      AND matnr IN s_matnr.
ENDFORM.                    " F_GETDATA
*&---------------------------------------------------------------------*
*&      Form  F_COMMENT_BUILD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_comment_build .
*ALV表头
  IF sy-langu = '1'.
    PERFORM f_build_title USING '测试标题' ' '.
    PERFORM f_build_title USING '工厂代码:'     w_title-werks.
    PERFORM f_build_title USING '工厂名称:'     w_title-name1.
  ELSE.
    PERFORM f_build_title USING 'Test title' ' '.
    PERFORM f_build_title USING 'Plant:'        w_title-werks.
    PERFORM f_build_title USING 'Plant Name:'     w_title-name1.
  ENDIF.
ENDFORM.                    " F_COMMENT_BUILD
*&---------------------------------------------------------------------*
*&      Form  F_BUILD_TITLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0100   text
*      -->P_0101   text
*----------------------------------------------------------------------*
FORM f_build_title  USING    value(l_title1)
                             value(l_title2).
*alv表头
  DATA: l_listheader TYPE slis_listheader,
        l_title(60).

  CONCATENATE l_title1 l_title2 INTO l_title.
  l_listheader-info = l_title.
  l_listheader-typ  = 'S'."M中号字体,H大号字体,S小号字体
  APPEND l_listheader TO t_listheader.
  CLEAR : l_title,
          l_title1,
          l_title2,
          l_listheader.
ENDFORM.                    " F_BUILD_TITLE
*&---------------------------------------------------------------------*
*&      Form  F_OUTDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_outdata .
  PERFORM f_get_event.
  PERFORM f_layout.
  PERFORM f_fieldcat.
  PERFORM f_call_alv.
ENDFORM.                    " F_OUTDATA
*&---------------------------------------------------------------------*
*&      Form  F_GET_EVENT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_get_event .
  DATA: formname_top_of_page TYPE slis_formname VALUE 'TOP-OF-PAGE'.
  DATA l_events TYPE slis_alv_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.
  IF sy-subrc = 0.
    MOVE formname_top_of_page TO l_events-form.
    MODIFY git_events FROM l_events INDEX sy-tabix.
  ENDIF.
ENDFORM.                    " F_GET_EVENT
*&---------------------------------------------------------------------*
*&      Form  F_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_layout .
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-zebra = 'X'.
*  gs_layout-box_fieldname = 'BOX'.
ENDFORM.                    " F_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  F_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_fieldcat .
  PERFORM f_init_fieldcat USING 'WERKS' '工厂'.
  PERFORM f_init_fieldcat USING 'MATNR' '主件料号'.
ENDFORM.                    " F_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  F_CALL_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_call_alv .
  g_repid = sy-repid.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = g_repid
*      i_callback_pf_status_set = 'F_STATUS'
*      i_callback_user_command  = 'F_COMMAND'
*      i_callback_top_of_page   = 'TOP-OF-PAGE' "第一种 直接调用 FORM top-of-page.
      is_layout                = gs_layout
      it_fieldcat              = gt_fieldcat[]
      i_save                   = 'A'
      it_events                = git_events[] "第二种 通过触发事件调用FORM top-of-page.
*      is_print                 = gd_prntparams
    TABLES
      t_outtab                 = t_mast[]
    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.                    " F_CALL_ALV
*&---------------------------------------------------------------------*
*&      Form  F_INIT_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0301   text
*      -->P_0302   text
*----------------------------------------------------------------------*
FORM f_init_fieldcat USING p_fieldname p_seltext_s.
  field_frame p_fieldname p_seltext_s.
ENDFORM.                    " frm_init_fieldcat
*&---------------------------------------------------------------------*
*&      Form  top-of-page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top-of-page.
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = t_listheader.
ENDFORM.                    "top-of-page

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值