Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
harsh_raj1
Explorer
12,928

Introduction:

In SAP ABAP (Advanced Business Application Programming), the SAP List Viewer (ALV) provides a powerful tool for displaying data in a tabular format. One of the key classes used for ALV development is the SALV (SAP List Viewer) class. In this blog post, we will explore how to leverage the SALV class to enhance the ALV grid output by incorporating features such as coloring columns, hiding columns, adding icons, and displaying status messages.

In SAP ABAP, if you want to change the color of a particular column, hiding any column, want to add any icons, and traffic lights in a ALV (ABAP List Viewer) list and grid using SALV (SAP List Viewer), you can achieve this by using the CL_SALV_TABLE, cl_salv_functions_list, cl_salv_columns_table, cl_salv_column class and the  color_column, main, fetch_records, display_alv,color_column method. 

Classes used: 

cl_salv_table:  

CL_SALV_TABLE is a class in SAP ABAP (Object-Oriented ABAP) that is used to create and display ALV (ABAP List Viewer) grids. The ALV grid provides a powerful and flexible way to display data in a tabular format with various features such as sorting, filtering, and column resizing. 

cl_salv_columns_table:  

CL_SALV_COLUMNS_TABLE is a class in SAP ABAP (Object-Oriented ABAP) that is used for managing the columns of an ALV (ABAP List Viewer) table created using the SALV (SAP List Viewer) framework. It allows you to manipulate the columns of the ALV table, such as changing the properties of columns, setting filters, and more. 

cl_salv_column: 

This class is responsible for customizing a chosen column individually. To perform some customization on a given column, we have to get an instance reference of the CL_SALV_COLUMN class by calling the method. 

cl_salv_functions_list: 

In ABAP, CL_SALV_FUNCTIONS_LIST is a class used in conjunction with the SALV (SAP List Viewer) framework to manage and control the functions (toolbar buttons) available in an ALV (ABAP List Viewer) grid. This class allows you to define and customize the functions that appear on the toolbar of the ALV grid. 

 Exception used: 

cx_salv_msg: 

In ABAP Object-Oriented Programming (OOABAP), CX_SALV_MSG is an exception class that is used in conjunction with the SALV (SAP List Viewer) framework to handle ALV (ABAP List Viewer) related exceptions. The SALV framework provides a set of classes for creating and working with ALV displays in a flexible and user-friendly way. 

cx_salv_data_error: 

CX_SALV_DATA_ERROR is an exception class in ABAP that is used with the SALV (SAP List Viewer) framework to handle errors related to data processing in an ALV (ABAP List Viewer) grid. This exception is raised when there is an issue with the data during processing. 

cx_salv_not_found: 

CX_SALV_NOT_FOUND is an exception class in ABAP used with the SALV (SAP List Viewer) framework to handle situations where an expected SALV object or element is not found. This exception is typically raised when attempting to access or manipulate a SALV object that hasn't been instantiated or doesn't exist. 

Methods used: 

Factory():  

In OOABAP (Object-Oriented ABAP), the CL_SALV_TABLE class provides a FACTORY method that you can use to create instances of the SALV (SAP List Viewer) framework. The FACTORY method allows you to create a SALV table and configure various settings before displaying the data. 

Display(): 

In OOABAP (Object-Oriented ABAP), the CL_SALV_TABLE class provides a DISPLAY method that is used to display the ALV (ABAP List Viewer) table. This method should be called after configuring the SALV table to ensure that the data is presented to the user in the SAP GUI. 

fetch_records: method used to get data, 
color_column: method used to color particular column, 
display_alv: method used to display data. 

get_alv_instance: to instantiate alv object.

Tables used: 

Sflight:  Flight table 

 code snippet:

 

 

 

 

*&---------------------------------------------------------------------*
*& Report ZHR_RP_SALV_ICONS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zhr_rp_salv_icons.

CLASS lcl_salv_tab DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS : main IMPORTING
                           i_list   TYPE xfeld
                           i_grid   TYPE xfeld
                           i_alv_tb TYPE c,

      fetch_records,

      get_alv_instance IMPORTING
                         i_list   TYPE xfeld
                         i_grid   TYPE xfeld
                         i_alv_tb TYPE c,


      display_alv,

      color_column.

  PRIVATE SECTION.

    TYPE-POOLS : icon, sym.

    TYPES : BEGIN OF ty_flight,

              status TYPE c LENGTH 1,

              icon   TYPE icon_d. " char – 4

              INCLUDE TYPE sflight.

    TYPES : END OF ty_flight.


    CLASS-DATA : lt_flight          TYPE TABLE OF ty_flight,

                 ls_flight          TYPE ty_flight,

                 lo_salv_tab TYPE REF TO cl_salv_table,

                 lo_func     TYPE REF TO cl_salv_functions_list,

                 lo_cols     TYPE REF TO cl_salv_columns_table,

                 lo_col      TYPE REF TO cl_salv_column,


                 lo_col_icon TYPE REF TO cl_salv_column,

                 lo_icon     TYPE REF TO cl_salv_column_table,


                 rem_seat    TYPE i,

                 lt_icon     TYPE TABLE OF icon,

                 ls_icon     TYPE icon.

    CLASS-DATA:  ls_color TYPE lvc_s_colo.

ENDCLASS.



CLASS lcl_salv_tab IMPLEMENTATION.

  METHOD main.

    fetch_records( ).

    get_alv_instance( EXPORTING  i_list     = i_list
                                 i_grid     = i_grid
                                 i_alv_tb   = i_alv_tb ).


    display_alv( ).


  ENDMETHOD.


  METHOD fetch_records.

    DATA indx TYPE sy-tabix.

    DATA line  TYPE i.

SELECT  FROM sflight FIELDS * INTO CORRESPONDING FIELDS OF TABLE @LT_flight UP TO 40 ROWS.        
    line = lines( lt_flight ).

    SELECT  FROM icon FIELDS * INTO TABLE @LT_icon UP TO @line ROWS.


    LOOP AT  lt_flight INTO ls_flight.

      indx = sy-tabix.

      rem_seat = ls_flight-seatsmax_b - ls_flight-seatsocc_b.

      IF  rem_seat = 0.

        ls_flight-status = 1 .

      ELSEIF rem_seat LE 10.

        ls_flight-status = 2.

      ELSE.

        ls_flight-status = 3.

      ENDIF.


      READ TABLE lt_icon INTO ls_icon INDEX indx .

      IF sy-subrc = 0.

        ls_flight-icon = ls_icon-id.

      ENDIF.


      IF indx <> 0.

        MODIFY lt_flight FROM ls_flight INDEX indx TRANSPORTING status icon.

      ENDIF.

      CLEAR ls_flight.

    ENDLOOP.

  ENDMETHOD.


  METHOD get_alv_instance.

    DATA : flag.

    IF i_list = 'X' OR i_grid = 'X'.

      IF i_list = 'X'.

        flag = 'X'.

      ELSE.

        flag = ' '.

      ENDIF.

      TRY.

          CALL METHOD cl_salv_table=>factory
            EXPORTING
              list_display = flag
            IMPORTING
              r_salv_table = lo_salv_tab
            CHANGING
              t_table      = lt_flight.


          IF i_alv_tb = abap_true.

**Begin- Displaying toolbar on alv **

            CALL METHOD lo_salv_tab->get_functions                                                                            
              RECEIVING
                value = lo_func.


            CALL METHOD lo_func->set_default
              EXPORTING
                value = if_salv_c_bool_sap=>true.  "displays toolbar on alv

**End- Displaying toolbar on alv**

          ENDIF.


** Begin – Hides particular column of the table in the list or grid**

          CALL METHOD lo_salv_tab->get_columns " get all cols of table
            RECEIVING
              value = lo_cols.

          TRY.

              CALL METHOD lo_cols->get_column  "get reference to particular  column  
                EXPORTING
                  columnname = 'MANDT'         "hiding particular column
                RECEIVING
                  value      = lo_col.

              CALL METHOD lo_col->set_technical  " true hides the col on ui
                EXPORTING
                  value = if_salv_c_bool_sap=>true.


            CATCH cx_salv_not_found .

          ENDTRY.


** End – Hides particular column of the table in the list or grid**

** Begin – Set status field as traffic icon **

          TRY.

              CALL METHOD lo_cols->set_exception_column
                EXPORTING
                  value = 'STATUS'.

            CATCH cx_salv_data_error .

          ENDTRY.

**End – Set status field as traffic icon **


**Begin – Set icon for the column ICON**

          TRY.

              CALL METHOD lo_cols->get_column " get reference to particular column                                           
                EXPORTING
                  columnname = 'ICON'
                RECEIVING
                  value      = lo_col_icon.

              lo_icon ?= lo_col_icon.

              CALL METHOD lo_icon->set_icon
                EXPORTING
                  value = if_salv_c_bool_sap=>true.

              CALL METHOD lo_icon->set_long_text
                EXPORTING
                  value = 'Icon'.


            CATCH cx_salv_not_found .

          ENDTRY.

**End – Set icon for the column ICON**


        CATCH cx_salv_msg .

      ENDTRY.

    ENDIF.

  ENDMETHOD.


  METHOD display_alv.

    color_column( ).

    CALL METHOD lo_salv_tab->display.

  ENDMETHOD.

  METHOD color_column.
    INCLUDE <color>.    "type pools
    DATA: lo_cols TYPE REF TO cl_salv_columns_table.
    DATA: lo_col TYPE REF TO cl_salv_column_table.
    lo_cols = lo_salv_tab->get_columns( ).
    lo_col ?= lo_cols->get_column( 'CURRENCY' ).   "for particular column color
    ls_color-col = col_positive.
    lo_col->set_color( ls_color ).
  ENDMETHOD.


ENDCLASS.


START-OF-SELECTION.


  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

    PARAMETERS : list     RADIOBUTTON GROUP g1,

                 grid     RADIOBUTTON GROUP g1,

                 alv_tool AS CHECKBOX.


  SELECTION-SCREEN END OF BLOCK b1.


  CALL METHOD lcl_salv_tab=>main
    EXPORTING
      i_list   = list
      i_grid   = grid
      i_alv_tb = alv_tool.

 

 

 

 

In this example, the color of the 'CURRENCY' column is set to green, ‘MANDT’ column is set to be hidden, adding icons and showing status of seat. You can customize the code based on the requirement. 

Output: 

 displaying output in list format:

harsh_123_2-1706611815947.png

harsh_123_0-1706613483171.png

displaying output in grid format:

harsh_123_5-1706611954691.png

harsh_123_1-1706613548800.png

 list display along with toolbar functionality :

harsh_123_8-1706612220798.png

harsh_123_2-1706613680378.png

if you want to display in grid format just select radio-button as grid along with checkbox.

 

 

 

 

 

 

 

 

3 Comments
Labels in this area