一个很好的ALV示例。
[@more@]*&---------------------------------------------------------------------*
*& Report Z_TEST01
*&
*&---------------------------------------------------------------------*
REPORT Z_TEST01 NO STANDARD PAGE HEADING.
*--------------------------定义数据------------------------------------*
*WERKS=工厂,NAME1=工厂名称,BUDAT=凭证日期,MBLNR=物料凭证编号,
*MJAHR=物料凭证年度,VGART=凭证类型----------*
DATA: BEGIN OF IT_RESULT OCCURS 0,
WERKS LIKE T001W-WERKS,
NAME1 LIKE T001W-NAME1,
BUDAT LIKE MKPF-BUDAT,
DMBTR LIKE MSEG-DMBTR,
ZWZXH TYPE P DECIMALS 3,
END OF IT_RESULT.
DATA: BEGIN OF IT_T001W OCCURS 0,
WERKS LIKE T001W-WERKS,
NAME1 LIKE T001W-NAME1,
END OF IT_T001W.
DATA: BEGIN OF IT_MKPF OCCURS 0,
MBLNR LIKE MKPF-MBLNR,
MJAHR LIKE MKPF-MJAHR,
BUDAT LIKE MKPF-BUDAT,
END OF IT_MKPF.
DATA: BEGIN OF IT_MSEG OCCURS 0,
MBLNR LIKE MSEG-MBLNR,
MJAHR LIKE MSEG-MJAHR,
ZEILE LIKE MSEG-ZEILE,
WERKS LIKE MSEG-WERKS,
DMBTR LIKE MSEG-DMBTR,
SHKZG LIKE MSEG-SHKZG,
END OF IT_MSEG.
TYPE-POOLS: SLIS.
DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
IS_LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA: TMP_TITLE(70),
TMP_REPID LIKE SY-REPID.
*----------------初始选择屏幕--------------------*
SELECT-OPTIONS: S_WERKS FOR IT_RESULT-WERKS,
S_BUDAT FOR IT_RESULT-BUDAT.
PARAMETERS: R1 RADIOBUTTON GROUP RAD1,
R2 RADIOBUTTON GROUP RAD1 DEFAULT 'X'.
*-------------------TOP-OF-PAGE------------------*
TOP-OF-PAGE.
PERFORM FRM_HEAD_OUTPUT .
*------------------START-OF-SELECTION-------------*
START-OF-SELECTION.
PERFORM FRM_GETDATA.
PERFORM FRM_PROCESS.
*------------------END-OF-SELECTION---------------*
END-OF-SELECTION.
PERFORM FRM_OUTPUT.
*&---------------------------------------------------------------------*
*& Form FRM_GETDATA
*&---------------------------------------------------------------------*
* 获取数据
*----------------------------------------------------------------------*
* --> p1 text
* *----------------------------------------------------------------------*
FORM FRM_GETDATA .
SELECT * FROM T001W
INTO CORRESPONDING FIELDS OF TABLE IT_T001W
WHERE WERKS IN S_WERKS.
SELECT * FROM MKPF
INTO CORRESPONDING FIELDS OF TABLE IT_MKPF
WHERE VGART EQ 'WA' AND BUDAT IN S_BUDAT.
SELECT * FROM MSEG
INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
WHERE WERKS IN S_WERKS
AND BWART IN ('201','202','261','262').
ENDFORM. " FRM_GETDATA
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS
*&---------------------------------------------------------------------*
* 对数据进行处理
*----------------------------------------------------------------------*
* --> p1 text
* *----------------------------------------------------------------------*
FORM FRM_PROCESS .
LOOP AT IT_MSEG.
CLEAR IT_RESULT.
READ TABLE IT_MKPF WITH KEY MBLNR = IT_MSEG-MBLNR
MJAHR = IT_MSEG-MJAHR.
CHECK SY-SUBRC EQ 0.
READ TABLE IT_T001W WITH KEY WERKS = IT_MSEG-WERKS.
IT_RESULT-WERKS = IT_T001W-WERKS.
IT_RESULT-NAME1 = IT_T001W-NAME1.
IT_RESULT-BUDAT = IT_MKPF-BUDAT.
CASE IT_MSEG-SHKZG.
WHEN 'H'.
IT_RESULT-DMBTR = IT_MSEG-DMBTR.
WHEN 'S'.
IT_RESULT-DMBTR = 0 - IT_MSEG-DMBTR.
ENDCASE.
COLLECT IT_RESULT.
ENDLOOP.
ENDFORM. " FRM_PROCESS
*&---------------------------------------------------------------------*
*& Form FRM_OUTPUT
*&---------------------------------------------------------------------*
* 输出数据
*----------------------------------------------------------------------*
* --> p1 text
* *----------------------------------------------------------------------*
FORM FRM_OUTPUT .
IF R2 = 'X' .
GT_FIELDCAT-FIELDNAME = 'WERKS'.
GT_FIELDCAT-SELTEXT_M = '物料号'.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'NAME1'.
GT_FIELDCAT-SELTEXT_M = '物料名称'.
GT_FIELDCAT-OUTPUTLEN = 30.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'BUDAT'.
GT_FIELDCAT-SELTEXT_M = '记帐日期'.
GT_FIELDCAT-OUTPUTLEN = 10.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'DMBTR'.
GT_FIELDCAT-SELTEXT_M = '本位币金额'.
GT_FIELDCAT-OUTPUTLEN = 20.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'ZWZXH'.
GT_FIELDCAT-SELTEXT_M = '物资消耗(万元/kwh)'.
GT_FIELDCAT-OUTPUTLEN = 30.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
TMP_TITLE = '库存资金占用额'.
TMP_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = TMP_REPID
IT_FIELDCAT = GT_FIELDCAT[]
I_GRID_TITLE = TMP_TITLE
TABLES
T_OUTTAB = IT_RESULT.
ENDIF.
IF R1 = 'X'.
LOOP AT IT_RESULT.
WRITE: '|',(10) IT_RESULT-WERKS CENTERED,
'|',(40) IT_RESULT-NAME1,
'|',(15) IT_RESULT-BUDAT CENTERED,
'|',(21) IT_RESULT-DMBTR,
'|',(22) IT_RESULT-ZWZXH ,
'|'.
ULINE /.
ENDLOOP.
ENDIF.
ENDFORM. " FRM_OUTPUT
*&---------------------------------------------------------------------*
*& Form FRM_HEAD_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* *----------------------------------------------------------------------*
FORM FRM_HEAD_OUTPUT .
ULINE .
WRITE: '|',(10) '物料号' CENTERED,
'|',(40) '物料名称' CENTERED,
'|',(15) '记账期间' CENTERED,
'|',(21) '本位币金额' CENTERED ,
'|',(22) '物资消耗(万元/kwh)' CENTERED ,
'|'.
ULINE /.
ENDFORM. " FRM_HEAD_OUTPUT
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8356764/viewspace-929419/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8356764/viewspace-929419/