2020.03.08 【ABAP随笔】- 简单ALV报表(模板)

今天写一个简单的ALV报表吧,仅显示的那种,这是前两天刚入门的ABAP小伙伴的小考题,

需求是这样的:

需求描述:

1.选择屏幕为:

 2020.03.08 【ABAP随笔】- 简单ALV报表(模板)_第1张图片

勾选时,才显示销售凭证的选项

2.展示订单,行项目,订单类型,物料号等信息的ALV展示

3.单击销售订单跳转到VA03查询页面

4.当订单数目小于10时,用红色标注

 2020.03.08 【ABAP随笔】- 简单ALV报表(模板)_第2张图片

 

代码如下:

*&---------------------------------------------------------------------*
*& Report ZALV_SIMPLE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zalv_simple.

TABLES:vbak.

TYPES:BEGIN OF gty_data,
        vbeln  TYPE vbak-vbeln,
        posnr  TYPE vbap-posnr,
        auart  TYPE vbak-auart,
        matnr  TYPE vbap-matnr,
        maktx  TYPE makt-maktx,
        kwmeng TYPE vbap-kwmeng,
        scol   TYPE lvc_t_scol, "用来控制单元格颜色
      END OF gty_data.

DATA gs_fieldcat TYPE lvc_s_fcat.
DATA gt_fieldcat TYPE lvc_t_fcat.
DATA gs_layout TYPE lvc_s_layo.

DATA gs_data TYPE gty_data.
DATA gt_data TYPE TABLE OF gty_data.

PARAMETERS p_auart TYPE vbak-auart .

PARAMETERS p_vkorg TYPE vbak-vkorg .

SELECT-OPTIONS:s_erdat FOR vbak-erdat.

PARAMETERS p_cbox AS CHECKBOX USER-COMMAND uc1.

SELECT-OPTIONS:s_vbeln FOR vbak-vbeln MODIF ID md1.

INITIALIZATION.


AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-name = 'P_AUART' OR screen-name = 'P_VKORG'.
      screen-required = '2'.
      MODIFY SCREEN.
    ENDIF.
    IF screen-group1 = 'MD1'.
      IF p_cbox EQ 'X'.
        screen-active = '1'.
      ELSE.
        screen-active = '0'.
      ENDIF.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.


START-OF-SELECTION.
  IF p_auart IS INITIAL OR p_vkorg IS INITIAL.
    MESSAGE '请输入必填项' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.
  PERFORM frm_get_data.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  DATA ls_scol TYPE lvc_s_scol."控制颜色的结构

  IF p_cbox IS INITIAL.
    CLEAR s_vbeln[].
  ENDIF.

  "抓取销售订单,行项目,类型,物料,物料描述,订单数量
  SELECT a~vbeln,b~posnr,a~auart,b~matnr,c~maktx,b~kwmeng INTO TABLE @DATA(lt_data)
    FROM vbak AS a
    INNER JOIN vbap AS b ON b~vbeln = a~vbeln
    INNER JOIN makt AS c ON c~matnr = b~matnr
  WHERE a~auart = @p_auart
    AND a~vkorg = @p_vkorg
    AND a~erdat IN @s_erdat
    AND a~vbeln IN @s_vbeln
    AND c~spras = @sy-langu.
  IF sy-subrc EQ 0.

    MOVE-CORRESPONDING lt_data[] TO gt_data[].

    LOOP AT gt_data ASSIGNING FIELD-SYMBOL() WHERE kwmeng < 10.
      CLEAR ls_scol.
      ls_scol-fname = 'KWMENG'.
      ls_scol-color-col = '6'."6:为红色 1:强化  0:颜色为背景
      ls_scol-color-int = '1'."6:为红色 1:强化  0:颜色为背景
      ls_scol-color-inv = '0'."6:为红色 1:强化  0:颜色为背景

      APPEND ls_scol TO -scol.
    ENDLOOP.

    gs_layout-sel_mode    = 'D' ."模式
    gs_layout-zebra = 'X'. "ALV表格按斑马线条纹显示
    gs_layout-cwidth_opt = 'X'. "将ALV字段宽度设置为最优化
    gs_layout-ctab_fname = 'SCOL'."ALV 控制: 带有复杂单元格颜色编码的字段名称

    PERFORM frm_build_cat.

    PERFORM frm_display.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_BUILD_CAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_build_cat .

  DATA lv_pos TYPE i.
  DATA ls_fieldcat LIKE LINE OF gt_fieldcat.
  DEFINE %%append_fieldcat.
    lv_pos = lv_pos + 1.
    ls_fieldcat-col_pos = lv_pos.
    ls_fieldcat-fieldname = &1."字段名
    ls_fieldcat-coltext = &2."文本
    ls_fieldcat-key = &3."关键字
    ls_fieldcat-ref_table = &4."参考的表
    ls_fieldcat-ref_field = &5."参考的字段
  APPEND ls_fieldcat TO gt_fieldcat.
  CLEAR ls_fieldcat.
  END-OF-DEFINITION.
  %%append_fieldcat:
  'VBELN' '' 'X' 'VBAK' 'VBELN',
  'POSNR' '' 'X' 'VBAP' 'POSNR',
  'AUART' '' '' 'VBAK' 'AUART',
  'MATNR' '' '' 'VBAP' 'MATNR',
  'MAKTX' '' '' 'MAKT' 'MAKTX',
  'KWMENG' '' '' 'VBAP' 'KWMENG'.

  READ TABLE gt_fieldcat ASSIGNING FIELD-SYMBOL() INDEX 1.
  IF sy-subrc EQ 0.
    -hotspot = 'X'."激活热点,单机跳转功能
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_display .

  "调用FM 显示报表
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*     I_INTERFACE_CHECK        = ' '
*     I_BYPASSING_BUFFER       =
*     I_BUFFER_ACTIVE          =
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'FRM_SET_STATUS' "状态栏form
      i_callback_user_command  = 'FRM_ALV_USER_COMMAND' "用户命令form
*     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_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
*     IT_EXCLUDING             =
*     IT_SPECIAL_GROUPS_LVC    =
*     IT_SORT_LVC              =
*     IT_FILTER_LVC            =
*     IT_HYPERLINK             =
*     IS_SEL_HIDE              =
*     I_DEFAULT                = 'X'
*     I_SAVE                   = ' '
*     IS_VARIANT               =
*     IT_EVENTS                =
*     IT_EVENT_EXIT            =
*     IS_PRINT_LVC             =
*     IS_REPREP_ID_LVC         =
*     I_SCREEN_START_COLUMN    = 0
*     I_SCREEN_START_LINE      = 0
*     I_SCREEN_END_COLUMN      = 0
*     I_SCREEN_END_LINE        = 0
*     I_HTML_HEIGHT_TOP        =
*     I_HTML_HEIGHT_END        =
*     IT_ALV_GRAPHICS          =
*     IT_EXCEPT_QINFO_LVC      =
*     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.
FORM frm_set_status USING p_extab TYPE slis_t_extab.

  SET PF-STATUS 'ZSTANDARD_FULLSCREEN' . "se41 从程序saplkkbl  状态STANDARD_FULLSCREEN 复制

ENDFORM.                    " frm_set_status

FORM frm_alv_user_command USING p_ucomm        TYPE sy-ucomm
                                p_rs_selfield  TYPE slis_selfield.
  CASE p_ucomm.
    WHEN '&IC1'."单击
      READ TABLE gt_data INTO DATA(ls_data) INDEX p_rs_selfield-tabindex.
      IF sy-subrc EQ 0 AND p_rs_selfield-fieldname = 'VBELN'.
        "跳转画面
        SET PARAMETER ID: 'AUN' FIELD ls_data-vbeln.
        CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.

 

2020.03.08 【ABAP随笔】- 简单ALV报表(模板)_第3张图片

 

你可能感兴趣的:(2020.03.08 【ABAP随笔】- 简单ALV报表(模板))