ABAP实现ALV表头的DEMO:
运行效果:
代码:
*********************************************************************** *ZMMRTESTR *********************************************************************** REPORT ZMMRTESTR. ************************************************************************ * Tables Definitions ************************************************************************ *TABLES:. ************************************************************************ * Data Definitions ************************************************************************ DATA:BEGIN OF GT_ITAB OCCURS 0, NAME(10) TYPE C, AGE TYPE I, CLASS(10) TYPE C, END OF GT_ITAB. TYPE-POOLS SLIS. DATA: GS_LAYOUT TYPE SLIS_LAYOUT_ALV, GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV, WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV, GT_EVENT TYPE SLIS_T_EVENT, GW_EVENT LIKE LINE OF GT_EVENT, LISTHEADER TYPE SLIS_T_LISTHEADER, WA_LISTHEADER LIKE LINE OF LISTHEADER. ************************************************************************ * Selection Screen ************************************************************************ ************************************************************************ * Initialization ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen ************************************************************************ AT SELECTION-SCREEN. ************************************************************************ * At Selection Screen Output ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process ************************************************************************ START-OF-SELECTION. PERFORM GET_DATA. PERFORM BUILD_EVENTS. PERFORM FRM_TOP_OF_PAGE. PERFORM CATA_LOG. PERFORM ALV_SHOW. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM GET_DATA . GT_ITAB-NAME = '张三'. GT_ITAB-AGE = 13. GT_ITAB-CLASS = 'Class1'. APPEND GT_ITAB. CLEAR GT_ITAB. GT_ITAB-NAME = '李四'. GT_ITAB-AGE = 14. GT_ITAB-CLASS = 'Class2'. APPEND GT_ITAB. CLEAR GT_ITAB. ENDFORM. " GET_DATA FORM BUILD_EVENTS. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING ET_EVENTS = GT_EVENT EXCEPTIONS LIST_TYPE_WRONG = 1 OTHERS = 2 . IF SY-SUBRC = 0. READ TABLE GT_EVENT WITH KEY NAME = 'TOP_OF_PAGE' INTO GW_EVENT. IF SY-SUBRC = 0. MOVE 'FRM_TOP_OF_PAGE' TO GW_EVENT-FORM. MODIFY GT_EVENT FROM GW_EVENT INDEX SY-TABIX. ENDIF. ENDIF. ENDFORM. FORM FRM_TOP_OF_PAGE. DATA: H_DATUM(10) TYPE C, H_UZEIT(10) TYPE C. CLEAR:LISTHEADER,WA_LISTHEADER. *标题 WA_LISTHEADER-TYP = 'H'. WA_LISTHEADER-KEY = ''. WA_LISTHEADER-INFO = 'XXX学校'. APPEND WA_LISTHEADER TO LISTHEADER. CLEAR:WA_LISTHEADER. *中标题 WA_LISTHEADER-TYP = 'S'. WA_LISTHEADER-KEY = ''. WA_LISTHEADER-INFO = '学生名单'. APPEND WA_LISTHEADER TO LISTHEADER. CLEAR:WA_LISTHEADER. WRITE SY-DATUM TO H_DATUM DD/MM/YYYY. WRITE SY-UZEIT TO H_UZEIT USING EDIT MASK '__:__:__'. *小标题 WA_LISTHEADER-TYP = 'A'. WA_LISTHEADER-KEY = ''. CONCATENATE 'Today:' H_DATUM 'Time:' H_UZEIT INTO WA_LISTHEADER-INFO. APPEND WA_LISTHEADER TO LISTHEADER. CLEAR:WA_LISTHEADER. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING IT_LIST_COMMENTARY = LISTHEADER * I_LOGO = '' * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. *&---------------------------------------------------------------------* *& Form CATA_LOG *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM CATA_LOG. DEFINE FIELDCAT. WA_FIELDCAT-FIELDNAME = &1. "对应内表字段名 WA_FIELDCAT-SELTEXT_L = &2. "输出列文本 APPEND WA_FIELDCAT TO GT_FIELDCAT.CLEAR WA_FIELDCAT. END-OF-DEFINITION. FIELDCAT 'NAME' '姓名'. FIELDCAT 'AGE' '年龄'. FIELDCAT 'CLASS' '班级'. ENDFORM. " CATA_LOG *&---------------------------------------------------------------------* *& Form ALV_SHOW *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM ALV_SHOW .GS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. * 显示ALV的函数 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = SY-REPID IS_LAYOUT = GS_LAYOUT IT_FIELDCAT = GT_FIELDCAT[] IT_EVENTS = GT_EVENT I_SAVE = 'A' "保存全局,特定變式 TABLES T_OUTTAB = gt_itab EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. ENDFORM. " ALV_SHOW