SALV Various ways to Access Columns

By | Last Updated on July 30, 2014 | 8,730

Various different ways to Access individual columns in ABAP ALV created by SALV object model – Using the column object and using the column table.

Access Individual column using GET_COLUMN

Should be used when you need to change different settings for few columns

 
* Defintion for the T_data and O_SALV
  DATA: t_data TYPE STANDARD TABLE OF faglflext.
  DATA: o_salv TYPE REF TO cl_salv_table.
 
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = o_salv
        CHANGING
          t_table      = t_data ).
    CATCH cx_salv_msg.
  ENDTRY.
 
*----
* Access Columns
*----
* Get the Columns object
* Get colum object using the column name
  DATA: lo_columns  TYPE REF TO cl_salv_columns_table,
        lo_column   TYPE REF TO cl_salv_column_list.
 
  lo_columns = o_salv->get_columns( ).
  TRY.
      lo_column ?= lo_columns->get_column( 'HSL001' ).
      lo_column->set_visible( ' ' ).
    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
  ENDTRY.
 
 
* Displaying the ALV
  o_salv->display( ).
 

Example can be found in SALV Table 7 – Changing Column settings

Access Columns from Column Table

Should be used when you have to set same properties for bunch of columns

 
* Defintion for the T_data and O_SALV
  DATA: t_data TYPE STANDARD TABLE OF faglflext.
  DATA: o_salv TYPE REF TO cl_salv_table.
 
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = o_salv
        CHANGING
          t_table      = t_data ).
    CATCH cx_salv_msg.
  ENDTRY.
 
*----
* Access Columns
*----
* Get the Columns object
* Get columns reference table
* Loop through the table entries to access the column
  DATA: lo_columns  TYPE REF TO cl_salv_columns_table,
        lo_column   TYPE REF TO cl_salv_column_list,
        lt_cols     TYPE        salv_t_column_ref,
        ls_cols     LIKE LINE OF lt_cols.
 
  lo_columns = o_salv->get_columns( ).
  lt_cols    = lo_columns->get( ).
 
  LOOP AT lt_cols INTO ls_cols.
    lo_column ?= ls_cols-r_column.    "Narrow casting
    CASE ls_cols-columnname+0(3).
      WHEN 'HSL' OR 'TSL'.
        lo_column->set_visible( ' ' ).
    ENDCASE.
  ENDLOOP.
 
* Displaying the ALV
  o_salv->display( ).
 

Example used in SALV Table 19 – Columns Specific Grouping

More SALV Tutorial

Advertisement

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 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 274 articles.

Load comments

1 Comment

  • Awesome

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