In this post we are going to see how to add radio buttons in output of an ALV Grid in SAP ABAP
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.