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 = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 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

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image