Enhancing ALV Grid Functionality: Sorting and Filtering

 

ALV Features :-

1. Sorting :-

  1. Users can arrange data in ascending or descending order by clicking on column headers.
  2. It supports sorting based on multiple columns simultaneously.
  3. Default sorting criteria can be predefined by developers.
  4. Sorting can be controlled programmatically within the application.
  5. Users can choose whether to enable or disable sorting functionality.

Requirement :-

  • Suppose, we have the below ALV program.

Code :-

*************************************************
*Start of Program
*Type Strcuture fo Both header and Item table
TYPES : BEGIN OF ty_vbak,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
        END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
          vbeln TYPE vbeln_va,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_vbap.

********************************************
*Type structure for final table.
TYPES : BEGIN OF ty_final,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_final.

*************************************************
*Declaring the internal table and work area
DATA : lt_vbak  TYPE TABLE OF ty_vbak,
       ls_vbak  TYPE ty_vbak,
       lt_vbap  TYPE TABLE OF ty_vbap,
       ls_vbap  TYPE ty_vbap,
       lt_final TYPE TABLE OF ty_final,
       ls_final TYPE ty_final,
       lv_vbeln TYPE vbeln_va.

***************
*Declaring a select option.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.

START-OF-SELECTION.
  SELECT vbeln erdat erzet ernam vbtyp
    FROM vbak INTO TABLE lt_vbak
    WHERE vbeln IN s_vbeln.

  IF lt_vbak IS NOT INITIAL.
    SELECT vbeln posnr matnr
      FROM vbap
      INTO TABLE lt_vbap
      FOR ALL ENTRIES IN lt_vbak
      WHERE vbeln = lt_vbak-vbeln.

    LOOP AT lt_vbap INTO ls_vbap.
      ls_final-posnr = ls_vbap-posnr.
      ls_final-matnr = ls_vbap-matnr.

      READ TABLE lt_vbak INTO ls_vbak WITH KEY vbeln = ls_vbap-vbeln.
      IF sy-subrc EQ 0.
        ls_final-vbeln = ls_vbak-vbeln.
        ls_final-erdat = ls_vbak-erdat.
        ls_final-erzet = ls_vbak-erzet.
        ls_final-ernam = ls_vbak-ernam.
        ls_final-vbtyp = ls_vbak-vbtyp.
      ENDIF.

      APPEND ls_final TO lt_final.
      CLEAR : ls_final, ls_vbak, ls_vbap.
    ENDLOOP.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
*   I_CALLBACK_PROGRAM                = ' '
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
   I_STRUCTURE_NAME                  = 'ZAR_ORDER_DETAILS'
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
*   IT_FIELDCAT                       =
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
*   O_PREVIOUS_SRAL_HANDLER           =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
  TABLES
    t_outtab                          = lt_final
 EXCEPTIONS
   PROGRAM_ERROR                     = 1
   OTHERS                            = 2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

  ENDIF.

*****************
*End of Program
*****************************************************
  • On executing the program, we are getting a simple ALV in form of grid display.



  • Now the customer wants that the output should be displayed in the descending order based upon creation date.

Solution :-

  • Step 1 :- We can see, we have it_sort parameter in the function module, built for the same kind of purpose.



  • Step 2 :- We will declare a internal table and work area for the same purpose.





  • Step 3 :- Write down the below logic.



Code :-

******************************************************************************
*Start of Program
*Type Strcuture fo Both header and Item table
TYPES : BEGIN OF ty_vbak,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
        END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
          vbeln TYPE vbeln_va,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_vbap.

********************************************
*Type structure for final table.
TYPES : BEGIN OF ty_final,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_final.

*************************************************
*Declaring the internal table and work area
DATA : lt_vbak  TYPE TABLE OF ty_vbak,
       ls_vbak  TYPE ty_vbak,
       lt_vbap  TYPE TABLE OF ty_vbap,
       ls_vbap  TYPE ty_vbap,
       lt_final TYPE TABLE OF ty_final,
       ls_final TYPE ty_final,
       lv_vbeln TYPE vbeln_va.

DATA : lt_sort type table of slis_sortinfo_alv,
       ls_sort type slis_sortinfo_alv.

***************
*Declaring a select option.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.

START-OF-SELECTION.
  SELECT vbeln erdat erzet ernam vbtyp
    FROM vbak INTO TABLE lt_vbak
    WHERE vbeln IN s_vbeln.

  IF lt_vbak IS NOT INITIAL.
    SELECT vbeln posnr matnr
      FROM vbap
      INTO TABLE lt_vbap
      FOR ALL ENTRIES IN lt_vbak
      WHERE vbeln = lt_vbak-vbeln.

    LOOP AT lt_vbap INTO ls_vbap.
      ls_final-posnr = ls_vbap-posnr.
      ls_final-matnr = ls_vbap-matnr.

      READ TABLE lt_vbak INTO ls_vbak WITH KEY vbeln = ls_vbap-vbeln.
      IF sy-subrc EQ 0.
        ls_final-vbeln = ls_vbak-vbeln.
        ls_final-erdat = ls_vbak-erdat.
        ls_final-erzet = ls_vbak-erzet.
        ls_final-ernam = ls_vbak-ernam.
        ls_final-vbtyp = ls_vbak-vbtyp.
      ENDIF.

      APPEND ls_final TO lt_final.
      CLEAR : ls_final, ls_vbak, ls_vbap.
    ENDLOOP.

    ls_sort-fieldname = 'ERDAT'.
    ls_sort-down = ABAP_TRUE.
    append ls_sort to lt_sort.
    clear ls_sort.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
*   I_CALLBACK_PROGRAM                = ' '
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
   I_STRUCTURE_NAME                  = 'ZAR_ORDER_DETAILS'
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
*   IT_FIELDCAT                       =
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
   IT_SORT                           = lt_sort
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
*   O_PREVIOUS_SRAL_HANDLER           =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
  TABLES
    t_outtab                          = lt_final
 EXCEPTIONS
   PROGRAM_ERROR                     = 1
   OTHERS                            = 2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

  ENDIF.

*****************
*End of Program
*******************************************************************

Execute the code :-



  • You can see the output is in the descending order based upon creation date.

2. Filter :-

  1. Users can filter data within columns by entering filter criteria.
  2. Various filter types such as text, numeric, and date ranges are supported.
  3. Custom filter logic can be implemented based on specific requirements.
  4. Filters can be applied dynamically based on user interactions or predefined conditions.
  5. Filter settings can persist across sessions for a consistent user experience.

Requirement :-

  • Now, suppose if the customer wants that, only those data should be present in the ALV where Created by = S419AH24.

Solution :-

  • Step 1 :- Uncomment the below parameter.


  • Step 2 :- Write down the internal table and work area for it.



  • Step 3 :- Write down the logic for it.



Code :-

******************************************************************************
*Start of Program
*Type Strcuture fo Both header and Item table
TYPES : BEGIN OF ty_vbak,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
        END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
          vbeln TYPE vbeln_va,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_vbap.

********************************************
*Type structure for final table.
TYPES : BEGIN OF ty_final,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_final.

*************************************************
*Declaring the internal table and work area
DATA : lt_vbak  TYPE TABLE OF ty_vbak,
       ls_vbak  TYPE ty_vbak,
       lt_vbap  TYPE TABLE OF ty_vbap,
       ls_vbap  TYPE ty_vbap,
       lt_final TYPE TABLE OF ty_final,
       ls_final TYPE ty_final,
       lv_vbeln TYPE vbeln_va.

DATA : lt_filter type table of slis_filter_alv,
       ls_filter type slis_filter_alv.

***************
*Declaring a select option.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.

START-OF-SELECTION.
  SELECT vbeln erdat erzet ernam vbtyp
    FROM vbak INTO TABLE lt_vbak
    WHERE vbeln IN s_vbeln.

  IF lt_vbak IS NOT INITIAL.
    SELECT vbeln posnr matnr
      FROM vbap
      INTO TABLE lt_vbap
      FOR ALL ENTRIES IN lt_vbak
      WHERE vbeln = lt_vbak-vbeln.

    LOOP AT lt_vbap INTO ls_vbap.
      ls_final-posnr = ls_vbap-posnr.
      ls_final-matnr = ls_vbap-matnr.

      READ TABLE lt_vbak INTO ls_vbak WITH KEY vbeln = ls_vbap-vbeln.
      IF sy-subrc EQ 0.
        ls_final-vbeln = ls_vbak-vbeln.
        ls_final-erdat = ls_vbak-erdat.
        ls_final-erzet = ls_vbak-erzet.
        ls_final-ernam = ls_vbak-ernam.
        ls_final-vbtyp = ls_vbak-vbtyp.
      ENDIF.

      APPEND ls_final TO lt_final.
      CLEAR : ls_final, ls_vbak, ls_vbap.
    ENDLOOP.

    ls_filter-fieldname = 'ERNAM'.
    ls_filter-tabname = 'LT_FINAL'.
    ls_filter-sign0 = 'I'.
    ls_filter-optio = 'EQ'.
    ls_filter-valuf_int = 'S419AH24'.
    APPEND ls_filter to lt_filter.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
*   I_CALLBACK_PROGRAM                = ' '
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
   I_STRUCTURE_NAME                  = 'ZAR_ORDER_DETAILS'
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
*   IT_FIELDCAT                       =
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
   IT_FILTER                         = lt_filter
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
*   O_PREVIOUS_SRAL_HANDLER           =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
  TABLES
    t_outtab                          = lt_final
 EXCEPTIONS
   PROGRAM_ERROR                     = 1
   OTHERS                            = 2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

  ENDIF.

*****************
*End of Program
*******************************************************************

Execute the program :-



Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP