Monday, 18 August 2014

Applying SORTING in Object Oriented ALV ( OO ALV )

Sorting in ALV always means that sort  settings applied for exactly one column , if someone says that
 an ALV display is sorted by 2 fields will simply means that the particular columns is sorted two times ., which is referres to as sort criteria . Sorting is very important functionality while reporting . Similar to filtering and aggregations , the user can do sorting  programmatically as well as using the ALV
 application toolbar functionality .

Similar to all other ALV applications , in order to create sorting you need to follow only 2 steps :
Step 1 : Get the object reference of class CL_SALV_SORTS by calling the method get_sorts( ) of classCL_SALV_TABLE.
lo_sorts = lo_alv->get_sorts( ).

STEP 2 : Call the add_sort( ) method of class CL_SALV_SORTS and passing the column name that
 you want to sort as the parameter in our case it is BELNR (Document Number).
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'.

Please refer to the following source codes :

*******************************************************************
*& Author      : Roushan Kumar
*& Data        : 13/06/2014
*& Description : Sorting in OO ALV
*******************************************************************
REPORT  ztestr_alv_8.
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.
*-- Sorting logic starts
DATA : lo_sorts TYPE REF TO cl_salv_sorts.
*-- Step 1 : get the cl_salv_sorts object ref.
lo_sorts = lo_alv->get_sorts( ).
**-- Step 2 : get sorting for column belnr
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'.
*-- Sorting logic end

lo_alv->display( ).
 

OUTPUT :
Sorting in OO ALV
Sorting in OO ALV























You can also change the individual sortings for a particular column by just following the two steps similar 
to above :
Step 1 : Get the object reference of class CL_SALV_SORT by calling the method get_sort( ) of classCL_SALV_SORTS and passing the column name as parameter .
lo_sort = lo_sorts->get_sort(
                             columnname = 
'BELNR'
                            ).
STEP 2 : Call the set_group ( ) method of class CL_SALV_SORT and passing the parameter .
lo_sort->set_group(
                   
value = if_salv_c_sort=>group_with_underline
                  ).
The  value of interface constants that will pass as the parameter value of method set_group( ) with there
 attributes are as follows :
Interface Constant
Attributes
IF_SALV_C_SORT=>GROUP_WITH_UNDER-LINE
Underlined
IF_SALV_C_SORT=>GROUP_WITH_NEW-PAGE
Page change
IF_SALV_C_SORT=>GROUP_NONE
No Display

Please refer to the below source codes :
*******************************************************************
*& Author      : Roushan Kumar
*& Data        : 13/06/2014
*& Description : Sorting in OO ALV
*******************************************************************
REPORT  ztestr_alv_8.
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.
*-- Sorting logic starts
DATA : lo_sorts TYPE REF TO cl_salv_sorts.
*-- Step 1 : get the cl_salv_sorts object ref.
lo_sorts = lo_alv->get_sorts( ).
**-- Step 2 : get sorting for column belnr
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'
    
position   = 1
    
group      = if_salv_c_sort=>group_with_newpage.
DATA : lo_sort TYPE REF TO cl_salv_sort.

lo_sort = lo_sorts->get_sort(
                             columnname = 
'BELNR'
                            ).

lo_sort->set_group(
                   
value = if_salv_c_sort=>group_none
                  ).
*-- Sorting logic end

lo_alv->display( ).

OUTPUT :

Sorting in OO ALV
Sorting in OO ALV

No comments:

Post a Comment