Displaying fields as BUTTONS and Raising an event in ALV

This code demonstrates how to display fields in ALV as a button.



on clicking a button..



The Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
201
202
203
204
205
*&---------------------------------------------------------------------*
*&Program: ZTEST_SDN
*&Creation Date: 23.08.2008 14:11:22
*&---------------------------------------------------------------------*
*& Demo Program for blog http://abap-explorer.blogspot.com/
*&---------------------------------------------------------------------*
REPORT z_sb_column_as_pushbutton.

TYPE-POOLS:cntl,icon.

TYPES:
BEGIN OF x_final,
carrid TYPE s_carr_id,
carrname TYPE s_carrname,
END OF x_final.

DATA: i_final TYPE STANDARD TABLE OF x_final INITIAL SIZE 0,
i_fieldcat TYPE lvc_t_fcat,
wa_layout TYPE lvc_s_layo,
ok_code TYPE syucomm,
oref_alv TYPE REF TO cl_gui_alv_grid.
field-symbols: <f1> type any.
*----------------------------------------------------------------------*
* CLASS lcl_event_handlers DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handlers DEFINITION.
PUBLIC SECTION.
METHODS:handle_button_click FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING es_col_id es_row_no.
ENDCLASS. "lcl_event_handlers DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_handlers IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handlers IMPLEMENTATION.
METHOD handle_button_click.
DATA l_row TYPE c LENGTH 10.
l_row = es_row_no-row_id.
MESSAGE i001(00) WITH 'You have clicked on column :'
ES_COL_ID-FIELDNAME ',row:' l_row.
ENDMETHOD. "handle_button_click
ENDCLASS. "lcl_event_handlers IMPLEMENTATION


START-OF-SELECTION.

PERFORM get_data.

END-OF-SELECTION.

PERFORM build_field_catalog USING: 'CARRID' 'CARRID' 'SCARR',
'CARRNAME' 'CARRNAME' 'SCARR'.
PERFORM populate_layout.

PERFORM display_alv.

*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Get data
*----------------------------------------------------------------------*
FORM get_data .

SELECT carrid carrname FROM scarr
INTO TABLE i_final
UP TO 200 ROWS.
IF sy-subrc NE 0.

MESSAGE i001(00) WITH 'No data found'.
LEAVE LIST-PROCESSING.
ENDIF.

ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form build_field_catalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_FIELDNAME Fieldname
* -->P_REF_FIELD Reference Field
* -->P_REF_TAB Reference Table
*----------------------------------------------------------------------*
FORM build_field_catalog USING
p_fieldname TYPE lvc_fname
p_ref_field TYPE lvc_rfname
p_ref_tab TYPE lvc_rtname.

DATA:l_fieldcat TYPE lvc_s_fcat.

l_fieldcat-fieldname = p_fieldname.
l_fieldcat-tabname = 'I_FINAL'.
l_fieldcat-ref_field = p_ref_field.
l_fieldcat-ref_table = p_ref_tab.
IF p_fieldname = 'CARRID'.
l_fieldcat-style = cl_gui_alv_grid=>mc_style_button.
ENDIF.

APPEND l_fieldcat TO i_fieldcat.
ENDFORM. " build_field_catalog
*&---------------------------------------------------------------------*
*& Form populate_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM populate_layout.
wa_layout-zebra = 'X'.
ENDFORM. "populate_layout
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_alv.

DATA: l_repid TYPE syrepid VALUE sy-repid,
oref_handlers TYPE REF TO lcl_event_handlers.
DATA l_wa_event TYPE cntl_simple_event.
IF oref_alv IS NOT BOUND.

CREATE OBJECT oref_alv
EXPORTING
* i_shellstyle = 0
* i_lifetime =
i_parent = cl_gui_container=>screen0
* i_appl_events = space
* i_parentdbg =
* i_applogparent =
* i_graphicsparent =
* i_name =
* i_fcat_complete = space
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5
.
IF sy-subrc = 0.

CREATE OBJECT oref_handlers.
SET HANDLER oref_handlers->handle_button_click FOR oref_alv.

CALL METHOD oref_alv->set_table_for_first_display
EXPORTING
* i_buffer_active =
* i_bypassing_buffer =
* i_consistency_check =
* i_structure_name =
* is_variant =
* i_save =
* i_default = 'X'
is_layout = wa_layout
* is_print =
* it_special_groups =
* it_toolbar_excluding =
* it_hyperlink =
* it_alv_graphics =
* it_except_qinfo =
* ir_salv_adapter =
CHANGING
it_outtab = i_final
it_fieldcatalog = i_fieldcat
* it_sort =
* it_filter =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH 'Error while ALV display'.
ENDIF.
ENDIF.
ENDIF.
CALL SCREEN 0100.

ENDFORM. "display_alv
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS '0100'.
SET TITLEBAR '0100'.

ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'BACK'.
CLEAR ok_code.
SET SCREEN 00.
LEAVE SCREEN.

ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT

HTML through ABAP

This code demonstrates how to write HTML code and raise SAP event in ABAP.






The Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
*&---------------------------------------------------------------------*
*&Program: Z_TEST_SOURAV_HTML
*&Creation Date: 20.08.2008 22:08:32
*&---------------------------------------------------------------------*
*& Demo Program for blog http://abap-explorer.blogspot.com/
*&---------------------------------------------------------------------*
REPORT z_test_sourav_html.
*----------------------------------------------------------------------*
* CLASS lcl_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_class DEFINITION.
PUBLIC SECTION.
METHODS:
main,
on_html_event FOR EVENT sapevent OF cl_gui_html_viewer
IMPORTING action frame getdata.
PROTECTED SECTION.

PRIVATE SECTION.
DATA oref TYPE REF TO cl_gui_html_viewer.
ENDCLASS. "lcl_class DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_class IMPLEMENTATION.
METHOD main.
DATA oref_lcl TYPE REF TO lcl_class.
DATA html TYPE w3htmltab.
DATA url TYPE c LENGTH 255.
CREATE OBJECT oref
EXPORTING
parent = cl_gui_container=>screen0.

DATA events TYPE cntl_simple_events.
DATA event TYPE cntl_simple_event.

event-eventid = oref->m_id_sapevent.
event-appl_event = 'X'.
APPEND event TO events.

CALL METHOD oref->set_registered_events
EXPORTING
events = events.
SET HANDLER me->on_html_event FOR oref.

APPEND '<html>' TO html.
APPEND '<body bgcolor= "#FFFFCC">' TO html.
APPEND '<font face="arial" size="2">' TO html.
APPEND '<b>Header</b>' TO html.
APPEND '<br>' TO html.
APPEND 'Text' TO html.
APPEND '</font>' TO html.
APPEND '<form name= "form1" action="SAPEVENT:save">' TO html.
APPEND 'First name:' TO html.
APPEND '<input type="text" name="firstname">' TO html.
APPEND '<br>' TO html.
APPEND 'Last name: ' TO html.
APPEND '<input type="text" name="lastname">' TO html.
APPEND '<br>' TO html.
APPEND '<input type="submit" value="Submit">' TO html.
APPEND '</form>' TO html.
APPEND '</body>' TO html.
APPEND '</html>' TO html.
oref->load_data( IMPORTING assigned_url = url
CHANGING data_table = html ).
oref->show_url( url = url ).
ENDMETHOD. "main
METHOD on_html_event.
DATA:l_string TYPE string.
l_string = getdata.

MESSAGE i001(00) WITH l_string.
ENDMETHOD. "on_html_event
ENDCLASS. "lcl_class IMPLEMENTATION

PARAMETERS: p_dummy TYPE c LENGTH 1.
AT SELECTION-SCREEN OUTPUT.
DATA:lcl_oref TYPE REF TO lcl_class.
IF lcl_oref IS NOT BOUND.
CREATE OBJECT lcl_oref.
CALL METHOD lcl_oref->main.
ENDIF.

Toolbar for a Custom Container

This code demonstrates how we can create a toolbar for Custom Container...I've used a Docking Container.



















The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
*&---------------------------------------------------------------------*
*&Program: ZTEST_DOCKING
*&Creation Date: 20.08.2008 12:43:57
*&---------------------------------------------------------------------*
*& Demo Program for blog http://abap-explorer.blogspot.com/
*&---------------------------------------------------------------------*

REPORT ztest_docking.

TYPE-POOLS:icon.

DATA: docking TYPE REF TO cl_gui_docking_container,
splitter TYPE REF TO cl_gui_easy_splitter_container,
toolbar TYPE REF TO cl_gui_toolbar,
picture TYPE REF TO cl_gui_picture,
events TYPE cntl_simple_events,
event TYPE cntl_simple_event,
url TYPE cndp_url.

*----------------------------------------------------------------------*
* CLASS lcl_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*

CLASS lcl_class DEFINITION.

PUBLIC SECTION.

METHODS:handle_toolbar_selection

FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode .

ENDCLASS. "lcl_class DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*

CLASS lcl_class IMPLEMENTATION.

METHOD handle_toolbar_selection.

CASE fcode.

WHEN 'FIND'.

MESSAGE i001(00) WITH 'You have clicked "FIND" button'.

WHEN 'FINDNX'.

MESSAGE i001(00) WITH 'You have clicked "FINDNX" button'.

WHEN OTHERS.

ENDCASE.
ENDMETHOD. "handle_toolbar_selection
ENDCLASS. "lcl_class IMPLEMENTATION

PARAMETERS : p_para TYPE c LENGTH 1.
DATA:oref TYPE REF TO lcl_class.

AT SELECTION-SCREEN OUTPUT.

IF docking IS NOT BOUND.

CREATE OBJECT docking
EXPORTING
* parent =
repid = sy-repid
dynnr = sy-dynnr
side = docking->dock_at_left
extension = 1000
* style =
* lifetime = lifetime_default
* caption =
* metric = 0
* ratio =
* no_autodef_progid_dynnr =
* name =
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6
.
IF sy-subrc = 0.

CREATE OBJECT splitter
EXPORTING
* link_dynnr =
* link_repid =
* metric = cntl_metric_dynpro
parent = docking
* orientation = 0
sash_position = 4
* with_border = 1
* name =
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
OTHERS = 3
.
IF sy-subrc = 0.

CREATE OBJECT toolbar
EXPORTING
parent = splitter->top_left_container
* shellstyle =
* lifetime =
* display_mode = m_mode_horizontal
* name =
EXCEPTIONS
cntl_install_error = 1
cntl_error = 2
cntb_wrong_version = 3
OTHERS = 4
.

IF sy-subrc = 0.

DATA: l_quickinfo TYPE iconquick.

l_quickinfo = 'Find'.
CALL METHOD toolbar->add_button
EXPORTING
fcode = 'FIND'
icon = icon_search
quickinfo = l_quickinfo
butn_type = cntb_btype_button.


l_quickinfo = 'Find next'.

CALL METHOD toolbar->add_button
EXPORTING
fcode = 'FINDNX'
icon = icon_search_next
quickinfo = l_quickinfo
butn_type = cntb_btype_button.

CLEAR event.
REFRESH events.

event-eventid = toolbar->m_id_function_selected.
event-appl_event = space. " system event

APPEND event TO events.

CALL METHOD toolbar->set_registered_events
EXPORTING
events = events.

CREATE OBJECT oref.
SET HANDLER oref->handle_toolbar_selection
FOR toolbar.

IF picture IS NOT BOUND.
CREATE OBJECT picture
EXPORTING
* lifetime =
* shellstyle =
parent = splitter->bottom_right_container
* name =
EXCEPTIONS
error = 1
OTHERS = 2
.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL METHOD picture->set_display_mode
EXPORTING
display_mode = cl_gui_picture=>display_mode_normal
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'DP_PUBLISH_WWW_URL'
EXPORTING
objid = 'DOCFINDER_LOGO'
lifetime = cndp_lifetime_transaction
IMPORTING
url = url
EXCEPTIONS
dp_invalid_parameters = 1
no_object = 2
dp_error_publish = 3
OTHERS = 4.
IF sy-subrc = 0.
CALL METHOD picture->load_picture_from_url_async
EXPORTING
url = url
EXCEPTIONS
error = 1
OTHERS = 2.

IF sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

Create a SELECT-OPTIONS in a module pool screen

Create a SELECT-OPTIONS in module pool screen using two methods as shown.

Method 1

a) Create a subscreen area in your screen layout where you want to create the select options.
b) In the top include of your module pool program declare a selection screen as a subscreen e.g.

1
2
3
4
5
SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.

select-options s_matnr for mara-matnr.

SELECTION-SCREEN END OF SCREEN.

c) In the PBO and PAI of the main screen where the select options needs to be created do a call subscreen of the above screen (100).

CALL SUBCREEN sub_area INCLUDING <program> <screen>

This CALL SUBSCREEN statement is necessary for transport of values between screen and program.

Note: All validations of the selection screen fields e.g. the s_matnr field created above should be done in selection screen events like AT SELECTION-SCREEN etc and not in PAI. These selection screen validations etc should be done in the top include only.

Method 2

a) Create 2 separate fields in your screen layout - one for the low value and one for the high value. Insert an icon beside the high value which will call the multiple selections popup screen on user command. Use function module COMPLEX_SELECTIONS_DIALOG to achieve this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struc_tab_and_field-fieldname = con_cust.  " 'KUNNR'
struc_tab_and_field-tablename = con_kna1. " 'KNA1'.

CALL FUNCTION 'COMPLEX_SELECTIONS_DIALOG'
EXPORTING
* TITLE = ' '
text = g_titl1 " 'Customers'
tab_and_field = struc_tab_and_field
TABLES
RANGE = rng_kunnr
EXCEPTIONS
NO_RANGE_TAB = 1
CANCELLED = 2
INTERNAL_ERROR = 3
INVALID_FIELDNAME = 4
OTHERS = 5.

IF NOT rng_kunnr[] IS INITIAL.

* Read the very first entry of the range table and pass it to
* dynpro screen field

READ TABLE rng_kunnr INDEX 1.
IF sy-subrc = 0.
g_cust = rng_kunnr-low.
ENDIF.
ENDIF.


You can use the return table rng_kunnr to populate your own internal range table with the values entered by the user. Basically here you are just simulating the work of a select-options parameter by module pool screen elements.