SALV Table 17 – Apply Styles to Cell

By | January 26, 2012 | SALV Table, SALV Tutorial | 9,527 | 3

Lets checkout how to apply different styles for cell, yes cell not the entire column. SALV object model does provide us the objects to achieve this.


Today is kind of a SALV day – specifically SALV HOTSPOT day. Fellow developer has posted a question on ABAP Help Forums, Using HOTSPOTs. Similar type of question on HOTSPOT was also asked on the SDN Forum CL_SALV_TABLE set_cell_type_column. But both are using different methods of SALV model to achieve similar functionality.

As you have seen in past, in post SALV Table 8 – Add & Handle Hotspot on how to create the HOTSPOT on the single column. This task is being done by the method SET_CELL_TYPE of the Columns object CL_SALV_COLUMNS_TABLE. We also seen, how we can handle that using the events object of the SALV.

Design Time Consideration

To apply different styles like HOTSPOT, Button, etc on different cells, SALV model has the method SET_CELL_TYPE_COLUMN of the class CL_SALV_COLUMNS_TABLE. To be able to effectively use this method, you need to do this:

  • Add a field with type SALV_T_INT4_COLUMN in the Output table. Here the output table is the table which we would use to generate the output in SALV.
  • Populate the data in the table.
  • Call method SET_CELL_TYPE_COLUMN, to set the name of the field of the style table which we have created in first step.

Since you have added the field in the output table itself, you can easily populate different styles for each row. If you specify the COLUMNNAME while populating the data in the style table, the style would be only applicable to that particular column for the row in which you are populating the styles.

Code Lines

Lets checkout the code lines. For this post, we’ll restrict till only applying different styles on the different cells. I’ll try to cover this in future post.

SALV Table - Various Styles for different Cell

REPORT  ztest_np_temp.
*   Final output table
    TYPES: BEGIN OF ty_vbak,
           vbeln TYPE vbak-vbeln,
           erdat TYPE erdat,
           auart TYPE auart,
           kunnr TYPE kunnr,
           i_celltype TYPE salv_t_int4_column,
           END   OF ty_vbak.
    DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*   ALV reference
    DATA: o_alv TYPE REF TO cl_salv_table.
      get_data,           "  data selection
      generate_output.    "  Generating output
      set_columns.        "  Set columns
ENDCLASS.                    "lcl_report DEFINITION
  DATA: lo_report TYPE REF TO lcl_report.
  CREATE OBJECT lo_report.
  lo_report->get_data( ).
  lo_report->generate_output( ).
  METHOD get_data.
*   data selection
    SELECT vbeln erdat auart kunnr
           FROM  vbak
           UP TO 20 ROWS.
    FIELD-SYMBOLS: <lfs_vbak> LIKE LINE OF t_vbak.
    DATA: lt_celltype TYPE salv_t_int4_column.
    DATA: ls_celltype LIKE LINE OF lt_celltype.
    LOOP AT t_vbak ASSIGNING <lfs_vbak>.
      CLEAR: lt_celltype.
* Only VBELN for 2nd record
      IF sy-tabix = 2.
        ls_celltype-columnname = 'VBELN'.
        ls_celltype-value      = if_salv_c_cell_type=>hotspot.
        APPEND ls_celltype TO lt_celltype.
* Only ERDAT for 3rd record
      ELSEIF sy-tabix = 3.
        ls_celltype-columnname = 'ERDAT'.
        ls_celltype-value      = if_salv_c_cell_type=>button.
        APPEND ls_celltype TO lt_celltype.
* Entire 5th record
      ELSEIF sy-tabix = 5.
        ls_celltype-columnname = ".
        ls_celltype-value      = if_salv_c_cell_type=>hotspot.
        APPEND ls_celltype TO lt_celltype.
      <lfs_vbak>-i_celltype = lt_celltype.
  ENDMETHOD.                    "get_data
  METHOD generate_output.
* New ALV instance
    DATA: lx_msg TYPE REF TO cx_salv_msg.
            r_salv_table = o_alv
            t_table      = t_vbak ).
      CATCH cx_salv_msg INTO lx_msg.
* Setting up the Columns
    me->set_columns( ).
* Displaying the ALV
    o_alv->display( ).
  ENDMETHOD.                    "generate_output
  METHOD set_columns.
*...Get all the Columns
    DATA: lo_cols TYPE REF TO cl_salv_columns_table.
    lo_cols = o_alv->get_columns( ).
*   set the Column optimization
    lo_cols->set_optimize( 'X' ).
*   Set the Cell Type
        lo_cols->set_cell_type_column( 'I_CELLTYPE' ).
      CATCH cx_salv_data_error.                         "#EC NO_HANDLER
  ENDMETHOD.                    "SET_COLUMNS
ENDCLASS.                    "lcl_report IMPLEMENTATION

This code will generate the output similar to this:

SALV Different Styles for Cell

Explore ALL

Check out all SALV related articles at SALV Tutorials

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{271 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 271 articles.

Load comments


  • sid

    Good one!

  • Dimple

    Really good, its been very helpful for me..


  • vijay

    i want to check event for input field in such way that the when user input any one key suppose press ‘v’ then certain event or module get called.
    purpose of my question is that i want to check the key event that when user press a character in mobile no. field then i can erase that character and user have to input only numeric field…..

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.