Anda di halaman 1dari 12

ALV List with Radio Buttons

ALV List with Radio Buttons


Applies to:
Application Server ABAP 6.40

Summary
The program shows how to define radio buttons in ALV grid lists.
Author(s): Uwe Schieferstein
Company: Cirrus Consulting AG, Switzerland
Created on: 11 January 2007

Author Bio
Uwe Schieferstein has 8 years of SAP experience has worked as technical consultant and
developer in Switzerland (Public Sector, Banking; SAP Authorization, Transport Management).
Uwe has a MS in Biochemistry from Eberhard Karls Universitt Tbingen (Germany) and
received a PhD in Molecular Biology from Swiss Federal Institute of Technology Zurich
(Switzerland). He is currently working for Cirrus Consulting AG (Switzerland).

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


1

ALV List with Radio Buttons

Table of Contents
Applies to: ........................................................................................................................................ 1
Summary.......................................................................................................................................... 1
Author Bio ........................................................................................................................................ 1
Report ZALVGRID_WITH_RADIOBUTTONS ................................................................................. 3
Related Content............................................................................................................................. 11
Disclaimer and Liability Notice....................................................................................................... 12

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


2

ALV List with Radio Buttons

Report ZALVGRID_WITH_RADIOBUTTONS
The following screen shot shows how the ALV grid list looks like when using radiobuttons. The radiobuttons
are not a genuine feature of ALV grid lists but are simulated using the corresponding icons and the
HOTSPOT_CLICK event.

*&---------------------------------------------------------------------*
*& Report ZALVGRID_WITH_RADIOBUTTONS
*&
*&---------------------------------------------------------------------*
*& This program shows how to realize radiobuttons in ALV grid lists
*& using event HOTSPOT_CLICK.
*&
*&---------------------------------------------------------------------*
*& Screen 100:
*& - Flow logic
*&
*&
PROCESS BEFORE OUTPUT.
*&
MODULE PBO.

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


3

ALV List with Radio Buttons

*&*
*&
PROCESS AFTER INPUT.
*&
MODULE PAI.
*&
*& - Screen elements: none
*& - ok-code field -> gd_okcode
*&
*& GUI Status MAIN100:
*& - F3 = 'BACK', Shift+F3 = 'EXIT', F12 = 'CANC'
*&---------------------------------------------------------------------*
PROGRAM zalvgrid_with_radiobuttons.

TYPE-POOLS: abap, icon.

" INCLUDE <icon>. for releases < 6.20

TYPES: BEGIN OF ty_s_sflight.


INCLUDE TYPE sflight.
TYPES: button1
TYPE iconname.
TYPES: button2
TYPE iconname.
TYPES: button3
TYPE iconname.
TYPES: button4
TYPE iconname.
TYPES: END OF ty_s_sflight.
DATA:
gt_sflight
*
gs_layout
gt_fcat
DATA:
gd_okcode
go_docking
go_grid

TYPE STANDARD TABLE OF ty_s_sflight,


TYPE lvc_s_layo,
TYPE lvc_t_fcat.

TYPE ui_func,
TYPE REF TO cl_gui_docking_container,
TYPE REF TO cl_gui_alv_grid.

*---------------------------------------------------------------------*
*
CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
handle_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING
e_row_id
e_column_id
es_row_no
sender.
ENDCLASS.

"lcl_eventhandler DEFINITION

*---------------------------------------------------------------------*
*
CLASS lcl_eventhandler IMPLEMENTATION

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


4

ALV List with Radio Buttons

*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.
METHOD handle_hotspot_click.
* define local data
FIELD-SYMBOLS:
<ls_entry>
TYPE ty_s_sflight,
<ld_fld>
TYPE ANY.
READ TABLE gt_sflight ASSIGNING <ls_entry> INDEX es_row_no-row_id.
CHECK ( <ls_entry> IS ASSIGNED ).
*

*
*

Set all radio buttons


<ls_entry>-button1 =
<ls_entry>-button2 =
<ls_entry>-button3 =
<ls_entry>-button4 =

"unselected"
icon_wd_radio_button_empty.
icon_wd_radio_button_empty.
icon_wd_radio_button_empty.
icon_wd_radio_button_empty.

ASSIGN COMPONENT e_column_id-fieldname OF STRUCTURE <ls_entry>


TO <ld_fld>.
IF ( <ld_fld> IS ASSIGNED ).
Set selected radio button "selected".
<ld_fld> = icon_wd_radio_button.
ENDIF.
Force PAI followed by refresh of table display in PBO
CALL METHOD cl_gui_cfw=>set_new_ok_code
EXPORTING
new_code = 'REFRESH'
IMPORTING
RC
=
.
ENDMETHOD.

ENDCLASS.

"handle_hotspot_click
"lcl_eventhandler IMPLEMENTATION

*---------------------------------------------------------------------*
*
MAIN
*
*---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM select_data.
PERFORM init_controls.
PERFORM build_fieldcatalog.
PERFORM set_layout.
CALL METHOD go_grid->set_table_for_first_display
EXPORTING
*
i_structure_name = 'SFLIGHT'
is_layout
= gs_layout
CHANGING
it_fieldcatalog = gt_fcat
it_outtab
= gt_sflight.
* Link docking container to dynpro
CALL METHOD go_docking->link
EXPORTING
repid
= syst-repid

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


5

ALV List with Radio Buttons

dynnr
= '0100'
CONTAINER
=
EXCEPTIONS
cntl_error
= 1
cntl_system_error
= 2
lifetime_dynpro_dynpro_link = 3
OTHERS
= 4.
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 SCREEN 100.


END-OF-SELECTION.

*---------------------------------------------------------------------*
*
MODULE PBO OUTPUT
*
*---------------------------------------------------------------------*
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'MAIN100'.
ENDMODULE.
"PBO OUTPUT
*---------------------------------------------------------------------*
*
MODULE PAI INPUT
*
*---------------------------------------------------------------------*
MODULE pai INPUT.
* Leave report
CASE gd_okcode.
WHEN 'BACK' OR
'EXIT' OR
'CANC'.
SET SCREEN 0. LEAVE SCREEN.
*

Refresh table display


WHEN 'REFRESH'.
PERFORM refresh_display.

WHEN OTHERS.
do nothing
ENDCASE.
CLEAR gd_okcode.
ENDMODULE.
*

"PAI INPUT

*&---------------------------------------------------------------------*
*&
Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


6

ALV List with Radio Buttons

* define local data


DATA:
ls_fcat
TYPE lvc_s_fcat.

*
*
*
*

*
*
*

*
*
*
*

*
*

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'


EXPORTING
I_BUFFER_ACTIVE
=
i_structure_name
= 'ICON'
I_CLIENT_NEVER_DISPLAY
= 'X'
I_BYPASSING_BUFFER
=
I_INTERNAL_TABNAME
=
CHANGING
ct_fieldcat
= gt_fcat
EXCEPTIONS
inconsistent_interface
= 1
program_error
= 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
DELETE gt_fcat WHERE ( fieldname <> 'NAME' ).
NOTE: field ICON-NAME has data element ICONNAME.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_BUFFER_ACTIVE
=
i_structure_name
= 'SFLIGHT'
I_CLIENT_NEVER_DISPLAY
= 'X'
I_BYPASSING_BUFFER
=
I_INTERNAL_TABNAME
=
CHANGING
ct_fieldcat
= gt_fcat
EXCEPTIONS
inconsistent_interface
= 1
program_error
= 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
READ TABLE gt_fcat INTO ls_fcat
WITH KEY fieldname = 'NAME'.
IF ( syst-subrc = 0 ).
DELETE gt_fcat INDEX syst-tabix.
ENDIF.
ls_fcat-fieldname = 'BUTTON4'.
ls_fcat-coltext
= ls_fcat-fieldname.
ls_fcat-icon
= 'X'.
ls_fcat-hotspot = 'X'.
INSERT ls_fcat INTO gt_fcat INDEX 5.

ls_fcat-fieldname = 'BUTTON3'.
ls_fcat-coltext
= ls_fcat-fieldname.
INSERT ls_fcat INTO gt_fcat INDEX 5.
ls_fcat-fieldname = 'BUTTON2'.
ls_fcat-coltext
= ls_fcat-fieldname.
INSERT ls_fcat INTO gt_fcat INDEX 5.

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


7

ALV List with Radio Buttons

ls_fcat-fieldname = 'BUTTON1'.
ls_fcat-coltext
= ls_fcat-fieldname.
INSERT ls_fcat INTO gt_fcat INDEX 5.

* Renumbering of the columns


LOOP AT gt_fcat INTO ls_fcat.
ls_fcat-col_pos = syst-tabix.
MODIFY gt_fcat FROM ls_fcat INDEX syst-tabix.
ENDLOOP.

ENDFORM.

" BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*&
Form SELECT_DATA
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM select_data .
* define local data
DATA:
ls_sflight
TYPE ty_s_sflight.
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight.
ls_sflight-button1 = icon_wd_radio_button. " selected radiobutton
ls_sflight-button2 = icon_wd_radio_button_empty.
ls_sflight-button3 = icon_wd_radio_button_empty.
ls_sflight-button4 = icon_wd_radio_button_empty.
* Alternatively: create icons using function module 'ICON_CREATE'
* on SAP releases where these icons are not available.

MODIFY gt_sflight FROM ls_sflight


TRANSPORTING button1 button2 button3 button4
WHERE ( carrid IS NOT INITIAL ).
ENDFORM.

" SELECT_DATA

*&---------------------------------------------------------------------*
*&
Form INIT_CONTROLS
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM init_controls .
CHECK ( go_docking IS NOT BOUND ).

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


8

ALV List with Radio Buttons

* Create docking container


CREATE OBJECT go_docking
EXPORTING
parent
= cl_gui_container=>screen0
*
REPID
=
*
DYNNR
=
*
SIDE
= DOCK_AT_LEFT
*
EXTENSION
= 50
*
STYLE
=
*
LIFETIME
= lifetime_default
*
CAPTION
=
*
METRIC
= 0
ratio
= 90
*
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.
*
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Size of container = full screen size
CALL METHOD go_docking->set_extension
EXPORTING
extension = 99999
EXCEPTIONS
cntl_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.
* Create ALV grid instance
CREATE OBJECT go_grid
EXPORTING
*
I_SHELLSTYLE
= 0
*
I_LIFETIME
=
i_parent
= go_docking
*
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.
*
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


9

ALV List with Radio Buttons

Set event handler for event HOTSPOT_CLICK


SET HANDLER:
lcl_eventhandler=>handle_hotspot_click FOR go_grid.

ENDFORM.

" INIT_CONTROLS

*&---------------------------------------------------------------------*
*&
Form REFRESH_DISPLAY
*&---------------------------------------------------------------------*
*
Refresh table display after switching the radiobuttons
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM refresh_display .
* define local data
DATA:
ls_stable
TYPE lvc_s_stbl.
ls_stable-row = abap_true.
ls_stable-col = abap_true.
CALL METHOD go_grid->refresh_table_display
EXPORTING
is_stable
= ls_stable
*
I_SOFT_REFRESH =
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_DISPLAY

*&---------------------------------------------------------------------*
*&
Form SET_LAYOUT
*&---------------------------------------------------------------------*
*
Set layout for ALV list
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM set_layout .
CLEAR: gs_layout.
gs_layout-cwidth_opt = abap_true.
gs_layout-zebra
= abap_true.
ENDFORM.

" SET_LAYOUT

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

" optimize column width

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


10

ALV List with Radio Buttons

Related Content

Adding Multiple check boxes to ALV and should select only one

Events of Class CL_GUI_ALV_GRID

Methods of the OO Control Framework

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


11

ALV List with Radio Buttons

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces
and therefore is not supported by SAP. Changes made based on this information are not supported and can
be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods
suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of
this technical article or code sample, including any liability resulting from incompatibility between the content
within this document and the materials and services offered by SAP. You agree that you will not hold, or
seek to hold, SAP responsible or liable with respect to the content of this document.

SAP DEVELOPER NETWORK | sdn.sap.com


2007 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


12