Sunteți pe pagina 1din 6

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

Log In

Register

About Us

How to Contribute

Store

Welcome Guest

SDN Community
Home Forums Wiki Blogs

BPX Community
Articles eLearning

Business Analytics
Downloads

University Alliances
Career Center

SAP EcoHub
InnoCentive Idea Place SAP Help Portal

Code Exchange

Events

ALV Grid Report - with Object Oriented SALV Classes


Added by Vinod Kumar, last edited by Alon Mizrahi on Nov 01, 2011

ALV Grid Report - with Object Oriented SALV Classes


Summary
This Post narrates the sample code for creating ALV Grid Report with Object Oriented method. Basic Functionalities for ALV Grid display is attained using different SALV Classes.

Useful Information ALV Grid Report using Object Oriented Programming Concept

Author(s): Vinod Kumar T


Company: Created on: Author(s) Bio Vinod Kumar is an ABAP consultant working with NIIT Technologies Limited. NIIT Technologies Limited 05.10.2010

Class and Methods used in the Report


Class Name CL_SALV_TABLE Parent Class Method FACTORY Description To Create the Instance of ALV Table Object CL_SALV_TABLE CL_SALV_MODEL_LIST GET_LAYOUT Layout Object with Current Settings CL_SALV_TABLE GET_FUNCTIONS Returns Object to Define the functions of the ALV Table CL_SALV_TABLE CL_SALV_MODEL_LIST GET_DISPLAY_SETTINGS Returns Global Settings for ALV Display CL_SALV_TABLE GET_AGGREGATIONS Return Object to Edit Aggregation Functions Settings CL_SALV_TABLE GET_SORTS Return Object to Set all Sort Information CL_SALV_TABLE GET_SELECTIONS Return Object to do Settings for Selection pattern for Row(s) and Column(s) CL_SALV_TABLE CL_SALV_MODEL_BASE SET_TOP_OF_LIST Set the Contents in Top of List in ALV Display CL_SALV_TABLE GET_EVENT Return the Object to manage existing events CL_SALV_TABLE GET_COLUMNS Return individual column objects of Output table CL_SALV_LAYOUT SET_KEY Set Layout Key for Layouts in Report CL_SALV_LAYOUT GET_LAYOUTS Returns all layouts defined for the report

1 of 6

09-02-2012 AM 11:45

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

CL_SALV_LAYOUT

F4_LAYOUTS

F4 Help to Display Existing Layouts of the Report

CL_SALV_LAYOUT

SET_SAVE_RESTRICTION

Restriction Methods while saving the Layout

CL_SALV_LAYOUT

SET_DEFAULT

Activate the Default Layout

CL_SALV_LAYOUT

SET_INITIAL_LAYOUT

Set specific Layout as Initial layout while starting ALV Display

CL_SALV_FUNCTIONS_LIST

CL_SALV_FUNCTIONS

SET_ALL

Activate all Internal Functions of ALV

CL_SALV_DISPLAY_SETTINGS

SET_STRIPPED_PATTERN

Set ALV Display in Stripped Pattern

CL_SALV_DISPLAY_SETTINGS

SET_LIST_HEADER

Set Title of the ALV Display

CL_SALV_SORTS

ADD_SORT

Define Sort for a Specific Column

CL_SALV_SELECTIONS

SET_SELECTION_MODE

Set the Selection Methods

CL_SALV_AGGREGATIONS

ADD_AGGREGATION

Add Aggregation function of a Table column

CL_SALV_FORM_LAYOUT_GRID

CREATE_HEADER_INFORMATION Create Header Information in the Cell

CL_SALV_FORM_LAYOUT_GRID CL_SALV_FORM_UIE_LAYOUT_GRID ADD_ROW

Add New row in the Grid

CL_SALV_FORM_LAYOUT_GRID

CREATE_LABEL

Create Label in Grid Cell

CL_SALV_FORM_LAYOUT_GRID

CREATE_TEXT

Create Text in Grid Cell

CL_SALV_FORM_LABEL

CL_SALV_FORM_UIE_TEXT_VIEW

SET_TEXT

Assign Text to the Label in Grid Cell

CL_SALV_COLUMNS_TABLE

CL_SALV_COLUMNS

GET

Get the list of Columns of output table

CL_SALV_COLUMNS_TABLE

CL_SALV_COLUMNS

SET_OPTIMIZE

Optimize the width of columns

CL_SALV_COLUMNS_TABLE

CL_SALV_COLUMNS

SET_KEY_FIXATION

Fix the Key columns while Vertical Scrolling

CL_SALV_COLUMNS_TABLE

CL_SALV_COLUMNS_LIST

SET_COLOR_COLUMN

Assign colors to the columns in output table

CL_SALV_COLUMNS_TABLE

CL_SALV_COLUMNS

GET_COLUMN

Get Properties of Single Column

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN

GET_DDIC_DATATYPE

Get data type of Selected column

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN

SET_TECHNICAL

Set column as Technical (Invisible)

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN

GET_DDIC_INITTYPE

Returns Internal ABAP Data Type

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN

GET_DDIC_DOMAIN

Get the Domain Name of Column

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN_LIST

SET_CELL_TYPE

Set Cell type of Column

2 of 6

09-02-2012 AM 11:45

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN_LIST

SET_COLOR

Set the color of Single Column

CL_SALV_COLUMN_TABLE

CL_SALV_COLUMN_LIST

SET_KEY

Set the column as Key column

Sample Code

001. 002. 003. 004. 005. 006. 007. 008. 009. 010. 011. 012. 013. 014. 015. 016. 017. 018. 019. 020. 021. 022. 023. 024. 025. 026. 027. 028. 029. 030. 031. 032. 033. 034. 035. 036. 037. 038. 039. 040. 041. 042. 043. 044. 045. 046. 047. 048. 049. 050. 051. 052. 053. 054. 055. 056. 057. 058. 059. 060. 061. 062. 063. 064. 065. 066. 067. 068. 069. 070. 071. 072. 073. 074. 075. 076. 077. 078. 079. 080. 081. 082. 083. 084. 085. 086. 087. 088. 089. 090. 091.

*&---------------------------------------------------------------------* *& Report ZVK_CL_SALV *&---------------------------------------------------------------------* *&Report using OOPs with Basic functionalities. *&---------------------------------------------------------------------* REPORT zvk_cl_salv_1. TABLES : vbak. TYPE-POOLS : abap,rsanm. TYPES : BEGIN OF t_vbak. INCLUDE TYPE vbak. TYPES : t_color TYPE lvc_t_scol, END OF t_vbak. DATA : lt_vbak TYPE STANDARD TABLE OF t_vbak, ls_vbak TYPE t_vbak. FIELD-SYMBOLS : DATA : l_rec(5) TYPE t_vbak. TYPE n.

** Declaration for ALV Grid ** DATA : gr_table TYPE REF TO cl_salv_table. ** Declarations for ALV Functions DATA : gr_functions TYPE REF TO cl_salv_functions_list. ** declaration for ALV Columns DATA : gr_columns TYPE REF TO cl_salv_columns_table, gr_column TYPE REF TO cl_salv_column_table, lt_column_ref TYPE salv_t_column_ref, ls_column_ref TYPE salv_s_column_ref. ** declaration for Layout Settings DATA : gr_layout TYPE REF TO cl_salv_layout, gr_layout_key TYPE salv_s_layout_key, ls_layout TYPE salv_s_layout, lt_layout_info TYPE salv_t_layout_info. ** Declaration for Global Display Settings DATA : gr_display TYPE REF TO cl_salv_display_settings, lv_title TYPE lvc_title. ** Declaration for Aggregate Function Settings DATA : gr_aggr TYPE REF TO cl_salv_aggregations. ** Declaration for Sort Function Settings DATA : gr_sort TYPE REF TO cl_salv_sorts. ** Declaration for Table Selection settings DATA : gr_select TYPE REF TO cl_salv_selections. ** Declaration for Top of List settings DATA : gr_content TYPE REF TO cl_salv_form_element. ** Class for handling Events CLASS : lcl_handle_events DEFINITION DEFERRED. DATA : gr_events TYPE REF TO lcl_handle_events, lr_events TYPE REF TO cl_salv_events_table. ** Coloring of Date columns ** DATA : lt_colo TYPE STANDARD TABLE OF lvc_s_colo, ls_colo TYPE lvc_s_colo. ** Color Structure of columns ** DATA : lt_color TYPE lvc_t_scol, ls_color TYPE lvc_s_scol. *----------------------------------------------------------------------* * CLASS lcl_handle_events DEFINITION *----------------------------------------------------------------------* CLASS lcl_handle_events DEFINITION. PUBLIC SECTION. METHODS : on_double_click FOR EVENT double_click OF cl_salv_events_table IMPORTING row column. METHODS : on_link_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row column. ENDCLASS. "lcl_handle_events DEFINITION *----------------------------------------------------------------------* * CLASS lcl_handle_events IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_handle_events IMPLEMENTATION. METHOD on_double_click. PERFORM get_ord_info USING row column. ENDMETHOD. "on_double_click METHOD on_link_click. PERFORM get_ord_info USING row column. ENDMETHOD. "on_link_click ENDCLASS. "lcl_handle_events IMPLEMENTATION SELECTION-SCREEN BEGIN OF BLOCK b1.

3 of 6

09-02-2012 AM 11:45

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

092. 093. 094. 095. 096. 097. 098. 099. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200.

SELECT-OPTIONS : p_erdat FOR vbak-erdat. PARAMETER : p_var TYPE slis_vari. SELECTION-SCREEN END OF BLOCK b1. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_var. CLEAR : ls_layout. TRY. CALL METHOD cl_salv_table=>factory EXPORTING list_display = if_salv_c_bool_sap=>false IMPORTING r_salv_table = gr_table CHANGING t_table = lt_vbak. CATCH cx_salv_msg . ENDTRY. IF gr_table IS NOT INITIAL. MOVE sy-repid TO gr_layout_key-report. "Set Layout Key as Report ID" gr_layout = gr_table->get_layout( ). "Get Layout of the Table" gr_layout->set_key( gr_layout_key ). "Set Layout key to Layout" lt_layout_info = gr_layout->get_layouts( )."Get the Layouts of report" IF lt_layout_info[] IS NOT INITIAL. ls_layout = gr_layout->f4_layouts( ). "Activate F4 Help for Layouts" IF ls_layout IS NOT INITIAL. MOVE ls_layout-layout TO p_var. ENDIF. ENDIF. ENDIF. START-OF-SELECTION. REFRESH : lt_vbak. SELECT * FROM vbak INTO CORRESPONDING FIELDS OF TABLE lt_vbak WHERE erdat IN p_erdat. IF sy-subrc EQ 0. DESCRIBE TABLE lt_vbak LINES l_rec. PERFORM set_color. PERFORM alv_grid_display. ENDIF. *&---------------------------------------------------------------------* *& Form alv_grid_display *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM alv_grid_display. CLEAR : gr_table. TRY. CALL METHOD cl_salv_table=>factory EXPORTING list_display = if_salv_c_bool_sap=>false IMPORTING r_salv_table = gr_table CHANGING t_table = lt_vbak. CATCH cx_salv_msg . ENDTRY. IF gr_table IS INITIAL. MESSAGE 'Error Creating ALV Grid ' TYPE 'I' DISPLAY LIKE 'E'. STOP. ENDIF. ** Get functions details gr_functions = gr_table->get_functions( ). ** Activate All Buttons in Tool Bar gr_functions->set_all( if_salv_c_bool_sap=>true ). ******* Layout Settings ******* CLEAR : gr_layout, gr_layout_key. MOVE sy-repid TO gr_layout_key-report.

"Set Report ID as Layout Key"

gr_layout = gr_table->get_layout( ). "Get Layout of Table" gr_layout->set_key( gr_layout_key ). "Set Report Id to Layout" gr_layout->set_save_restriction( if_salv_c_layout=>restrict_none ). "No Restriction to Save Layout" IF p_var IS INITIAL. gr_layout->set_default( if_salv_c_bool_sap=>true ). "Set Default Variant" ELSE. gr_layout->set_initial_layout( p_var ). "Set the Selected Variant as Initial" ENDIF. ******* Global Display Settings ******* CLEAR : gr_display. MOVE 'Sales Order Details' TO lv_title. gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( if_salv_c_bool_sap=>true ). gr_display->set_list_header( lv_title ). ******* Aggregate Function Settings ******* gr_aggr = gr_table->get_aggregations( ).

" Global Display settings" "Activate Strip Pattern" "Report Header" "Get Aggregate Functions"

******* Sort Functions ******* gr_sort = gr_table->get_sorts( ). IF gr_sort IS NOT INITIAL. TRY. gr_sort->add_sort( columnname = 'ERNAM' position = 1 sequence = if_salv_c_sort=>sort_up subtotal = if_salv_c_bool_sap=>true group = if_salv_c_sort=>group_none obligatory = if_salv_c_bool_sap=>false ). CATCH cx_salv_not_found . CATCH cx_salv_existing . CATCH cx_salv_data_error . ENDTRY. TRY.

4 of 6

09-02-2012 AM 11:45

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309.

gr_sort->add_sort( columnname = 'ERDAT' position = 2 sequence = if_salv_c_sort=>sort_down subtotal = if_salv_c_bool_sap=>false group = if_salv_c_sort=>group_none obligatory = if_salv_c_bool_sap=>false ). CATCH cx_salv_not_found . CATCH cx_salv_existing . CATCH cx_salv_data_error . ENDTRY. ENDIF. ******* Table Selection Settings ******* gr_select = gr_table->get_selections( ). IF gr_select IS NOT INITIAL. gr_select->set_selection_mode( if_salv_c_selection_mode=>row_column ). "Allow single row Selection" ENDIF. ******* Top of List settings ******* PERFORM top_of_page CHANGING gr_content. gr_table->set_top_of_list( gr_content ). ******* Event Register settings ******* lr_events = gr_table->get_event( ). CREATE OBJECT gr_events. SET HANDLER gr_events->on_double_click FOR lr_events. SET HANDLER gr_events->on_link_click FOR lr_events. ** Get the columns from ALV Table gr_columns = gr_table->get_columns( ). IF gr_columns IS NOT INITIAL. REFRESH : lt_column_ref. CLEAR : ls_column_ref. lt_column_ref = gr_columns->get( ). ** Get columns properties gr_columns->set_optimize( if_salv_c_bool_sap=>true ). gr_columns->set_key_fixation( if_salv_c_bool_sap=>true ). TRY. gr_columns->set_color_column( 'T_COLOR' ). CATCH cx_salv_data_error . ENDTRY. ** Individual Column Properties. PERFORM column_settings. ENDIF. CALL METHOD gr_table->display. ENDFORM. "alv_grid_display *&---------------------------------------------------------------------* *& Form column_settings *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM column_settings. LOOP AT lt_column_ref INTO ls_column_ref. TRY. gr_column ?= gr_columns->get_column( ls_column_ref-columnname ). CATCH cx_salv_not_found. ENDTRY. IF gr_column IS NOT INITIAL. ** Make Mandt column invisible ** IF gr_column->get_ddic_datatype( ) = 'CLNT'. gr_column->set_technical( if_salv_c_bool_sap=>true ). ENDIF. ** Create Aggregate function total for All Numeric/Currency Fields ** IF gr_column->get_ddic_inttype( ) EQ 'P' OR gr_column->get_ddic_datatype( ) EQ 'CURR'. IF gr_aggr IS NOT INITIAL. TRY. gr_aggr->add_aggregation( columnname = ls_column_ref-columnname aggregation = if_salv_c_aggregation=>total ). CATCH cx_salv_data_error . CATCH cx_salv_not_found . CATCH cx_salv_existing . ENDTRY. ENDIF. ENDIF. ** Create Check box for fields with domain "XFELD" IF gr_column->get_ddic_domain( ) EQ 'XFELD'. gr_column->set_cell_type( if_salv_c_cell_type=>checkbox ). ENDIF. ** Set color to Date Columns ** IF gr_column->get_ddic_datatype( ) EQ 'DATS'. CLEAR : ls_colo. MOVE 2 TO ls_colo-col. MOVE 1 TO ls_colo-int. MOVE 1 TO ls_colo-inv. gr_column->set_color( ls_colo ). ENDIF. ** Add Hotspot&Hyper Link to the column vbeln IF ls_column_ref-columnname EQ 'VBELN'. gr_column->set_cell_type( if_salv_c_cell_type=>hotspot ). gr_column->set_key( if_salv_c_bool_sap=>true ). ENDIF. ENDIF. ENDLOOP. ENDFORM.

"column_settings

*&---------------------------------------------------------------------* *& Form top_of_page *&---------------------------------------------------------------------* FORM top_of_page CHANGING lr_content TYPE REF TO cl_salv_form_element. DATA : lr_grid TYPE REF TO cl_salv_form_layout_grid, lr_text TYPE REF TO cl_salv_form_text, lr_label TYPE REF TO cl_salv_form_label, lr_head TYPE string.

5 of 6

09-02-2012 AM 11:45

SAP Community Network Wiki - ABAP Development - ALV Grid Report ...

http://wiki.sdn.sap.com/wiki/display/ABAP/ALV+Grid+Report+-+with+...

310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380.

MOVE 'Sales Order List' TO lr_head. CREATE OBJECT lr_grid. ** Header of Top of Page ** lr_grid->create_header_information( row

= 1 column = 1 text = lr_head tooltip = lr_head ).

** Add Row ** lr_grid->add_row( ). ** Add Label in Grid ** lr_label = lr_grid->create_label( row = 2 column = 1 text = 'No of Records' tooltip = 'No of Records' ). ** Add Text in The Grid ** lr_text = lr_grid->create_text( row = 2 column = 2 text = l_rec tooltip = l_rec ). ** Set Label and Text Link ** lr_label->set_label_for( lr_text ). ** Move lr_grid to lr_content ** lr_content = lr_grid. ENDFORM. "top_of_page *&---------------------------------------------------------------------* *& Form *&---------------------------------------------------------------------* FORM get_ord_info USING row TYPE salv_de_row column TYPE salv_de_column. IF column EQ 'VBELN'. CLEAR : ls_vbak. READ TABLE lt_vbak INTO ls_vbak INDEX row. IF sy-subrc EQ 0. SET PARAMETER ID 'AUN' FIELD ls_vbak-vbeln. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. ENDIF. ENDIF. ENDFORM. "get_ord_info *&---------------------------------------------------------------------* *& Form set_color *&---------------------------------------------------------------------* * Set color to the column VBELN & NETWR for Order Value > 1000 *----------------------------------------------------------------------* FORM set_color. LOOP AT lt_vbak ASSIGNING . IF -netwr GT 1000. REFRESH : lt_color. CLEAR : ls_color. MOVE 'VBELN' TO ls_color-fname. MOVE 3 TO ls_color-color-col. MOVE 0 TO ls_color-color-int. MOVE 0 TO ls_color-color-inv. APPEND ls_color TO lt_color. CLEAR : ls_color. MOVE 'NETWR' TO ls_color-fname. MOVE 3 TO ls_color-color-col. MOVE 0 TO ls_color-color-int. MOVE 0 TO ls_color-color-inv. APPEND ls_color TO lt_color. MOVE lt_color TO -t_color. ENDIF. ENDLOOP. ENDFORM. "set_color

Remarks
Select Query used for this report is only for representational purpose. Performance of the query is not taken into consideration.

Labels
salv cl_salv_table ooabap alv grid

Comments (5)

Contact Us

Site Index

Marketing Opportunities

Legal Terms

Privacy

Impressum

Powered by SAP NetWeaver

6 of 6

09-02-2012 AM 11:45

S-ar putea să vă placă și