SALV Making All Columns Invisible from A Specific Type

By | Last Updated on September 11, 2013 | 6,431

Code Snippet to access all columns from a Type when working with SALV table using RTTS (Run Time Type Services).

RTTS is very handy when working with components from a specific Type. One of reader, Jack Sugrue has recently sent me an email for a solution for hiding the many columns from the specific type. This is the code snippet with few more details which I sent to Jack earlier.

Making all Columns Invisible

 
DATA: t_mara TYPE STANDARD TABLE OF mara.
DATA: o_salv TYPE REF TO cl_salv_table.
DATA: t_fields TYPE STANDARD TABLE OF char30.
 
START-OF-SELECTION.
  SELECT * FROM mara
    INTO TABLE t_mara
    UP TO 10 ROWS.
*
  TRY.
    cl_salv_table=>factory(
      IMPORTING
        r_salv_table = o_salv
      CHANGING
        t_table      = t_mara ).
  ENDTRY.
*======
* ---  Making all columns invisible
* Get the components using the Structure Descriptor
* For each component, call the respective method from SALV model
*
  DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table.
  DATA: lo_column TYPE REF TO cl_salv_column_table.
  DATA: lo_struct   TYPE REF TO cl_abap_structdescr,
        lt_comp     TYPE cl_abap_structdescr=>component_table,
        ls_comp     LIKE LINE OF lt_comp,
        lt_comp_c   TYPE abap_component_view_tab,
        ls_comp_c   LIKE LINE OF lt_comp_c,
        lv_type_name TYPE char30,
        lv_field    TYPE char30.
*
  lo_cols_tab = o_salv->get_columns( ).
*
  lv_type_name = 'MARA'.
  lo_struct ?= cl_abap_typedescr=>describe_by_name( lv_type_name ).
* If type is not deep, use the method GET_COMPONETS to get the components
* When type is deep, use the method GET_INCLUDED_VIEW to get all
*   the components
  "lt_comp  = lo_struct->get_components( ).
  lt_comp_c  = lo_struct->get_included_view( ).
  LOOP AT lt_comp_c INTO ls_comp_c.
    lv_field = ls_comp_c-name.
    TRY.
        lo_column ?= lo_cols_tab->get_column( lv_field ).
        lo_column->set_visible( abap_false ).
      CATCH cx_salv_not_found.
    ENDTRY.
  ENDLOOP.
*========
*
*==== Putting back few columns
* Display the required few columns
  APPEND 'MATNR' TO t_fields.
  APPEND 'MTART' TO t_fields.
*
  LOOP AT t_fields INTO lv_field.
    TRY.
        lo_column ?= lo_cols_tab->get_column( lv_field ).
        lo_column->set_visible( abap_true ).
      CATCH cx_salv_not_found.
    ENDTRY.
  ENDLOOP.
*========
*
 
  o_salv->display( ).
 

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

2 Comments

  • steve oldner

    Nice trick.

    Thanks!

  • Jack Sugrue

    Thanks, Naimesh. That works perfectly! I used it on BKPF, which has several includes and with this code, I no longer have to manually set the “included” fields to abap_false.

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