ABAP ALV表头的实现

ABAP实现ALV表头的DEMO:

效果:

源代码:

  1 *&---------------------------------------------------------------------*
  2 *& Report  Z_ALV_DEMO
  3 *&
  4 *&---------------------------------------------------------------------*
  5 *&
  6 *&
  7 *&---------------------------------------------------------------------*
  8 
  9 REPORT  Z_ALV_DEMO NO STANDARD PAGE HEADING.
 10 
 11 
 12 
 13 TABLES ZEMP_TEST.
 14 
 15 DATA: IT_ZEMP TYPE STANDARD TABLE OF ZEMP_TEST,
 16       IW_ZEMP TYPE ZEMP_TEST.
 17 
 18 
 19 TYPE-POOLS : SLIS.
 20 DATA :
 21        IT_EVENT TYPE SLIS_T_EVENT, "不能有表头,否则会RUNTIME ERROR
 22        IT_LISTHEADER TYPE SLIS_T_LISTHEADER.
 23 
 24 PARAMETERS P_DEPT TYPE ZEMP_TEST-EDEPT.
 25 
 26 START-OF-SELECTION.
 27 
 28   PERFORM FRM_GET_EVENT.
 29 
 30   PERFORM FRM_GET_DATA.
 31 
 32   PERFORM FRM_COMMENT_BUILD.
 33 
 34   PERFORM FRM_ALV_DISPLAY.
 35 
 36 *&---------------------------------------------------------------------*
 37 *&      Form  FRM_GET_DATA
 38 *&---------------------------------------------------------------------*
 39 *       text
 40 *----------------------------------------------------------------------*
 41 *  -->  p1        text
 42 *  <--  p2        text
 43 *----------------------------------------------------------------------*
 44 FORM FRM_GET_DATA .
 45 
 46   SELECT *
 47     FROM ZEMP_TEST
 48     INTO TABLE IT_ZEMP
 49    WHERE EDEPT = P_DEPT.
 50 
 51   IF SY-SUBRC <> 0.
 52     MESSAGE S001(00) WITH '不存在符合条件的数据' DISPLAY LIKE 'E'.
 53     STOP.
 54   ENDIF.
 55 
 56 ENDFORM.                    " FRM_GET_DATA
 57 
 58 
 59 
 60 
 61 *&---------------------------------------------------------------------*
 62 *&      Form  FRM_ALV_DISPLAY
 63 *&---------------------------------------------------------------------*
 64 *       text
 65 *----------------------------------------------------------------------*
 66 *  -->  p1        text
 67 *  <--  p2        text
 68 *----------------------------------------------------------------------*
 69 FORM FRM_ALV_DISPLAY .
 70   DATA LW_REPID TYPE SY-REPID.
 71   LW_REPID = SY-REPID.
 72 
 73   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 74    EXPORTING
 75 *     I_INTERFACE_CHECK                 = ' '
 76 *     I_BYPASSING_BUFFER                = ' '
 77 *     I_BUFFER_ACTIVE                   = ' '
 78      I_CALLBACK_PROGRAM                = LW_REPID
 79 *     I_CALLBACK_PF_STATUS_SET          = ' '
 80 *     I_CALLBACK_USER_COMMAND           = ' '
 81 *     I_CALLBACK_TOP_OF_PAGE            = ' '
 82 *     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
 83 *     I_CALLBACK_HTML_END_OF_LIST       = ' '
 84      I_STRUCTURE_NAME                  = 'ZEMP_TEST'
 85 *     I_BACKGROUND_ID                   = ' '
 86      I_GRID_TITLE                      = 'ALV DEMO'
 87 *     I_GRID_SETTINGS                   =
 88 *     IS_LAYOUT                         =
 89 *     IT_FIELDCAT                       =
 90 *     IT_EXCLUDING                      =
 91 *     IT_SPECIAL_GROUPS                 =
 92 *     IT_SORT                           =
 93 *     IT_FILTER                         =
 94 *     IS_SEL_HIDE                       =
 95 *     I_DEFAULT                         = 'X'
 96 *     I_SAVE                            = ' '
 97 *     IS_VARIANT                        =
 98      IT_EVENTS                         = IT_EVENT
 99 *     IT_EVENT_EXIT                     =
100 *     IS_PRINT                          =
101 *     IS_REPREP_ID                      =
102 *     I_SCREEN_START_COLUMN             = 0
103 *     I_SCREEN_START_LINE               = 0
104 *     I_SCREEN_END_COLUMN               = 0
105 *     I_SCREEN_END_LINE                 = 0
106 *     I_HTML_HEIGHT_TOP                 = 0
107 *     I_HTML_HEIGHT_END                 = 0
108 *     IT_ALV_GRAPHICS                   =
109 *     IT_HYPERLINK                      =
110 *     IT_ADD_FIELDCAT                   =
111 *     IT_EXCEPT_QINFO                   =
112 *     IR_SALV_FULLSCREEN_ADAPTER        =
113 *   IMPORTING
114 *     E_EXIT_CAUSED_BY_CALLER           =
115 *     ES_EXIT_CAUSED_BY_USER            =
116     TABLES
117       T_OUTTAB                          = IT_ZEMP
118 *   EXCEPTIONS
119 *     PROGRAM_ERROR                     = 1
120 *     OTHERS                            = 2
121             .
122   IF SY-SUBRC <> 0.
123 *   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
124 *           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
125   ENDIF.
126 
127 ENDFORM.                    " FRM_ALV_DISPLAY
128 *&---------------------------------------------------------------------*
129 *&      Form  FRM_GET_EVENT
130 *&---------------------------------------------------------------------*
131 *       text
132 *----------------------------------------------------------------------*
133 *  -->  p1        text
134 *  <--  p2        text
135 *----------------------------------------------------------------------*
136 FORM FRM_GET_EVENT .
137 
138 * EVENTS
139 *SLIS_EV_ITEM_DATA_EXPAND  TYPE SLIS_FORMNAME VALUE 'ITEM_DATA_EXPAND',
140 *SLIS_EV_REPREP_SEL_MODIFY TYPE SLIS_FORMNAME VALUE 'REPREP_SEL_MODIFY',
141 *SLIS_EV_CALLER_EXIT_AT_START TYPE SLIS_FORMNAME VALUE 'CALLER_EXIT',
142 *SLIS_EV_USER_COMMAND       TYPE SLIS_FORMNAME VALUE 'USER_COMMAND',
143 *SLIS_EV_TOP_OF_PAGE        TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE',
144 *SLIS_EV_DATA_CHANGED       TYPE SLIS_FORMNAME VALUE 'DATA_CHANGED',
145 *SLIS_EV_TOP_OF_COVERPAGE  TYPE SLIS_FORMNAME VALUE 'TOP_OF_COVERPAGE',
146 *SLIS_EV_END_OF_COVERPAGE  TYPE SLIS_FORMNAME VALUE 'END_OF_COVERPAGE',
147 *SLIS_EV_FOREIGN_TOP_OF_PAGE TYPE SLIS_FORMNAME
148 *                                       VALUE 'FOREIGN_TOP_OF_PAGE',
149 *SLIS_EV_FOREIGN_END_OF_PAGE TYPE SLIS_FORMNAME
150 *                                       VALUE 'FOREIGN_END_OF_PAGE',
151 *SLIS_EV_PF_STATUS_SET      TYPE SLIS_FORMNAME VALUE 'PF_STATUS_SET',
152 *SLIS_EV_LIST_MODIFY        TYPE SLIS_FORMNAME VALUE 'LIST_MODIFY',
153 *SLIS_EV_TOP_OF_LIST        TYPE SLIS_FORMNAME VALUE 'TOP_OF_LIST',
154 *SLIS_EV_END_OF_PAGE        TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE',
155 *SLIS_EV_END_OF_LIST        TYPE SLIS_FORMNAME VALUE 'END_OF_LIST',
156 *SLIS_EV_AFTER_LINE_OUTPUT TYPE SLIS_FORMNAME VALUE 'AFTER_LINE_OUTPUT',
157 *SLIS_EV_BEFORE_LINE_OUTPUT TYPE SLIS_FORMNAME VALUE
158 *                                                 'BEFORE_LINE_OUTPUT',
159 *SLIS_EV_SUBTOTAL_TEXT      TYPE SLIS_FORMNAME VALUE 'SUBTOTAL_TEXT',
160 *SLIS_EV_GROUPLEVEL_CHANGE  TYPE SLIS_FORMNAME VALUE
161 *                                                  'GROUPLEVEL_CHANGE',
162 *SLIS_EV_CONTEXT_MENU       TYPE SLIS_FORMNAME VALUE 'CONTEXT_MENU'.
163 
164   DATA: FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'FRM_TOP_OF_PAGE'.
165 
166   DATA L_EVENTS TYPE SLIS_ALV_EVENT.
167 
168   CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
169     EXPORTING
170       I_LIST_TYPE     = 0
171     IMPORTING
172       ET_EVENTS       = IT_EVENT
173     EXCEPTIONS
174       LIST_TYPE_WRONG = 1
175       OTHERS          = 2.
176   IF SY-SUBRC <> 0.
177     MESSAGE ID SY-MSGID TYPE SY-MSGTY
178         NUMBER SY-MSGNO
179           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
180   ENDIF.
181 * 在事件(字段名:NAME)为"TOP_OF_PAGE"的行中
182 *将FORM名(字段名:FORM)设为"FRM_TOP_OF_PAGE"
183 * 这样,系统就会自动PERFORM FRM_TOP_OF_PAGE.
184   CLEAR L_EVENTS.
185   READ TABLE IT_EVENT
186         WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
187         INTO L_EVENTS.
188 
189   IF SY-SUBRC = 0.
190     MOVE FORMNAME_TOP_OF_PAGE TO L_EVENTS-FORM.
191     MODIFY IT_EVENT FROM L_EVENTS INDEX SY-TABIX.
192   ELSE.
193     L_EVENTS-FORM = FORMNAME_TOP_OF_PAGE.
194     L_EVENTS-NAME = FORMNAME_TOP_OF_PAGE.
195     APPEND L_EVENTS TO IT_EVENT.
196   ENDIF.
197 
198 ENDFORM.                    " FRM_GET_EVENT
199 
200 *&---------------------------------------------------------------------*
201 *&      Form  FRM_TOP_OF_PAGE
202 *&---------------------------------------------------------------------*
203 *       表头
204 *----------------------------------------------------------------------*
205 *  -->  p1        text
206 *  <--  p2        text
207 *----------------------------------------------------------------------*
208 FORM  FRM_TOP_OF_PAGE.
209   CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
210     EXPORTING
211       IT_LIST_COMMENTARY       = IT_LISTHEADER
212       I_LOGO                   = 'ENJOYSAP_LOGO'
213 *     I_END_OF_LIST_GRID       =
214 *     I_ALV_FORM               =
215       .
216 * "TCODE:OAOR,CLASS
217 * NAME:PICTURES,CLASS TYPE :OT,执行,可以看到所有的PICTURE
218 
219 ENDFORM.                    "FRM_TOP_OF_PAGE
220 *&---------------------------------------------------------------------*
221 *&      Form  FRM_COMMENT_BUILD
222 *&---------------------------------------------------------------------*
223 *       text
224 *----------------------------------------------------------------------*
225 *  -->  p1        text
226 *  <--  p2        text
227 *----------------------------------------------------------------------*
228 FORM FRM_COMMENT_BUILD .
229   DATA LW_LISTHEADER TYPE SLIS_LISTHEADER.
230 
231   CLEAR: IT_LISTHEADER, LW_LISTHEADER.
232 
233   LW_LISTHEADER-TYP  = 'H'.
234   LW_LISTHEADER-INFO = 'ALV DEMO'.
235   APPEND LW_LISTHEADER TO IT_LISTHEADER.
236 
237   CLEAR LW_LISTHEADER.
238   LW_LISTHEADER-TYP  = 'S'.
239   LW_LISTHEADER-KEY  = '用户:'.
240   LW_LISTHEADER-INFO = SY-UNAME.
241   APPEND LW_LISTHEADER TO IT_LISTHEADER.
242 
243   CLEAR LW_LISTHEADER.
244   LW_LISTHEADER-TYP  = 'S'.
245   LW_LISTHEADER-KEY  = '日期:'.
246   LW_LISTHEADER-INFO = SY-DATUM.
247   APPEND LW_LISTHEADER TO IT_LISTHEADER.
248 
249 ENDFORM.                    " FRM_COMMENT_BUILD

 

posted @ 2015-12-04 17:01  明大叔  阅读(9785)  评论(0编辑  收藏  举报