2011年3月30日 星期三

[Code] RadioButton in ALV

How to create radio button in alv grid report
DATA DEFINITION
TYPE-POOLS: slis, abap, icon.
INCLUDE <icons>.

CONSTANTS: icon_wd_radio_button_empty TYPE icon_l2 VALUE '@SR@'.

DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
alv_container TYPE scrfname VALUE 'ALV_GRID_5000',
alv_grid TYPE REF TO cl_gui_alv_grid,
alv_custom_container TYPE REF TO cl_gui_custom_container,
alv_layout TYPE lvc_s_layo,
alv_field TYPE lvc_s_fcat,
alv_t_field TYPE lvc_t_fcat,
alv_exclude TYPE ui_functions,
alv_dropdown TYPE lvc_t_drop,
alv_variant TYPE disvariant.
Handler
*---------------------------------------------------------------------*
* CLASS alv_event_handler DEFINITION
*---------------------------------------------------------------------*
CLASS alv_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING
e_row_id
e_column_id
es_row_no.

ENDCLASS. "alv_event_handler

*---------------------------------------------------------------------*
* CLASS alv_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS alv_event_handler IMPLEMENTATION.
* For Radio Button
METHOD handle_hotspot_click.
DATA fieldname TYPE lvc_fname.
DATA index LIKE sy-tabix.
DATA alv_s_itab LIKE alv_itab.
DATA ls_stable TYPE lvc_s_stbl.

fieldname = e_column_id-fieldname.
index = es_row_no-row_id.
READ TABLE alv_itab INDEX index INTO alv_s_itab.

CASE fieldname.
WHEN 'SHIPY'.
IF alv_s_itab-shipy = icon_wd_radio_button_empty.
alv_s_itab-shipy = icon_radiobutton.
alv_s_itab-shipn = icon_wd_radio_button_empty.
ENDIF.
WHEN 'SHIPN'.
IF alv_s_itab-shipn = icon_wd_radio_button_empty.
alv_s_itab-shipn = icon_radiobutton.
alv_s_itab-shipy = icon_wd_radio_button_empty.
ENDIF.
ENDCASE.
MODIFY alv_itab FROM alv_s_itab INDEX index.

CLEAR ls_stable.
ls_stable-col = 'X'.
CALL METHOD alv_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
ENDMETHOD. "handle_hotspot_click

ENDCLASS. "alv_event_handler IMPLEMENTATION
Event - INITIALIZATION:
* --------------
* INITIALIZATION
* --------------
INITIALIZATION.
alv_repid = sy-repid.
Print
*&---------------------------------------------------------------------*
*& Form data_print
*&---------------------------------------------------------------------*
FORM data_print.

CREATE OBJECT alv_custom_container
EXPORTING container_name = alv_container.
CREATE OBJECT alv_grid
EXPORTING i_parent = alv_custom_container.

PERFORM alvfield.
PERFORM alvevent.
* Alv_layout
alv_layout-box_fname = 'SELEC'.
alv_layout-cwidth_opt = 'X'.
alv_layout-stylefname = 'CELLTAB'.
alv_layout-info_fname = 'ROWCOLOR'.
* ALV_Variant
alv_variant-report = alv_repid.
alv_variant-username = sy-uname.

CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
is_variant = alv_variant
i_save = 'A'
it_toolbar_excluding = alv_exclude
is_layout = alv_layout
CHANGING
it_fieldcatalog = alv_t_field
it_outtab = alv_itab[].

* Set editable cells to ready for input initially
CALL METHOD alv_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.

ENDFORM. " data_print

*&---------------------------------------------------------------------*
*& Form alvfield
*&---------------------------------------------------------------------*
FORM alvfield .
DATA: lv_tabix LIKE sy-tabix,
it_fieldcat TYPE slis_t_fieldcat_alv.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = alv_repid
i_internal_tabname = 'ALV_ITAB'
i_inclname = alv_repid
CHANGING
ct_fieldcat = it_fieldcat.

CALL FUNCTION 'LVC_TRANSFER_FROM_SLIS'
EXPORTING
it_fieldcat_alv = it_fieldcat
IMPORTING
et_fieldcat_lvc = alv_t_field
TABLES
it_data = alv_itab.

LOOP AT alv_t_field INTO alv_field.
lv_tabix = sy-tabix.
CASE alv_field-fieldname.
WHEN 'SHIPY'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Shipped'.
alv_field-outputlen = 6.
alv_field-icon = 'X'.
alv_field-hotspot = 'X'.
WHEN 'SHIPN'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Non-ship'.
alv_field-outputlen = 6.
alv_field-icon = 'X'.
alv_field-hotspot = 'X'.
ENDCASE.
MODIFY alv_t_field FROM alv_field INDEX lv_tabix.
ENDLOOP.

ENDFORM. " alvfield

*&---------------------------------------------------------------------*
*& Form alvevent
*&---------------------------------------------------------------------*
FORM alvevent .
IF alv_t_events IS INITIAL.
CREATE OBJECT alv_t_events.
ENDIF.

SET HANDLER alv_t_events->handle_hotspot_click FOR alv_grid.
ENDFORM. " alvevent