Friday 13 March 2015

Enabling Selection Mode in Object Oriented ALV ( OO ALV )

Object Oriented ALV using CL_SALV_TABLE also provide the factility to the programmer to determine whether and in which situation the user can select areas of the ALV output .it is possible to programmatically determine which area needs to be selected and which areas are not required to select. The user can select multiple columns and multiple rows in an Object Oriented ALV .

Similar to all other ALV applications , in order to achive this functionality you need to follow only 2 steps :
Step 1 : Get the object reference of class CL_SALV_SELECTIONS by calling the method get_selections( ) of class CL_SALV_TABLE.
DATA : lo_selections TYPE REF TO cl_salv_selections.
lo_selections = lo_alv->get_selections( ).
STEP 2 : Call the set_selection_mode( ) method of class CL_SALV_SELECTIONS
CALL METHOD lo_selections->set_selection_mode
EXPORTING
value = if_salv_c_selection_mode=>multiple.
Please refer to the following source codes :

REPORT ztestr_alv_9.
TYPES : BEGIN OF gy_bseg,
bukrs TYPE bukrs,
belnr TYPE belnr_d,
gjahr TYPE gjahr,
buzei TYPE buzei,
wrbtr TYPE wrbtr,
END OF gy_bseg.
DATA : gt_bseg TYPE STANDARD TABLE OF gy_bseg INITIAL SIZE 1.
REFRESH : gt_bseg[].
SELECT bukrs belnr gjahr buzei wrbtr
FROM bseg
INTO TABLE gt_bseg
UP TO 10 ROWS.
IF sy-subrc IS INITIAL.
SORT gt_bseg BY bukrs belnr gjahr buzei .
ENDIF.
DATA : lo_alv TYPE REF TO cl_salv_table.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = gt_bseg.
CATCH cx_salv_msg .
ENDTRY.
*-- Enabling Selection Mode logic starts
DATA : lo_selections TYPE REF TO cl_salv_selections.
lo_selections = lo_alv->get_selections( ).
CALL METHOD lo_selections->set_selection_mode
EXPORTING
value = if_salv_c_selection_mode=>multiple.
*-- Enabling Selection Mode logic end
lo_alv->display( ).

HOTSPOT/SINGLE CLICK AREA IN ( OO ALV ) IN SAP ABAP .

In this SAP ABAP tutorial I will show you how to make hospot or single click area in Object Oriented ALV ( OO ALV ) using CL_SALV_TABLE.

To achieve this functionality you need to call the method set_cell_type of class cl_salv_column_table and pass the value as if_salv_c_cell_type=>hotspot . Now clicking on the checkbox will trigger the event LINK_CLICK , so we need to declare and event handler class here lcl_event_handler and declare a method m_link_click to handle the action . The LINK_CLICK event will give us the row and column value of the selected row , so that we can do further process based on the value of that field.
NB : Hotspot coding is similar to the checkbox one of.my previous tutorials except instead of if_salv_c_cell_type=>checkbox_hotspot we need to pass if_salv_c_cell_type=>hotspot .
At the end we need to call the REFRESH( ) method of class CL_SALV_TABLE .
In this SAP ABAP tutorial example we are making the column vbeln values as hotspot so that when the user single click on the value it will lead us to the transaction ‘VL03N’ with the corresponding VBELN value.
Please refer to the following source codes :

REPORT  ztestr_alv_10.
TYPES : BEGIN OF gy_vbap,
          vbeln TYPE vbeln_va,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
END OF   gy_vbap.
DATA : gt_vbap TYPE STANDARD TABLE OF gy_vbap INITIAL SIZE 1.
DATA : lo_alv TYPE REF TO cl_salv_table.
REFRESH : gt_vbap[].
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
      m_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
ENDCLASS.                    "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
METHOD m_link_click.
FIELD-SYMBOLS : <lfs_vbap> TYPE gy_vbap.
READ TABLE gt_vbap ASSIGNING <lfs_vbap> INDEX row.
IF sy-subrc IS INITIAL.
SET PARAMETER ID 'VL' FIELD <lfs_vbap>-vbeln.
CALL TRANSACTION 'VL03N'.
ENDIF.
    lo_alv->refresh( ).
ENDMETHOD.                    "m_link_click
"m_link_click
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION
START-OF-SELECTION.
SELECT vbeln posnr matnr
FROM vbap
INTO TABLE gt_vbap
UP TO 10 ROWS.
IF sy-subrc IS INITIAL.
SORT gt_vbap BY vbeln posnr matnr  .
ENDIF.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
          r_salv_table = lo_alv
CHANGING
          t_table      = gt_vbap.
CATCH cx_salv_msg .
ENDTRY.
*-- Checkbox logic starts
DATA : lo_columns TYPE REF TO cl_salv_columns_table.
  lo_columns = lo_alv->get_columns( ).
  lo_columns->set_optimize( 'X' ).
DATA : lo_column TYPE REF TO cl_salv_column_table.
TRY.
      lo_column ?= lo_columns->get_column( 'VBELN' ).
      lo_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
      lo_column->set_output_length( 10 ).
CATCH cx_salv_not_found.
ENDTRY.
DATA: lo_events TYPE REF TO cl_salv_events_table.
  lo_events = lo_alv->get_event( ).
DATA: lo_event_handler TYPE REF TO lcl_event_handler.
CREATE OBJECT lo_event_handler.
SET HANDLER lo_event_handler->m_link_click FOR lo_events.
*-- Checkbox logic end
  lo_alv->display( ).