Sample Code of Radio Button in Alv Report

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document describes how to add radio button functionality to an ALV report output to allow selecting only one row at a time.

You need to include icons, declare an internal table with an additional field for the radio button, define an event handler class, and prepare the field catalog.

The steps include including icons, declaring a data table with a radio field, defining an event handler class, updating the radio buttons, and preparing the field catalog.

Radio Buttons in the output of an ALV

By Khaja Moulali Shaik, Siemens IT Solutions


As per business need there was a scenario for an ALV Report Output, It must have
functionality selecting only one row at a time. It is possible with Radio buttons.
NOTE: Here we have two types of Radio buttons Icons i.e. icon_wd_radio_button_empty
(Empty Radio Button) and icon_radiobutton (Selected Radio button)
Below are the steps to display Radio Buttons in the output of an ALV.
a) Include ICONS.
INCLUDE <icons>.
b) Declare an internal table with the required fields and additional field called RADIO of
type CHAR of size 4.
c) Define a class to handle an even HOTSPOT_CLICK o trigger when we click on the radio
button icon. Definition as coded below.
* Handles the Even when user clicks on any row.
METHODS: handle_hotspot_click FOR EVENT hotspot_click
OF cl_gui_alv_grid
IMPORTING e_row_id.
d) Do the implementation for the same class to handle the event i.e. if we select a radio
button, then the already selected radio button should be deselect and new radio button
should be selected. Code as follows.
*&--------------------------------------------------------------*
*& METHOD handle_hotspot_click.
*&--------------------------------------------------------------*
*& On double clicking a particulat row
*&--------------------------------------------------------------*
METHOD handle_hotspot_click .
CLEAR : gs_emp.
READ TABLE gt_emp INTO gs_emp
WITH KEY radio = icon_radiobutton.
IF sy-subrc NE 0.
CLEAR gs_emp .
READ TABLE gt_emp INTO gs_emp INDEX e_row_id.
IF gs_emp-radio = icon_radiobutton.
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ELSE.

gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ENDIF.
ELSE .
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX sy-tabix FROM gs_emp
TRANSPORTING radio.
CLEAR gs_emp.
READ TABLE gt_emp INTO gs_emp INDEX e_row_id .
IF sy-subrc = 0.
gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ENDIF.
ENDIF .
CALL METHOD cl_gui_cfw=>set_new_ok_code
EXPORTING
new_code = 'REFRESH'
*
IMPORTING
*
rc
=
.
ENDMETHOD.
"handle_hotspot_click
e) Update the RADIO button with empty radio button in the internal table. Code as follows.
LOOP AT gt_emp INTO gs_emp.
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp FROM gs_emp TRANSPORTING radio.
ENDLOOP.
f)

While preparing the field catalogue, Prepare field catalogue for that additional field
RADIO as coded below.
ls_fieldcat-reptext = 'Radio Button'.
ls_fieldcat-fieldname = 'RADIO'.
ls_fieldcat-ref_table = 'gt_emp'.
ls_fieldcat-icon
= 'X'.
"Icons
ls_fieldcat-hotspot = 'X'.
"Hotspot(Hand Symbol)
ls_fieldcat-col_pos = '1'.

g) Refresh the grid, Perform REFRESH action when the radio button is selected.
* Define local data
DATA:ls_stable TYPE lvc_s_stbl.
ls_stable-row = abap_true.
ls_stable-col = abap_true.
CALL METHOD gv_grid->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 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.

Radio Buttons in the output of an ALV


Click here for the tutorial
*&---------------------------------------------------------------------*
*& Report ZALV_RADIO
*
*&
*
*&---------------------------------------------------------------------*
*& Displays the Radio Button Icons in the output of a an ALV
*
*&
*
*&---------------------------------------------------------------------*
REPORT zalv_radio.
INCLUDE <icons>.
TABLES : pa2001 .
*******************************************************
**Types Declaration
*******************************************************
TYPES: BEGIN OF gty_emp,
pernr TYPE persno ,
subty TYPE subty ,
begda TYPE begda ,

endda TYPE endda ,


abwtg TYPE abwtg ,
radio(4) TYPE c,
END OF gty_emp.
*******************************************************
**Data Declaration
*******************************************************
DATA: go_container TYPE REF TO cl_gui_custom_container.
DATA: ls_layout
TYPE lvc_s_layo ,
ls_fieldcat TYPE lvc_s_fcat ,
lt_fieldcat TYPE lvc_t_fcat .
DATA : gv_grid
TYPE REF TO cl_gui_alv_grid ,
grid_name
TYPE REF TO cl_gui_alv_grid .
DATA: gt_emp TYPE STANDARD TABLE OF gty_emp ,
gs_emp TYPE gty_emp.
DATA: repid TYPE sy-repid.
*----------------------------------------------------------------------*
*
CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION FINAL .
PUBLIC SECTION.
* Handles the Even when user clicks on any row..
METHODS:handle_hotspot_click FOR EVENT hotspot_click OF
cl_gui_alv_grid
IMPORTING
e_row_id .
ENDCLASS.
"lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
*&---------------------------------------------------------------------*
*& METHOD handle_hotspot_click.
*&---------------------------------------------------------------------*
*& On double clicking a particulat row
*&---------------------------------------------------------------------*
METHOD handle_hotspot_click .
CLEAR : gs_emp.
READ TABLE gt_emp INTO gs_emp WITH KEY radio = icon_radiobutton.
IF sy-subrc NE 0.
CLEAR gs_emp .
READ TABLE gt_emp INTO gs_emp INDEX e_row_id.
IF gs_emp-radio = icon_radiobutton.
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ELSE.
gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp TRANSPORTING radio.
ENDIF.
ELSE .
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX sy-tabix FROM gs_emp

TRANSPORTING radio.
CLEAR gs_emp.
READ TABLE gt_emp INTO gs_emp INDEX e_row_id .
IF sy-subrc = 0.
gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ENDIF.
ENDIF .
CALL METHOD cl_gui_cfw=>set_new_ok_code
EXPORTING
new_code = 'REFRESH'
*
IMPORTING
*
rc
=
.
ENDMETHOD .
"handle_hotspot_click
ENDCLASS .
"lcl_event_handler IMPLEMENTATION
*******************************************************
**Initialization Declaration
*******************************************************
INITIALIZATION .
repid = sy-repid.
DATA: go_handler
TYPE REF TO lcl_event_handler .
"Object
for event handler
***To Get data from the database table
PERFORM get_details.
***To Set the empty Radio Button
PERFORM set_empty_radio .
*********************************************************
**Create object alv container
*********************************************************
IF go_container IS INITIAL .
CREATE OBJECT go_container
EXPORTING
container_name = 'GC_CON'.
*********************************************************
**Create object alv Grid
*********************************************************
CREATE OBJECT gv_grid
EXPORTING
i_parent = go_container.
***Perform to design field catalog
PERFORM fieldcatatalog.
ls_layout-cwidth_opt = 'X' .
CREATE OBJECT go_handler .
SET HANDLER:
go_handler->handle_hotspot_click FOR gv_grid .
*********************************************************
**Call method set table for first display
*********************************************************
CALL METHOD gv_grid->set_table_for_first_display
EXPORTING
is_layout
= ls_layout
CHANGING
it_outtab
= gt_emp[]
it_fieldcatalog
= lt_fieldcat[]
EXCEPTIONS

invalid_parameter_combination
program_error
too_many_lines
OTHERS

=
=
=
=

1
2
3
4.

ENDIF.
*&---------------------------------------------------------------------*
*&
Module STATUS_2000 OUTPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
MODULE status_2000 OUTPUT.
SET PF-STATUS 'RADIO'.
SET TITLEBAR 'RADIO'.
ENDMODULE.
" STATUS_2000 OUTPUT
*******************************************************
**Start-of-selection Declaration
*******************************************************
START-OF-SELECTION.
CALL SCREEN 2000.
*&---------------------------------------------------------------------*
*&
Form GET_DETAILS
*&---------------------------------------------------------------------*
*
To get the data from th database table pa2001
*----------------------------------------------------------------------*
FORM get_details .
SELECT pernr
subty
begda
endda
abwtg
FROM pa2001
INTO TABLE gt_emp
UP TO 10 ROWS.
IF sy-subrc = 0.
SORT gt_emp ASCENDING BY pernr.
ENDIF.
ENDFORM.
" GET_DETAILS
*&---------------------------------------------------------------------*
*&
Form FIELDCATATALOG
*&---------------------------------------------------------------------*
*
Building Fieldcatlog for ALV
*----------------------------------------------------------------------*
FORM fieldcatatalog .
REFRESH: lt_fieldcat.
CLEAR: ls_fieldcat.
ls_fieldcat-reptext
= 'Radio Button'.
ls_fieldcat-fieldname = 'RADIO'.
ls_fieldcat-ref_table = 'gt_emp'.
ls_fieldcat-icon
= 'X'.
"Icons
ls_fieldcat-hotspot
= 'X'.
"Hotspot(Hand
Symbol)
ls_fieldcat-col_pos
= '1'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR: ls_fieldcat.
ls_fieldcat-reptext
= 'PERNR'.
ls_fieldcat-fieldname = 'PERNR'.
ls_fieldcat-ref_table = 'gt_emp'.

ls_fieldcat-outputlen = '20'.
ls_fieldcat-col_pos
= '2'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR: ls_fieldcat.
ls_fieldcat-reptext
= 'SUBTY'.
ls_fieldcat-fieldname = 'SUBTY'.
ls_fieldcat-ref_table = 'gt_emp'.
ls_fieldcat-outputlen = '20'.
ls_fieldcat-col_pos
= '3'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR: ls_fieldcat.
ls_fieldcat-reptext
= 'BEGDA'.
ls_fieldcat-fieldname = 'BEGDA'.
ls_fieldcat-ref_table = 'WT_CUST'.
ls_fieldcat-outputlen = '10'.
ls_fieldcat-fix_column = 'X'.
ls_fieldcat-key
= 'X'.
ls_fieldcat-col_pos
= '4'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR: ls_fieldcat.
ls_fieldcat-reptext
= 'ENDDA '.
ls_fieldcat-fieldname = 'ENDDA'.
ls_fieldcat-ref_table = 'WT_CUST'.
ls_fieldcat-outputlen = '30'.
ls_fieldcat-fix_column = 'X'.
ls_fieldcat-key
= 'X'.
ls_fieldcat-col_pos
= '5'.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-reptext
= 'ABWTG'.
ls_fieldcat-fieldname = 'ABWTG' .
ls_fieldcat-ref_table = 'gt_emp'.
ls_fieldcat-outputlen = '20'.
ls_fieldcat-col_pos
= '6'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR: ls_fieldcat.
ENDFORM.
" FIELDCATATALOG
*&---------------------------------------------------------------------*
*&
Module USER_COMMAND_2000 INPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
MODULE user_command_2000 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE PROGRAM .
WHEN 'CANCEL'.
LEAVE PROGRAM .
WHEN 'EXIT'.
LEAVE PROGRAM .
WHEN 'REFRESH' .
grid_name = gv_grid .
PERFORM refresh_alv USING grid_name .
ENDCASE.
ENDMODULE.
" USER_COMMAND_2000 INPUT
*&---------------------------------------------------------------------*
*&
Form SET_EMPTY_RADIO
*&---------------------------------------------------------------------*

*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM set_empty_radio .
LOOP AT gt_emp INTO gs_emp.
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp FROM gs_emp TRANSPORTING radio .
ENDLOOP.
ENDFORM.
" SET_EMPTY_RADIO
*&---------------------------------------------------------------------*
*&
Form REFRESH_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_GRID_NAME text
*----------------------------------------------------------------------*
FORM refresh_alv USING p_grid_name TYPE REF TO cl_gui_alv_grid..
* define local data
DATA:ls_stable TYPE lvc_s_stbl.
ls_stable-row = abap_true.
ls_stable-col = abap_true.
CALL METHOD p_grid_name->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 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.
ENDFORM.
" REFRESH_ALV

You might also like