Вы находитесь на странице: 1из 5

6/18/2019 Let's code ABAP: Create a report using CL_SALV_TABLE in ABAP

Let's code ABAP


My experiences developing in ABAP

March 13, 2015 History

► 2016 ( 1 )
Create a report using CL_SALV_TABLE in ABAP
▼ 2015 ( 21 )
This time we're going to make a report using the CL_SALV_TABLE to create a grid with data.
► November ( 3 )
Let's code it. ► September ( 1 )
► August ( 2 )
1 report zcl_alv_table.
► July ( 3 )
2
3 tables: mara. ► April ( 3 )
4
5 types: begin of ty_material, ▼ March ( 4 )
6 matnr type matnr, Dynamic code generation
7 maktx type maktx, in ABAP
8 end of ty_material.
9 Get reservations in ABAP
10 data it_material type standard table of ty_material.
11
Create a report using
12 *----------------------------------------------------------------------*
CL_SALV_TABLE in
13 * CLASS cl_handler DEFINITION
ABAP
14 *----------------------------------------------------------------------* Table maintenance and
15 * other features
16 *----------------------------------------------------------------------*
17 class cl_handler definition. ► February ( 5 )
18 public section.
19 methods on_double_click for event double_click of cl_salv_events_table
20 importing row column.
21 endclass. "cl_handler DEFINITION
22 Popular Posts
23 *----------------------------------------------------------------------*
24 * CLASS cl_handler IMPLEMENTATION QR Code in ABAP
25 *----------------------------------------------------------------------* This is a very common
26 * situation where you have
27 *----------------------------------------------------------------------* to investigate but also to
28 class cl_handler implementation. improvise in many ways.
29 method on_double_click. Most of the time in SAP the answer for
30 data: wa_st_data type ty_material, lv_count type i. ma...
31
32 if column eq 'MATNR'. Table Maintenance
33 read table it_material into wa_st_data index row. options and ABAP
34 One of the traits I like
35 * Check that material exists about SAP is that you
36 select count( * ) into lv_count from mara up to 1 rows where matnr eq have several facilities to
37 achieve more. One of those
38 if lv_count > 0. " Exists? characteristics is in transaction ‘SE1...
39 * Load parameters
40 set parameter id 'MXX' field 'K'. " Default view Text elements in ABAP
41 set parameter id 'MAT' field wa_st_data-matnr. " Material number
When programming in
42
ABAP there are features
43 call transaction 'MM03' and skip first screen.
that are intrinsical to SAP.
44 else. " No ?
One of those features are
45 data err type string.
' Text elements ', which purpose...
46
47 call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
Create a report using
48 EXPORTING
CL_SALV_TABLE in ABAP
49 input = wa_st_data-matnr
50 IMPORTING This time we're going to make a report
51 output = wa_st_data-matnr. using the CL_SALV_TABLE to create a
52 concatenate `Material ` wa_st_data-matnr ` does not exist.` into err grid with data. Let's code it. 1 2 3 4 5...
53 message err type 'I' display like 'E'.
54 endif. Dynamic code generation
55 else. in ABAP
56 message text-002 type 'I'. " Invalid cell Recently I discovered an
57 endif. excellent feature in ABAP
58 endmethod. "on_double_click which has helped me a lot
59 endclass. "cl_handler IMPLEMENTATION in a requirement I had. This feature is
60 the capacity to genera...
61 selection-screen: begin of block b1 with frame title text-001.
62 select-options: s_matnr for mara-matnr. Clear, refresh and free in
63 selection-screen: end of block b1. ABAP
64 In contrast to other
65 start-of-selection. languages where we
66 perform retrieve_data. need to make use of its
67 perform display. basic value to initialize the variables, in
68 SAP we don't have to make su...
69 *&---------------------------------------------------------------------*
70 *& Form get_data Get consumption values
71 *&---------------------------------------------------------------------* in ABAP
72 * text Hi there, today I will talk
73 *----------------------------------------------------------------------* about consumption in
74 form retrieve_data. SAP and we will examine
75 select matnr maktx from makt up to 100 rows " Retrieve only 100 records some BAPIs (or function modules) to
76 into table it_material get those values. Consumptio...
77 where matnr in s_matnr and spras eq sy-langu.
78 endform. "get_data Print different smartform in MIGO per
79
letscodeabap.blogspot.com/2015/03/create-report-using-clsalvtable-with.html plant in ABAP 1/5
6/18/2019 80 *&---------------------------------------------------------------------*
Let's code ABAP: Create a report using CL_SALV_TABLE in ABAP Hi, sometimes we need to
81 *& Form display print different layouts per
82 *&---------------------------------------------------------------------* plant when using
83 * text transaction 'MIGO'. In
84 *----------------------------------------------------------------------* order to make this we
85 form display. have to crea...
86 data: l_gr_alv type ref to cl_salv_table, " Variables for ALV properties
87 l_gr_functions type ref to cl_salv_functions_list. Locking a row in SM30 in
88 ABAP
89 data: lv_event_handler type ref to cl_handler, " Variables for events Hi, in the previous post I
90 lv_events type ref to cl_salv_events_table. posted an example of
91 how we can unlock
92 data: lr_grid type ref to cl_salv_form_layout_grid, " Variables for header transaction 'SM30', however although
93 lr_layout type ref to cl_salv_form_layout_logo, we now allow other users to...
94 cr_content type ref to cl_salv_form_element,
95 lv_title type string, Classes in ABAP
96 rows type string.
Let's cut to the chase and let's make a
97
program to show how to define classes
98 data: gr_layout type ref to cl_salv_layout, " Variables for enabling Save
in ABAP . 1 2 3 4 5 6 7 8 9 10 ...
99 key type salv_s_layout_key.
100
101 data: lv_display type ref to cl_salv_display_settings. " Variable for layo
102
103 data: lr_selections type ref to cl_salv_selections, " Variables for select Labels
104 lr_columns type ref to cl_salv_columns,
105 lr_column type ref to cl_salv_column_table. abap
106
107 * Create the ALV object abaper
108 try.
109 call method cl_salv_table=>factory badi
110 IMPORTING
111 r_salv_table = l_gr_alv BADI_MATERIAL_CHECK
112 CHANGING
113 t_table = it_material. bapi
114 catch cx_salv_msg.
115 endtry. cast
116
117 * Let's show all default buttons of ALV CL_EXITHANDLER
118 l_gr_functions = l_gr_alv->get_functions( ).
119 l_gr_functions->set_all( abap_true ). cl_salv_table
120
121 * Fit the columns classes
122 lr_columns = l_gr_alv->get_columns( ).
123 lr_columns->set_optimize( 'X' ). clear
124
125 * Create header clear[]
126 describe table it_material lines rows.
127 concatenate 'Number of rows: ' rows into lv_title separated by space. CMOD
128
129 create object lr_grid. consumption
130 create object lr_layout.
131 lr_grid->create_label( row = 1 column = 1 text = lv_title tooltip = lv_tit conversion
132 lr_layout->set_left_content( lr_grid ).
133 cr_content = lr_layout. data
134 l_gr_alv->set_top_of_list( cr_content ).
135 dequeue
136 * Apply zebra style to rows
137 lv_display = l_gr_alv->get_display_settings( ). dynpro
138 lv_display->set_striped_pattern( cl_salv_display_settings=>true ).
139 enqueue
140 * Enable the save layout buttons
141 key-report = sy-repid. events
142 gr_layout = l_gr_alv->get_layout( ).
143 gr_layout->set_key( key ). feature
144 gr_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
145 gr_layout->set_default( abap_true ). fieldname
146
147 * Register events filter
148 lv_events = l_gr_alv->get_event( ).
149 create object lv_event_handler. free
150 set handler lv_event_handler->on_double_click for lv_events.
151 hashed
152 * Enable cell selection mode
153 lr_selections = l_gr_alv->get_selections( ). initialize
154 lr_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
155 inoque
156 try.
157 lr_column ?= lr_columns->get_column( 'MAKTX' ). " Seek the 'MAKTX' col introduction
158 lr_column->set_visible( if_salv_c_bool_sap=>true ).
159 lr_column->set_long_text( 'MyTitle' ). label
160 lr_column->set_medium_text( 'MyTitle' ).
161 lr_column->set_short_text( 'MyTitle' ). lines
162 catch cx_salv_not_found.
163 catch cx_salv_existing. message
164 catch cx_salv_data_error.
165 endtry. messages
166
167 l_gr_alv->display( ). metaprogramming
168 endform. "display
migo

mmbe
First we define a global structure to store the data we are going to show in the ALV.
nace

1 tables: mara. non-unique


2
3 types: begin of ty_material, objects
4 matnr type matnr,
5 maktx type maktx,
letscodeabap.blogspot.com/2015/03/create-report-using-clsalvtable-with.html print 2/5
6/18/2019 6 end of ty_material. Let's code ABAP: Create a report using CL_SALV_TABLE in ABAP
7 qrcode
8 data it_material type standard table of ty_material.
refresh
2. We define a 'handler' class whose purpose is to control the double click event. In the class report
definition we code the signature of the event double click and, in the implementation we code the
action or process that we want to happen when the event is raised. reservations

rest
1 *----------------------------------------------------------------------*
2 * CLASS cl_handler DEFINITION RS_CONV_EX_2_IN
3 *----------------------------------------------------------------------*
4 * s_tabu_lin
5 *----------------------------------------------------------------------*
6 class cl_handler definition. sap
7 public section.
8 methods on_double_click for event double_click of cl_salv_events_table SE18
9 importing row column.
10 endclass. "cl_handler DEFINITION SE24
11
12 *----------------------------------------------------------------------* SE38
13 * CLASS cl_handler IMPLEMENTATION
14 *----------------------------------------------------------------------* selection texts
15 *
16 *----------------------------------------------------------------------* sm30
17 class cl_handler implementation.
18 method on_double_click. smartform
19 data: wa_st_data type ty_material, lv_count type i.
20 SMOD
21 if column eq 'MATNR'.
22 read table it_material into wa_st_data index row. sorted
23
24 * Check that material exists standard
25 select count( * ) into lv_count from mara up to 1 rows where matnr eq w
26 stock
27 if lv_count > 0. " Exists?
28 * Load parameters subroutine pool
29 set parameter id 'MXX' field 'K'. " Default view
30 set parameter id 'MAT' field wa_st_data-matnr. " Material number table
31
32 call transaction 'MM03' and skip first screen. table maintenance
33 else. " No ?
34 data err type string. tabname
35
36 call function 'CONVERSION_EXIT_ALPHA_OUTPUT' text element
37 EXPORTING
38 input = wa_st_data-matnr text symbol
39 IMPORTING
40 output = wa_st_data-matnr. transaction
41 concatenate `Material ` wa_st_data-matnr ` does not exist.` into err.
42 message err type 'I' display like 'E'. unlock
43 endif.
44 else. user-exit
45 message text-002 type 'I'. " Invalid cell
46 endif. VKP1
47 endmethod. "on_double_click
48 endclass. "cl_handler IMPLEMENTATION webservice

work area
In this case we will execute transaction 'MM03' and we will send the material code. Here we make
sure that parameter 'column' is 'MATNR', which is from the structure 'ty_material' that we defined at
the beginning of this program.

Then we verify that the material exists seeking it at table 'MARA'. MARA is a standard table that
stores the materials data.

If the material exists then the next step is to load the transaction settings in memory and finally
execute it.

Otherwise we show a message in the screen to indicate that material doesn't exist.

3. Create the input screen. We have only one parameter; 's_matnr'. S_MATNR is a SELECT-
OPTIONS structure where we can specify diverse criteria in order to use it in a SQL statement or at
internal tables.

1 selection-screen: begin of block b1 with frame title text-001.


2 select-options: s_matnr for mara-matnr.
3 selection-screen: end of block b1.
4
5 start-of-selection.
6 perform retrieve_data.
7 perform display.

4. We then code the forms 'retrieve_data' and 'display'.

1 *&---------------------------------------------------------------------*
2 *& Form get_data
3 *&---------------------------------------------------------------------*
4 * text
5 *----------------------------------------------------------------------*
6 form retrieve_data.
7 select matnr maktx from makt up to 100 rows " Retrieve only 100 records
8 into table it_material
letscodeabap.blogspot.com/2015/03/create-report-using-clsalvtable-with.html 3/5
6/18/2019 9 where matnr in s_matnr and spras eq sy-langu.
Let's code ABAP: Create a report using CL_SALV_TABLE in ABAP
10 endform. "get_data

For example purposes we'll limit 100 max rows of the MARA table in the 'retrieve_data' form.

1 *&---------------------------------------------------------------------*
2 *& Form display
3 *&---------------------------------------------------------------------*
4 * text
5 *----------------------------------------------------------------------*
6 form display.
7 data: l_gr_alv type ref to cl_salv_table, " Variables for ALV properties
8 l_gr_functions type ref to cl_salv_functions_list.
9
10 data: lv_event_handler type ref to cl_handler, " Variables for events
11 lv_events type ref to cl_salv_events_table.
12
13 data: lr_grid type ref to cl_salv_form_layout_grid, " Variables for header
14 lr_layout type ref to cl_salv_form_layout_logo,
15 cr_content type ref to cl_salv_form_element,
16 lv_title type string,
17 rows type string.
18
19 data: gr_layout type ref to cl_salv_layout, " Variables for enabling Save b
20 key type salv_s_layout_key.
21
22 data: lv_display type ref to cl_salv_display_settings. " Variable for layou
23
24 data: lr_selections type ref to cl_salv_selections, " Variables for selecti
25 lr_columns type ref to cl_salv_columns,
26 lr_column type ref to cl_salv_column_table.
27
28 * Create the ALV object
29 try.
30 call method cl_salv_table=>factory
31 IMPORTING
32 r_salv_table = l_gr_alv
33 CHANGING
34 t_table = it_material.
35 catch cx_salv_msg.
36 endtry.
37
38 * Let's show all default buttons of ALV
39 l_gr_functions = l_gr_alv->get_functions( ).
40 l_gr_functions->set_all( abap_true ).
41
42 * Fit the columns
43 lr_columns = l_gr_alv->get_columns( ).
44 lr_columns->set_optimize( 'X' ).
45
46 * Create header
47 describe table it_material lines rows.
48 concatenate 'Number of rows: ' rows into lv_title separated by space.
49
50 create object lr_grid.
51 create object lr_layout.
52 lr_grid->create_label( row = 1 column = 1 text = lv_title tooltip = lv_titl
53 lr_layout->set_left_content( lr_grid ).
54 cr_content = lr_layout.
55 l_gr_alv->set_top_of_list( cr_content ).
56
57 * Apply zebra style to rows
58 lv_display = l_gr_alv->get_display_settings( ).
59 lv_display->set_striped_pattern( cl_salv_display_settings=>true ).
60
61 * Enable the save layout buttons
62 key-report = sy-repid.
63 gr_layout = l_gr_alv->get_layout( ).
64 gr_layout->set_key( key ).
65 gr_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
66 gr_layout->set_default( abap_true ).
67
68 * Register events
69 lv_events = l_gr_alv->get_event( ).
70 create object lv_event_handler.
71 set handler lv_event_handler->on_double_click for lv_events.
72
73 * Enable cell selection mode
74 lr_selections = l_gr_alv->get_selections( ).
75 lr_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
76
77 try.
78 lr_column ?= lr_columns->get_column( 'MAKTX' ). " Seek the 'MAKTX' colum
79 lr_column->set_visible( if_salv_c_bool_sap=>true ).
80 lr_column->set_long_text( 'MyTitle' ).
81 lr_column->set_medium_text( 'MyTitle' ).
82 lr_column->set_short_text( 'MyTitle' ).
83 catch cx_salv_not_found.
84 catch cx_salv_existing.
85 catch cx_salv_data_error.
86 endtry.
87
88 l_gr_alv->display( ).
89 endform. "display

letscodeabap.blogspot.com/2015/03/create-report-using-clsalvtable-with.html 4/5
6/18/2019The Let's code
'display' form is going to show the data in the ALV where weABAP: Create
define a report
several using CL_SALV_TABLE
settings which are in ABAP
documented in the code itself. Just copy and paste it and try it.

An interesting property about CL_SALV_TABLE is that even though you order or filter the grid the
internal table 'it_material' reorganizes itself, so the 'double_click' event will always be accurate to load
the selected material.

Hope it helps.

Please Share it! :)

Posted by Unknown at 5:04 PM

Labels: abap , cl_salv_table , report

1 comment :

Rafael February 6, 2019 at 6:31 AM

Very good! Thanks for sharing!


Reply

Enter your comment...

Comment as: Google Accoun

Publish Preview

Newer Post Home Older Post

Subscribe to: Post Comments ( Atom )

About Author:
Nelson Miranda is a passionate developer who enjoys technology and all programming stuff. Lately developing full time in ABAP. Among other
interests are ASP.Net MVC and Rails.
Follow him @ Twitter | Google+

Powered by Blogger.

letscodeabap.blogspot.com/2015/03/create-report-using-clsalvtable-with.html 5/5

Вам также может понравиться