Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Alv grid (oops):hiding empty column

Former Member
0 Kudos
3,666

Hi ,

I want to hide the column when the entire column is empty .

In the above screen shot  column Id of a switch ,switch position etc  is empty ,i need  to hide column when it is empty  .

5 REPLIES 5

former_member206575
Participant
0 Kudos
1,569

You can hide a column by using the set_technical property, in the column settings.

Below are code examples from a program I wrote before. Just an example, not ready to use.

*   Get the columns from ALV Table

    lr_columns_table = <table>->get_columns( ).

    if lr_columns_table is not initial.

      refresh : lt_t_column_ref.

      lt_t_column_ref = lr_columns_table->get( ).

...

...

...

...

*     Individual Column Properties.

      me->column_settings( exporting im_column_ref         = lt_t_column_ref

                                     im_rf_columns_table   = lr_columns_table

                                     im_table              = <table> ).

    endif.

In the Column_settings method, you loop over the columns and check if the column is completely empty.

If that is the case, you can use the method SET_TECHNICAL to remove the column from the table.

    loop at im_column_ref into ls_s_column_ref.

      try.

          lr_column_table ?=

            im_rf_columns_table->get_column( ls_s_column_ref-columnname ).

        catch cx_salv_not_found into rf_root.

          me->handle_error( exporting rf_oref = rf_root ).

      endtry.

...


*       Remove columns that are not required when empty

        if lr_column_table->get_columnname( ) in lra_hide_when_empty.

          lp_return = me->is_empty_column( im_column = ls_s_column_ref-columnname

                                                im_table  = main_list ).

          if lp_return = abap_true.

            lr_column_table->set_technical( if_salv_c_bool_sap=>true ).

          endif.

        endif.

Former Member
0 Kudos
1,569

HI,

   we can update field catalog as

 

    loop at lt_table into ls_tab where column is not initial.

       endloop.

       if sy-subrc <> 0.

          ls_fcat-no_out = c_true.   " ls_fcat-tech = c_true

        endif.

Regards,

Praveen

Former Member
0 Kudos
1,569

Hi Juneed,

Before passing  final internal table to display , check the internal table fields whether all fields having values in each row or not .If any one column field not having any data,just leave that field and pass remaining fields to another internal table and display it.so that no need of hiding the columns.

But if you want to hide the column means ,check the final internal table empty fields and hide using class CL_SALV_COLUMNS_TABLE and method REMOVE_COLUMN.If helpful please reward the points.

Regards
Ashok P

Former Member
0 Kudos
1,569

This solution works perfectly for me.

While creating the field catalog, add the following statement to each field.

data : flag.

   lv_pos = lv_pos + 1.
   CLEAR gw_alv_fieldcat.
   gw_alv_fieldcat-fieldname = 'ORT02'.
   gw_alv_fieldcat-tabname   = 'GT_OUT'.
   gw_alv_fieldcat-seltext_l = text-h11.
   gw_alv_fieldcat-col_pos   = lv_pos.
   gw_alv_fieldcat-outputlen = 10.

  clear flag.
    loop at gt_out into gw_out where ort02 ne '  '.
       flag = 'X'.
       exit.
    endloop .

* With the first non initial value, it will exit the loop. Hence there will not be more than one loop for each field in the field catalog.

    if flag is INITIAL.
      gw_alv_fieldcat-no_out = 'X'.
    endif.
 

APPEND gw_alv_fieldcat TO gt_alv_fieldcat.

You will be happy with the output.  (Just make sure that data is retrieved before creation of field catalog )

juancarlos_dasilva
Participant
0 Kudos
1,200

 

      DATA(where) = |{ ls_fieldcat-fieldname } IS NOT INITIAL|.
      LOOP AT gt_alv INTO DATA(ls_alv) WHERE (where).
      ENDLOOP.
      IF sy-subrc NE 0.
        ls_fcat-no_out = abap_true.
      ENDIF.