当前位置: 代码迷 >> 综合 >> SAP-ABAP-一般FUNCTION ALV代码示例
  详细解决方案

SAP-ABAP-一般FUNCTION ALV代码示例

热度:70   发布时间:2023-12-13 04:19:10.0

 复制粘贴即可使用,只需要修改一下几点即可实现ALV报表展示

1.TY_ALV的定义,这是展示的alv的结构

2.选择屏幕

3.PERFORM GET_DATA.的内容,在里面写入报表的取值即可,ALV的展示表是GT_ALV

4.FIELDCAT 报表展示的清单

5.gui状态,不写会导致按钮报错

6.选择屏幕文本 


TABLES : BKPF,BSEG.
"定义内表
TYPES:BEGIN OF TY_ALV,SEL     TYPE C ,BUKRS   TYPE BSEG-BUKRS ,BELNR   TYPE BSEG-BELNR ,GJAHR   TYPE BSEG-GJAHR ,BUZEI   TYPE BSEG-BUZEI ,WRBTR   TYPE BSEG-WRBTR ,HKONT   TYPE BSEG-HKONT ,SHKZG   TYPE BSEG-SHKZG ,MWSKZ   TYPE BSEG-MWSKZ .TYPES   END OF TY_ALV.
"定义内表和工作区
DATA:GT_ALV    TYPE STANDARD TABLE OF TY_ALV,GS_ALV    TYPE TY_ALV,GS_LAYOUT TYPE LVC_S_LAYO,GT_FCAT   TYPE LVC_T_FCAT,GT_EVENTS TYPE SLIS_T_EVENT.SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:S_BUKRS FOR  BKPF-BUKRS    ,           "公司代码S_GJAHR FOR  BKPF-GJAHR    ,           "会计年度S_MONAT FOR  BSEG-H_MONAT  ,           "过账期间S_BLART FOR  BKPF-BLART  ,                       "凭证类型S_HKONT FOR  BSEG-HKONT .                        "科目
SELECTION-SCREEN END OF BLOCK BLK1.
**********************************************************************
*                INITIALIZATION
**********************************************************************
INITIALIZATION.
**********************************************************************
*               AT SELECTION-SCREEN OUTPUT                           *
**********************************************************************
AT SELECTION-SCREEN OUTPUT.
**********************************************************************
*                AT SELECTION-SCREEN.
**********************************************************************
AT SELECTION-SCREEN.
**********************************************************************
*                S T A R T    O F   S E L E C T I O N                *
**********************************************************************
START-OF-SELECTION.PERFORM GET_DATA.PERFORM PROCESS_DATA.PERFORM DISPLAY_DATA.
*&---------------------------------------------------------------------*
*&      Form  FRM_AUTH_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_AUTH_CHECK ."
"  AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'
"                      ID 'BUKRS' FIELD S_BUKRS
"                      ID 'ACTVT' FIELD '03'.
"  IF SY-SUBRC <> 0.
"    MESSAGE E001(00) WITH  '您没有该公司的权限'  S_BUKRS.
"  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM GET_DATA .SELECT * FROM BSEG INTO CORRESPONDING FIELDS OF TABLE @GT_ALV UP TO 10 ROWS.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM PROCESS_DATA .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM DISPLAY_DATA .PERFORM FRM_SET_LAYOUT.PERFORM FRM_SET_EVENT.PERFORM FRM_SET_FIELDCAT.PERFORM FRM_DISPLAY_ALV.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text alv 布局
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .CLEAR GS_LAYOUT.GS_LAYOUT-CWIDTH_OPT = 'X'.GS_LAYOUT-SEL_MODE   = 'A'.GS_LAYOUT-BOX_FNAME  = 'SEL'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .DATA: LS_FCAT LIKE LINE OF GT_FCAT.DATA: LV_INDEX TYPE I.DEFINE MCR_BUILD_FCAT.lv_index = lv_index + 1.CLEAR ls_fcat.ls_fcat-col_pos   = lv_index.ls_fcat-fieldname = &2.ls_fcat-ref_field = &3.ls_fcat-ref_table = &4.ls_fcat-no_out    = &5.ls_fcat-coltext   = &6.ls_fcat-scrtext_l = &6.ls_fcat-scrtext_m = &6.ls_fcat-scrtext_s = &6.IF ls_fcat-fieldname = 'ZNUM'.""改成需要去前导零的字段ls_fcat-no_zero = 'X'.ENDIF.APPEND ls_fcat TO &1.CLEAR ls_fcat.END-OF-DEFINITION.
*** ALV1 fieldcat 设置字段
* - 固定字段MCR_BUILD_FCAT  GT_FCAT 'BUKRS  '   'BUKRS  '   'BSEG'     ''  '公司代码'.MCR_BUILD_FCAT  GT_FCAT 'BELNR  '   'BELNR  '   'BSEG'     ''  '凭证编号'.MCR_BUILD_FCAT  GT_FCAT 'GJAHR  '   'GJAHR  '   'BSEG'     ''  '财年'.MCR_BUILD_FCAT  GT_FCAT 'BUZEI  '   'BUZEI  '   'BSEG'     ''  '行项目'.MCR_BUILD_FCAT  GT_FCAT 'WRBTR  '   'WRBTR  '   'BSEG'     ''  '金额'.MCR_BUILD_FCAT  GT_FCAT 'HKONT  '   'HKONT  '   'BSEG'     ''  '科目'.MCR_BUILD_FCAT  GT_FCAT 'SHKZG  '   'SHKZG  '   'BSEG'     ''  '借贷'.MCR_BUILD_FCAT  GT_FCAT 'MWSKZ  '   'MWSKZ  '   'BSEG'     ''  '税码'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_EVENT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_SET_EVENT.DATA:LS_EVENT LIKE LINE OF GT_EVENTS.LS_EVENT-NAME = 'USER_COMMAND' . "用户响应事件LS_EVENT-FORM = 'FRM_USER_COMMAND'.APPEND LS_EVENT TO GT_EVENTS.LS_EVENT-NAME = 'PF_STATUS_SET' . "工具栏事件LS_EVENT-FORM = 'FRM_SET_STATUS'.LS_EVENT-NAME = 'DATA_CHANGED' . "修改事件LS_EVENT-FORM = 'frm_alv_data_changed'.APPEND LS_EVENT TO GT_EVENTS.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .DATA ls_grid_settings TYPE LVC_S_GLAY .ls_grid_settings-edt_cll_cb = 'X' .CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'EXPORTINGI_CALLBACK_PROGRAM       = SY-REPIDIS_LAYOUT_LVC            = GS_LAYOUTIT_FIELDCAT_LVC          = GT_FCATIT_EVENTS                = GT_EVENTSi_grid_settings          = ls_grid_settingsI_CALLBACK_PF_STATUS_SET = 'FRM_SET_STATUS'I_CALLBACK_USER_COMMAND  = 'FRM_USER_COMMAND'I_SAVE                   = 'A'TABLEST_OUTTAB                 = GT_ALVEXCEPTIONSPROGRAM_ERROR            = 1OTHERS                   = 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.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
FORM FRM_SET_STATUS USING P_EXTAB TYPE SLIS_T_EXTAB .SET PF-STATUS 'STANDARD'.SET TITLEBAR 'STANDARD'.
ENDFORM.
FORM FRM_USER_COMMAND USING P_UCOMM        TYPE SY-UCOMMP_RS_SELFIELD  TYPE SLIS_SELFIELD.READ TABLE GT_ALV INTO DATA(LS_ALV) INDEX P_RS_SELFIELD-TABINDEX .CASE P_UCOMM.WHEN '&DATA_SAVE' .                            " 保存数据PERFORM FRM_SAVE_DATA .ENDCASE.P_RS_SELFIELD-REFRESH = 'X' .   " 当用户在显式界面上对数据进行修改时,同时内表中的数据也随之刷新
ENDFORM.                    " frm_alv_user_command
FORM FRM_SAVE_DATA .LOOP AT GT_ALV INTO DATA(LS_ALV) WHERE SEL = 'X' .ENDLOOP.
ENDFORM .
form frm_alv_data_changed using p_changed_data type ref to cl_alv_changed_data_protocol.data: field_name(20),modified_cells type lvc_s_modi.data: l_city like spfli-cityto.field-symbols <field_value>.loop at p_changed_data->mt_mod_cells into modified_cells .  "" where fieldname = 'C'.READ TABLE GT_ALV INDEX modified_cells-ROW_ID ASSIGNING FIELD-SYMBOL(<FS_ALV>) .IF SY-SUBRC = 0.""alv数据变化了,其他字段同步修改ENDIF.endloop.
endform.

  相关解决方案